C # 6 new features to make your code cleaner

1, collection initializer

Copy the code
// 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
  {

  }

Copy the code

 

2, the dictionary initializer

    This I actually did not find how to streamline

Copy the code
 var dictionary = new Dictionary<string, string>
  {
       { "key1","value1"},
        { "key2","value2"}
   };

//新特性
 var dictionary1 = new Dictionary<string, string>
  {
         ["key1"]="value1",
         ["key2"]="value2"
  };
Copy the code

 

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.

Copy the code
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}";
         
Copy the code

 

4, air is determined

    Empty judge us often, C # 6 New features also make the code even more convenient new features

Copy the code
// 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?;
Copy the code

 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.

Copy the code
= 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]?;
Copy the code

 

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

Copy the code
Post class public 
{ 
      public int {GET Votes; Private SET;} 
} 


// new features in this way 
public class Post 
{ 
     public int {GET Votes;} 
}
Copy the code

 

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.

Copy the code
Post class public 
 { 
        
       public int AddOld () 
        { 
            return +. 1. 1; 
        } 
 
       // new properties or use only the syntax Lambda 
        public int the AddNew () => +. 1. 1; 

    }
Copy the code

 

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.

-----------------------------------------------------------------

Guess you like

Origin www.cnblogs.com/sexintercourse/p/12118459.html