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