1, collection initializer
// old syntax, a class that you want to initialize some private property, it would have to work hard in the constructor.
Post class public { public the DateCreated the DateTime {GET; Private SET;} public List <it Comment> Comments {GET; Private SET;} public Post () { the DateCreated = the DateTime.Now; Comments = new new List <it Comment> (); } } the Comment class public { }
// use the new features, we can initialize private property, and do not have to create a constructor
public class Post
{
public DateTime DateCreated { get; private set; } = DateTime.Now;
public List<Comment> Comments { get; private set; } = new List<Comment>();
}
public class Comment
{
}
2, the dictionary initializer
This I actually did not find how to streamline
var dictionary = new Dictionary<string, string> { { "key1","value1"}, { "key2","value2"} }; //新特性 var dictionary1 = new Dictionary<string, string> { ["key1"]="value1", ["key2"]="value2" };
3、string.Format
Often string concatenation of this method is certainly not a model, either string.Format, or is a StringBuilder. This is my latest favorite of a new feature.
Post new new POST = Post (); post.Title = "the Title"; post.Content = "Content"; // Under normal circumstances we are so write string t1 = string.Format ( "{0 } _ {1}", post.Title, post.Content); // C # 6 where we can write, introduced $ backstage, and support smart tips. string t2 = $ "{post.Title} _ {post.Content}";
4, air is determined
Empty judge us often, C # 6 New features also make the code even more convenient new features
// old syntax, simple but tedious. I feel very cumbersome Post POST = null; String title = ""; IF (POST = null!) { Title = post.Title; } // C # 6 new properties a code to get an empty judgment title = post .Title?;
Empty set judgment, this scenario we really see them at work too, taken out from the database collection, empty judgment, judgment will encounter an empty set.
= Null POST POST; List <POST> Posts = null; IF (! Posts = null) { POST Posts = [0]; } // new features, we also get a code. Is not it cool? post = posts [0]?;
5, getter-only initializer
I am not much feel that this is a new feature, official explanation given is that when we want to create a read-only attribute we'll automatically so defined as follows
Post class public { public int {GET Votes; Private SET;} } // new features in this way public class Post { public int {GET Votes;} }
6, the expression of the method body
English Expression Bodied Members. In fact, I think that is an extension Lambda, is not really new features.
Post class public { public int AddOld () { return +. 1. 1; } // new properties or use only the syntax Lambda public int the AddNew () => +. 1. 1; }
7, with reference to static using static class methods
I totally do not understand the characteristics of the design intent where the original static method called directly tell at a glance which class of that method, now let you use using static XXX introduction class. Then call directly to their way, that code is not her own writing, one also can not see this method of that class membership.