C #6 new features make your code cleaner,

Source: Internet
Author: User

C #6 new features make your code cleaner,

When I saw a friend's blog a few days ago, I saw that he used the features of C #6, but I haven't seen it for so long. Now I have read new features, to put it bluntly, it's just syntactic sugar. But it can make your code clean. Let's try it.


1. Set Initiator
// In the old syntax, if a class wants to initialize several private attributes, it has to work on the constructor.
Public class Post {public DateTime DateCreated {get; private set;} public List <Comment> Comments {get; private set;} public Post () {DateCreated = DateTime. now; Comments = new List <Comment> () ;}} public class Comment {}

// With the new features, we can initialize private properties in this way, instead of creating constructors.

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. dictionary Initiator

I haven't found out how simple it is.

Var dictionary = new Dictionary <string, string >{{ "key1", "value1" },{ "key2", "value2 "}}; // new feature var dictionary1 = new Dictionary <string, string> {["key1"] = "value1", ["key2"] = "value2 "};


3. string. Format

The method that is often concatenated with strings is definitely not in the pattern, either string. Format or StringBuilder. This is a new feature that I like most recently.

Post post = new Post (); post. title = "Title"; post. content = "Content"; // we usually write string t1 = string. format ("{0 }_{ 1}", post. title, post. content); // we can write this in C #6. $ is introduced in the background, and smart prompts are supported. String t2 = $ "{post. Title }_{ post. Content }";


4. null judgment

Empty judgment is also common, and the new features in C #6 make the new feature code easier.

// The old syntax is simple but tedious. I think it is very complicated to Post post = null; string title = ""; if (post! = Null) {title = post. Title;} // C #6 new feature a code to get null judgment title = post ?. Title;

Empty set judgment. In this scenario, we see too much in our work. The empty set judgment and empty set judgment obtained from the database will be met.

Post post = null; List <Post> posts = null; if (posts! = Null) {post = posts [0];} // new features. We can also use a code. Is it nice? Post = posts? [0];


5. getter-only initializer

I don't think this is a new feature. The official explanation is that when we want to create a read-only automatic attribute, we will define it as follows:

Public class Post {public int Votes {get; private set ;}// this method is used for new features: public class Post {public int Votes {get ;}}


6. Method body expression

Expression Bodied Members in English. In fact, what I think is the extension of Lambda is not a new feature.

Public class Post {public int AddOld () {return 1 + 1;} // The new feature still uses the Lambda syntax. public int AddNew () => 1 + 1 ;}


7. Use static using to reference static class methods

I have no idea where the design intent of this feature is. The method of which class can be directly called by a static method at a Glance. Now you can use using static XXX to introduce the class. Then, call the method directly. The code is not self-written, and it cannot be seen at a glance that the method belongs to that class.



Among them, string interpolation and null judgment are my favorite features. Of course, the collection may not be complete. Please add. At the same time, I look forward to developing ASP. NET Core. Products responsible for. net.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.