C # 6.0 (C # VNext) new features: Expression bodied Functions and Properties

Source: Internet
Author: User

Expression bodied Function can be used in:
    • Methods
    • user-defined operators
    • Type conversions
    • Read-only Properties
    • Indexers
Look at the following example:
public class RgbColor (int R, int. g, int b) {public  int Red {get;} = r;  public int Green {get;} = g;  public int Blue {get;} = b;   public string Tohex () =    string. Format ("#{0:x2}{1:x2}{2:x2}", Red, Green, Blue);   public static RgbColor operator-(RgbColor color) =    new RgbColor (      color. Red ^ 0xFF,      color. Green ^ 0xFF,      color. Blue ^ 0xFF    );}

Examples of "methods" used:
public string Tohex () = string. Format ("#{0:x2}{1:x2}{2:x2}", Red, Green, Blue);p ublic override string ToString () = this. Name;

Examples of "user-defined operators":
  public static RgbColor operator-(RgbColor color) =    new RgbColor (      color. Red ^ 0xFF,      color. Green ^ 0xFF,      color. Blue ^ 0xFF    );

public static Complex operator + (Complex A, Complex b) = A.add (b);

Examples of "read-only property":
Special note that this is Read only and not Field
public string ID = = Guid.NewGuid (). ToString ();
Another example:
public class point (int x, int. y) {public    int x + = x;    public int y = = y;    public double Dist = math.sqrt (x * x + y * y);    Public point Move (int dx, int dy) = + new Point (x + dx, y + dy);}

Example of "type conversions": Implementing the implicit conversion between string and Name
public static implicit operator string (Name N) = N.first + "" + n.last;


Examples of "indexers" used:

Public Customer this[id Id] \ = store. Lookupcustomer (ID);







C # 6.0 (C # VNext) new features: Expression bodied Functions and Properties

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.