In C# 6 Microsoft has introduced Expression-bodied Method and properties. Here I am go to explain only about Expression-bodied Properties if you are interested to know about Expression-bodied method then check here .It’s inspired by lambda expression.

In C# 5

  1. get { return string.Format("{0} {1} {2} {3}", UserId, UserName, EmailId, ContactNumber); }
Complete code
  1. class User
  2. {
  3. public int UserId { get; set; }
  4. public string UserName { get; set; }
  5. public string EmailId { get; set; }
  6. public string ContactNumber { get; set; }
  7. public string UserDetails
  8. {
  9. get { return string.Format("{0} {1} {2} {3}", UserId, UserName, EmailId, ContactNumber); }
  10. }
  11. }

In C# 6

  1. public string UserDetails => $"{UserId} {UserName} {EmailId} {ContactNumber}";
Complete Code
  1. class User
  2. {
  3. public int UserId { get; set;}
  4. public string UserName { get; set; }
  5. public string EmailId { get; set; }
  6. public string ContactNumber { get; set; }
  7. public string UserDetails => $"{UserId} {UserName} {EmailId} {ContactNumber}";
  8. }

Or

  1. class User
  2. {
  3. public int UserId { get; } = 1001;
  4. public string UserName { get; } = "BNarayan";
  5. public string EmailId { get; } = "[email protected]";
  6. public string ContactNumber { get; } = "99xxxxxxxx";
  7. public string UserDetails => $"{UserId} {UserName} {EmailId} {ContactNumber}";
  8. }

Example 2:

In C# 5

  1. public class Rectangle
  2. {
  3. public double Width { get; set; }
  4. public double Lenght { get; set;}
  5. public double Area { get { return Width * Lenght; } }
  6. }

In C# 6

  1. public class Rectangle
  2. {
  3. public double Width { get; set; } = 10.0;
  4. public double Lenght { get; set; } = 10.0;
  5. public double Area => Width * Lenght;
  6. }