[C #6] 6-member functions in expression form,

Source: Internet
Author: User

[C #6] 6-member functions in expression form,
0. Directory

C #6 Add feature catalog

1. Old Version code
 1 internal class Person 2 { 3     public string FirstName { get; set; } 4     public string LastName { get; set; } 5  6     public string FullName 7     { 8         get { return FirstName + LastName; } 9     }10 11     public override string ToString()12     {13         return string.Format("[firstname={0},lastname={1}]", FirstName, LastName);14     }15 }

Generally, some simple read-only attributes and methods have only one line of code, but we have to follow the tedious syntax to implement it. C #6 comes with a simplified syntax that is highly consistent with lambda syntax to help us simplify these syntaxes. Let's take a look at the old version of the IL code (here I will not expand the IL, just look at the structure, all are common attributes and methods ):

2. member functions in Expression Form

Let's take a look at the new simplified writing methods:

1 internal class Person2 {3     public string FirstName { get; set; }4     public string LastName { get; set; }5 6     public string FullName => FirstName + LastName;7 8     public override string ToString() => string.Format("[firstname={0},lastname={1}]", FirstName, LastName);9 }

If the get declaration is omitted for the attribute, the method saves {} of the method and is represented in the => syntax. Let's take a look at IL:

Okay, I don't want to explain anything. I just say the same thing ,,,

3. Example
1 internal class Point 2 {3 public int X {get; private set;} 4 public int Y {get; private set;} 5 6 public Point (int x, int y) 7 {8 this. X = x; 9 this. Y = y; 10} 11 12 public Point Add (Point other) 13 {14 return new Point (this. X + other. x, this. Y + other. y); 15} 16 17 // method 1, return value 18 public Point Move (int dx, int dy) => new Point (X + dx, Y + dy ); 19 20 // method 2, no return value 21 public void Print () => Console. writeLine (X + "," + Y); 22 23 // static method, operator overload 24 public static Point operator + (Point a, Point B) =>. add (B); 25 26 // read-only attribute, which can only be used for the read-only attribute 27 public string Display => "[" + X + "," + Y + "]"; 28 29 // indexer 30 public int this [long id] => 1; 31} 32 33 internal class Program34 {35 private static void Main () 36 {37 Point p1 = new Point (1, 1); 38 Point p2 = new Point (2, 3); 39 Point p3 = p1 + p2; 40 // output: [3, 4] 41 Console. writeLine (p3.Display); 42} 43}

This new syntax only simplifies the syntax and does not actually change it. The compilation result is exactly the same as that of earlier versions.

4. Reference

Method Expression Body Definitions

Property Expression Body Definitions

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.