Ç°ÑÔ
΢Èí×òÌì·¢²¼ÁËеÄVS 2017 ..ËæÖ®¶øÀ´µÄ»¹ÓкܶàºÜ¶à¶«Î÷... .NETа汾 ASP.NETа汾...µÈµÈ..Ì«¶à..ʵÔÚûÏû»¯..
·ÖÏíÒ»ÏÂÆäʵ2016Äê12Ô¾ÍÒѾ¹«²¼Á˵ÄC#7.0µÄÐÂÌØÐÔ°É,ËäÈ»ºÜÔç¾Í³öÀ´ÁË,µ«ÔÛÕâIDE²»Ö§³Ö°¡..
²»¹ýÔÚ×òÌìµÄVS2017ÖÐÒѾÍêÃÀ¿ÉÒÔÖ§³ÖʹÓÃÁË.
EÎĺõÄ,ÒƲ½¹Ù·½½éÉܵØÖ·:https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/csharp-7
ÏÈÁÐÒ»ÏÂÏà¹ØµÄÓï·¨:
1.out-variables(Out±äÁ¿)
2.Tuples(Ôª×é)
3.Pattern Matching(Æ¥Åäģʽ)
4.ref locals and returns (¾Ö²¿ÒýÓúÍÒýÓ÷µ»Ø)
5.Local Functions (¾Ö²¿º¯Êý)
6.More expression-bodied members(¸ü¶àµÄº¯Êý³ÉÔ±µÄ±í´ïʽÌå)
7.throw Expressions (Òì³£±í´ïʽ)
8.Generalized async return types (ͨÓÃÒì²½·µ»ØÀàÐÍ)
9.Numeric literal syntax improvements(ÊýÖµÎÄ×ÖÓï·¨¸Ä½ø)
ÕýÎÄ
1. out-variables(Out±äÁ¿)ÒÔÇ°,ÎÒÃÇʹÓÃout±äÁ¿µÄʱºò,ÐèÒªÔÚÍⲿÏÈÉêÃ÷,È»ºó²ÅÄÜ´«Èë·½·¨,ÀàËÆÈçÏÂ:
string ddd = ""; //ÏÈÉêÃ÷±äÁ¿ ccc.StringOut(out ddd); Console.WriteLine(ddd);
ÔÚC#7.0ÖÐÎÒÃÇ¿ÉÒÔ²»±ØÉêÃ÷,Ö±½ÓÔÚ²ÎÊý´«µÝµÄͬʱÉêÃ÷Ëü,ÈçÏÂ:
StringOut(Console.WriteLine(ddd); Console.ReadLine();
2.Tuples(Ôª×é)
Ôø½ñÔÚ.NET4.0ÖÐ,΢Èí¶Ô¶à¸ö·µ»ØÖµ¸øÁËÎÒÃÇÒ»¸ö½â¾ö·½°¸½ÐÔª×é,ÀàËÆ´úÂëÈçÏÂ:
static void Main(string[] args) { var data = GetFullName(); Console.WriteLine(data.Item1); Console.WriteLine(data.Item2); Console.WriteLine(data.Item3); Console.ReadLine(); } static Tuple<string, string, string> GetFullName() { , , ); }
ÉÏÃæ´úÂëչʾÁËÒ»¸ö·½·¨,·µ»Øº¬ÓÐ3¸ö×Ö·û´®µÄÔª×é,È»¶øµ±ÎÒÃÇ»ñÈ¡µ½Öµ,ʹÓõÄʱºò ÐÄÒѾըÁË,Item1,Item2,Item3ÊÇʲô¹í,ËäÈ»´ïµ½ÁËÎÒÃǵÄÒªÇó,µ«ÊÇʵÔÚ²»ÓÅÑÅ
ÄÇô,ÔÚC#7.0ÖÐ,΢ÈíÌṩÁ˸üÓÅÑŵķ½°¸:(×¢Òâ:ÐèҪͨ¹ýnugetÒýÓÃSystem.ValueTuple)ÈçÏÂ:
static void Main(string[] args) { var data=GetFullName(); Console.WriteLine(data.a); //¿ÉÓÃÃüÃû»ñÈ¡µ½Öµ Console.WriteLine(data.b); Console.WriteLine(data.c); Console.ReadLine(); } (string a,string b,string c) GetFullName() { ,,); }
½â¹¹Ôª×é,ÓеÄʱºòÎÒÃDz»ÏëÓÃvarÄäÃûÀ´»ñÈ¡,ÄÇôÈçºÎ»ñÈ¡abcÄØ?ÎÒÃÇ¿ÉÒÔÈçÏÂ:
static void Main(string[] args) { //¶¨Òå½â¹¹Ôª×é (string a, string b, string c) = GetFullName(); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.ReadLine(); } private static (string a,string b,string c) GetFullName() { ,,); }
3. Pattern Matching(Æ¥Åäģʽ)
ÔÚC#7.0ÖÐ,ÒýÈëÁËÆ¥ÅäģʽµÄÍæ·¨,ÏȾٸöÀÏÀõ×Ó.Ò»¸öobjectÀàÐÍ,ÎÒÃÇÏëÅжÏËûÊÇ·ñΪintÈç¹ûÊÇintÎÒÃǾͼÓ10,È»ºóÊä³ö,ÐèÒªÈçÏÂ:
object a = 1; { d = b+10; //¼Ó10 Console.WriteLine(d); //Êä³ö }
ÄÇôÔÚC#7.0ÖÐ,Ê×ÏȾÍÊǶÔisµÄÒ»¸öСÀ©Õ¹,ÎÒÃÇÖ»ÐèÒªÕâÑùд¾ÍÐÐÁË,ÈçÏÂ:
object a = 1; { int d = c + 10; Console.WriteLine(d); }
ÕâÑùÊDz»ÊǺܷ½±ã?ÌرðÊǾ³£Ó÷´ÉäµÄͬ־ÃÇ..
ÄÇôÎÊÌâÀ´ÁË,ÍÚ¾ò»ú¼¼ÊõÄļÒÇ¿?!(¿È¿È,ÅÞ ¿ªÍæЦ)
ÆäʵÊÇ,Èç¹ûÓжàÖÖÀàÐÍÐèҪƥÅä,ÄÇÔõô°ì?¶à¸öif else?µ±È»Ã»ÎÊÌâ,²»¹ý,΢Èí°Ö°ÖÒ²ÌṩÁËswitchµÄÐÂÍæ·¨,ÎÒÃÇÀ´¿´¿´,ÈçÏÂ:
ÎÒÃǶ¨ÒåÒ»¸öAddµÄ·½·¨,ÒÔObject×÷Ϊ²ÎÊý,·µ»Ø¶¯Ì¬ÀàÐÍ
static dynamic Add(object a) { dynamic data; switch (a) { case int b: data=b++; break; case string c: data= c + ; break; default: data = null; break; } return data; }
ÏÂÃæÔËÐÐ,´«ÈëintÀàÐÍ:
object a = 1; var data= Add(a); Console.WriteLine(data.GetType()); Console.WriteLine(data);
Êä³öÈçͼ:
ÎÒÃÇ´«ÈëStringÀàÐ͵IJÎÊý,´úÂëºÍÊä³öÈçÏÂ:
; var data= Add(a); Console.WriteLine(data.GetType()); Console.WriteLine(data);
ͨ¹ýÈçÉÏ´úÂë,ÎÒÃǾͿÉÒÔÌå»áµ½switchµÄÐÂÍæ·¨ÊǶàôµÄ˳³©ºÍÇ¿´óÁË.
Æ¥ÅäģʽµÄCase Whenɸѡ
ÓеĻùÓѾÍÒªÎÊÁË.¼ÈÈ»ÎÒÃÇ¿ÉÒÔÔÚSwitchÀïÃæÆ¥ÅäÀàÐÍÁË,ÄÇÎÒÃÇÄܲ»ÄÜ˳±ãɸѡһÏÂÖµ?´ð°¸µ±È»Êǿ϶¨µÄ.
ÎÒÃÇ°ÑÉÏÃæµÄSwitch´úÂë¸ÄÒ»ÏÂ,ÈçÏÂ:
¡¡