C# 6.0
Expression bodied Methods
Expression bodied Properties
C# 7.0
Expression bodied constructor
Expression bodied destructor
Expression bodied getters
Expression bodied setters
Expression bodied Events.
Expression bodied Methods:
Let us learn the Previous Release of Expression bodied Method in C sharp 6.0.
Sample Syntax :
public string GetUserDetails
() => $ "{UserId} + {UserName} + {EmailId} + {ContactNumber}";
Example:
class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string EmailId { get; set; }
public string ContactNumber { get; set; }
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args) {
User obj = new User();
obj.UserId = 001;
obj.UserName = "Rajakonda Uday Kumar";
obj.EmailId = "rajakondauday@gmail.com";
obj.ContactNumber = "123456";
Console.WriteLine(obj.GetUserDetails());
Console.ReadLine();
}
}
There might be the situation where we want to override the ToString method in that case
class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string EmailId { get; set; }
public string ContactNumber { get; set; }
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
public override string ToString() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
obj.UserId = 001;
obj.UserName = "Rajakonda Uday Kumar";
obj.EmailId = "rajakondauday@gmail.com";
obj.ContactNumber = "123456";
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
Expression bodied Methods
Expression bodied Properties
C# 7.0
Expression bodied constructor
Expression bodied destructor
Expression bodied getters
Expression bodied setters
Expression bodied Events.
Let us learn the Previous Release of Expression bodied Method in C sharp 6.0.
Sample Syntax :
public string GetUserDetails
() => $ "{UserId} + {UserName} + {EmailId} + {ContactNumber}";
Example:
class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string EmailId { get; set; }
public string ContactNumber { get; set; }
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args) {
User obj = new User();
obj.UserId = 001;
obj.UserName = "Rajakonda Uday Kumar";
obj.EmailId = "rajakondauday@gmail.com";
obj.ContactNumber = "123456";
Console.WriteLine(obj.GetUserDetails());
Console.ReadLine();
}
}
Output:
class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string EmailId { get; set; }
public string ContactNumber { get; set; }
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
public override string ToString() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
obj.UserId = 001;
obj.UserName = "Rajakonda Uday Kumar";
obj.EmailId = "rajakondauday@gmail.com";
obj.ContactNumber = "123456";
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
Expression Bodied Properties:
class User
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; } = "123456";
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
public override string ToString() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
class User
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; } = "123456";
public string GetUserDetails() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
public override string ToString() => $"{UserId} + {UserName} + {EmailId} + {ContactNumber}";
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
In C Sharp 7.0
Expression
bodied constructor
class User
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public User()=> ContactNumber= "99898955555";
}
class User
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public User()=> ContactNumber= "99898955555";
~User() => Console.WriteLine("Destructor Method");
}
class Program
{
static void Main(string[] args)
{
Details();
GC.Collect();
Console.ReadLine();
}
public static void Details()
{
Console.WriteLine(new User().ContactNumber);
User user = new User();
}
}
{
public Dictionary<int, string> UserList = new Dictionary<int, string>();
public int UserId { get; } = 101;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public string UserDetails
{ get => UserList[UserId];
set => UserList[UserId] = value;
}
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
obj.UserList.Add(101, "A");
obj.UserList.Add(102, "B");
Console.WriteLine(obj.UserDetails);
Console.ReadLine();
}
}
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public User()=> ContactNumber= "99898955555";
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new User().ContactNumber);
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
Console.WriteLine(new User().ContactNumber);
Console.ReadLine();
}
}
Output:
Expression bodied destructor
{
public int UserId { get; } = 0001;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public User()=> ContactNumber= "99898955555";
~User() => Console.WriteLine("Destructor Method");
}
class Program
{
static void Main(string[] args)
{
Details();
GC.Collect();
Console.ReadLine();
}
public static void Details()
{
Console.WriteLine(new User().ContactNumber);
User user = new User();
}
}
Expression bodied getters & setters
class User{
public Dictionary<int, string> UserList = new Dictionary<int, string>();
public int UserId { get; } = 101;
public string UserName { get; } = "Rajakonda Uday Kumar";
public string EmailId { get;} = "rajakondauday@gmail.com";
public string ContactNumber { get; }
public string UserDetails
{ get => UserList[UserId];
set => UserList[UserId] = value;
}
}
class Program
{
static void Main(string[] args)
{
User obj = new User();
obj.UserList.Add(101, "A");
obj.UserList.Add(102, "B");
Console.WriteLine(obj.UserDetails);
Console.ReadLine();
}
}
Examples:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($" {new Employee().EmployeeDetail()}");
Console.ReadLine();
}
}
class Employee
{
int empId { get; } = 101;
string Name { get; } = "ABC";
string Address { get; } = "Hyderabad";
double Salary { get; } = 1000;
public double HRA() => (Salary * 40) / 100;
public string EmployeeDetail() => $"EmployeeId: {empId}\n EmployeeName: {Name}\nEmployeeAddress: {Address}\nEmployeeHRA: {HRA()}";
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine($" {new Employee().EmployeeDetail()}");
Console.ReadLine();
}
}
class Employee
{
int empId { get; } = 101;
string Name { get; } = "ABC";
string Address { get; } = "Hyderabad";
double Salary { get; } = 1000;
public double HRA() => (Salary * 40) / 100;
public string EmployeeDetail() => $"EmployeeId: {empId}\n EmployeeName: {Name}\nEmployeeAddress: {Address}\nEmployeeHRA: {HRA()}";
}
Output:
2) Example:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($" {new Employee().EmployeeDetail()}");
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
Console.WriteLine($" {new Employee().EmployeeDetail()}");
Console.ReadLine();
}
}
class Employee
{
int empId { get; } = 101;
string Name { get; } = "ABC";
string Address { get; } = "Hyderabad";
double Salary { get; } = 1000;
public Employee() => Salary = 1000;
{
int empId { get; } = 101;
string Name { get; } = "ABC";
string Address { get; } = "Hyderabad";
double Salary { get; } = 1000;
public Employee() => Salary = 1000;
public double HRA() => (Salary * 40) / 100;
public string EmployeeDetail() => $"EmployeeId: {empId}\n EmployeeName: {Name}\nEmployeeAddress: {Address}\nEmployeeHRA: {HRA()}";
public string EmployeeDetail() => $"EmployeeId: {empId}\n EmployeeName: {Name}\nEmployeeAddress: {Address}\nEmployeeHRA: {HRA()}";
~Employee() => Console.WriteLine("\n Destructor");
}
}