New features in VS2015 C #6.0,
Preface
VS2015 was indeed installed on its own machine, and it took a long time to experience the pleasure of cross-platform. As a result, Microsoft was so awesome that it was useless to install it, you should also install the Xarmain plug-in to configure some parameters. Since this item has never been used before, you have to think about it before and continue to waste time here, so you can try new features.
The original blog address is http://aehyok.com/blog/detail/66.html.
This article reference http://roslyn.codeplex.com, reference PDF document http://files.cnblogs.com/aehyok/VS2015CSharp6.0.pdf
1. Automatic attribute Enhancement
1.1. Initializers for auto-properties)
C #4.0 cannot be implemented.
C #6.0 automatic attribute Initialization Method
If you have been familiar with C #, you will certainly like this method. It's really simple and convenient.
1.2 read-only attributes initialize Getter-only auto-properties
Let's take a look at the method we used before.
public class Customer { public string Name { get; } public Customer(string firstName,string lastName) { Name = firstName +" "+ lastName; } }
Let's take a look at C #6.0.
public class Customer { public string FirstName { get; }="aehyok"; public string LastName { get; }="Kris"; }
It is consistent with the first automatic attribute initialization method.
2. Expression bodied function members
2.1 use Lambda as the function body Expression bodies on method-like members
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
Here is a simple example: a function without return values.
public void Print() => Console.WriteLine(FirstName + " " + LastName);
2.2 Lambda Expression used as the attribute Expression bodies on property-like function members
public override string ToString() { return FirstName + " " + LastName; }
In C #6
public class User { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() => string.Format("{0}——{1}", FirstName, LastName); public string FullName => FirstName + " " + LastName; }
3. Reference Static class Using Static
You can specify a static class in Using, and then you can directly use static members in subsequent code.
4. Null-conditional operators
Let's look at the code and running results.
Through the results, we can find that all the returned values are null, which is no longer as tedious as before.
5. String embedding Value
Embedded value in string
Which of the following methods has been used before?
Now we can simply splice the data as follows:
6. nameof expression nameof expressions
During the method parameter check, you may often see such code (less used before, this time also learned)
public static void AddCustomer(Customer customer) { if (customer == null) { throw new ArgumentNullException("customer"); } }
The customer is a handwritten string. When we rename the customer, it is easy to forget the string below. C #6.0 nameof helps us solve this problem and look at the new writing method.
public static void AddCustomer(Customer customer) { if (customer == null) { throw new ArgumentNullException(nameof(customer)); } }
7. Index initializers
Objects can be initialized directly through indexes.
In this way, we can find that there are only three elements in the dictionary, so only these three indexes can be accessed. Other types of objects and collections can also be initialized in this way, we will not list them here.
8. Exception filters)
Let's look at a porting method.
try { var numbers = new Dictionary<int, string> {[7] = "seven",[9] = "nine",[13] = "thirteen" }; } catch (ArgumentNullException e) { if (e.ParamName == "customer") { Console.WriteLine("customer can not be null"); } }
The Microsoft document also provides another usage. This exception will be thrown to the caller at the previous layer when the log record fails.
Private static bool Log (Exception e) {// process some logs return false;} static void Main (string [] args) {try {//} catch (Exception e) {if (! Log (e) {}} Console. ReadLine ();}
9. await in catch and finally -- Await in catch and finally blocks
In C #5.0, The await keyword cannot appear in catch or finnaly blocks. In 6.0
try { res = await Resource.OpenAsync(…); // You could do this. … } catch (ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
10. Parameterless constructors in structs, a struct without Parameters
Summary
I have seen an article published by a big God. It feels good. I also learned a lot of new things.
Personal website address: aehyok.com
QQ Technical Group No.: 206058845, verification code: aehyok
Article link: http://www.cnblogs.com/aehyok/p/3946286.html
Thank you for reading this article. If you are interested in the content described in my blog, please make a suggestion. Thank you for your support:-O.