In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are the new features of C # version 6.0in VS2015? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Note: these new features can only be used in VS2015 and later versions, not in lower versions such as VS2013, VS2010, etc. Of course, if you don't like these new features, you can still use the original usage (so it's a new grammatical sugar).
1. Improvement of automatic attribute initialization (useful)
The original usage (cannot be initialized at the same time when declared), for example:
Class MyClass {public int Age {get; set;} public string Name {get; set;} public MyClass () {Age = 20; Name = "Zhang San";}}
New usage (it can be initialized at the same time when declared, which is more convenient), for example:
Class MyClass {public int Age {get; set;} = 20; public string Name {get; set;} = "Zhang San";}
2. Improvement of String.Format (useful)
Original usage: use string.Format (…) Implementation, for example:
Class MyClass {public void MyMethod () {string name = "Zhang San"; int age = 20; string S1 = string.Format ("{0}, {1}", name, age); string S2 = string.Format ("name = {0}, age = {1}", name, age); string S3 = string.Format ("{0c15}, {1:d3}", name, age) String S4 = string.Format ("{0jue 15}, {1d3}", name, age); Console.WriteLine ("{0}, {1}, {2}, {3}", S1, S2, S3, S4); string S5 = string.Format ("{0:yyyy-MM-dd}", DateTime.Now);}}
New usage: implemented with the "$" prefix (variables are written directly into curly braces and with smart tips, which is more convenient), for example:
Class MyClass {public void MyMethod () {string name = "Zhang San"; int age = 20; string S1 = $"{name}, {age}"; string S2 = $"name = {name}, age = {age}"; string S3 = $"{name,15}, {age:d3}"; string S4 = $"{name,15}, {age,10:d3}" Console.WriteLine ($"{S1}, {S2}, {S3}, {S4}"); string S5 = $"{DateTime.Now:yyyy-MM-dd}";}}
3. Initialization of dictionary
The original usage:
Class MyClass {public void MyMethod () {Dictionary student = new Dictionary (); student.Add ("A1", 15); student.Add ("a2", 14); student.Add ("A3", 16);}}
New usage (you can write initialized values directly, which is more convenient):
Class MyClass {public void MyMethod () {Dictionary student = new Dictionary () {["A1"] = 15, ["a2"] = 14, ["a3"] = 16};}}
4. You can declare references to static classes with static
The original usage:
Using System;namespace MyApp {class Demo1New {public static double MyMethod (double x, double angle) {return Math.Sin (x) + Math.Cos (angle);}
New usage (useful when expressions are more complex, code is more concise):
Using static System.Math;namespace MyApp {class Demo1New {public static double MyMethod (double x, double angle) {return Sin (x) + Cos (angle);}
5. Nameof expression
Suppose you have the following classes in your WPF application:
Public class MyClass {public string MyText {get; set;} = "aaa";}
And assume the following XAML code:
……
The original usage in the code-behind class:
Txt1.SetBinding (TextBlock.TextProperty, "MyText")
Current usage (because of the error checking intelligence prompt, it is more convenient to use):
Txt1.SetBinding (TextBlock.TextProperty, nameof (MyClass.MyText))
6. Null- conditional expression
(useful)
Var ss = new string [] {"Foo", null}; var length0 = ss [0]? .length; / / result is 3var length2 = ss [1]? .length; / / result is nullvar lengths = ss.Select (s = > s?.Length?? 0); / / result is [3,0]
7. Use await in try-catch-finally
In asynchronous programming, you can't use await in catch or finally, but now you can:
Async void SomeMethod () {try {/ /... etc... } catch (Exception x) {var diagnosticData = await GenerateDiagnosticsAsync (x); Logger.log (diagnosticData);} finally {await someObject.FinalizeAsync ();}}
8. Other
There are some new features in C# 6.0, which are not used too much for beginners, so I won't cover them here.
Again, if you don't like the new features, you can still use the original usage.
After reading the above, have you mastered the new features of C # version 6. 0 in VS2015? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.