Sunday, February 10, 2019

Expression bodied members in C Sharp 7.0

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();
        }
    }

Output:


 
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 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();
        }
    }





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 Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new User().ContactNumber);
            Console.ReadLine();
        }
    }

Output:


 
Expression bodied destructor
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();
        }

    }

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()}";
    }

 
Output:
 

2) Example:
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 Employee() => Salary = 1000;
        public double HRA() => (Salary * 40) / 100;
        public string EmployeeDetail() => $"EmployeeId: {empId}\n EmployeeName: {Name}\nEmployeeAddress: {Address}\nEmployeeHRA: {HRA()}";
        ~Employee() => Console.WriteLine("\n Destructor");
    }
 

C Sharp Throw Exception Directly


From C sharp 7.0 we can throw an exception directly through expression.

New Access Specifier in C sharp 7.2

In C# 7.2 a private protected has been added. So they are total 6 access specifier
 
From C Sharp 7.2.
  •         public
  •         protected
  •         internal
  •         private
  •         protected internal
  •         private protected
  To know about previous access specifier please go through this.


Private Protected: It really means protected AND internal.

 
From the above Example you can see that it gives Me Error. Reason is
 
 

To Fix this,
In Visual Studio 2017 first, we need to change the Language Type to C Sharp 7.2 as shown below

Goto Project File-> Right Click and Select Properties.

That is - member is accessible only to child classes which are in the same assembly, but not to child classes which are outside assembly.

 
Now let me try to access this From the Outside Assembly.
1) private protected member is inaccessible outside the assembly for the child class.


 
2) private protected member is inaccessible inside the assembly for non-inherited class.
 

Error:


Thursday, November 1, 2018

New Feature in C Sharp 7.0


Pattern Matching: Patterns are introduced in 7.0.

We can perform Pattern on Any Data Type.

We can also extract a value from an expression.

We can do Pattern matching in Two Cases which is useful for us.

            Using the is expression.

            Using the switch statement.

Using is expression:

‘is’ Pattern Matching

Using this we can assign to a variable.

Before C#7.0

class Program
    {
        public void GetDetails(dynamic a)
        {
            if (a is Employee) {
                var c = a as Employee;
                Console.WriteLine(c.Empid);
            }
            else if (a is Student) {
                var c = a as Student;
                Console.WriteLine(c.StudentId);
                Console.ReadLine();
            }
        }
        static void Main(string[] args) {
            dynamic a = new Student();
            a.StudentId = 1;
            Program obj = new Program();
            obj.GetDetails(a);
        }
    }
    class Employee {
        public int Empid { get; set; }
    }
    class Student{
        public int StudentId { get; set; }
    }

Output:




After C# 7.0

class Program
    {
        public void GetDetails(dynamic a)
        {
            if (a is Employee emp)
                Console.WriteLine(emp.Empid);
            if (a is Student student)
                Console.WriteLine(student.StudentId);
                Console.ReadLine();
        }
        static void Main(string[] args) {
            dynamic a = new Student();
            a.StudentId = 1;
            Program obj = new Program();
            obj.GetDetails(a);
        }
    }
    class Employee {
        public int Empid { get; set; }
    }
    class Student{
        public int StudentId { get; set; }
    }

Using the switch statement.
class Program
    {
        public void GetDetails(dynamic a)
        {
            switch (a)
            {
                case Employee emp:
                    Console.WriteLine(emp.Empid);
                    Console.ReadLine();
                    break;
                case Student student:
                    Console.WriteLine(student.StudentId);
                    Console.ReadLine();
                    break;
            }
        }
        static void Main(string[] args) {
            dynamic a = new Student();
            a.StudentId = 1;
            Program obj = new Program();
            obj.GetDetails(a);
        }
    }
    class Employee {
        public int Empid { get; set; }
    }
    class Student{
        public int StudentId { get; set; }
    }

Output:




Switch Statement with When Condition

class Program
    {
        public void GetDetails(dynamic a)
        {
            switch (a)
            {
                case Employee emp:
                    Console.WriteLine(emp.Empid);
                    Console.ReadLine();
                    break;
                case Student student when student.StudentId==1:
                    Console.WriteLine(student.StudentId);
                    Console.ReadLine();
                    break;
                case Student student when student.StudentId == 2:
                    Console.WriteLine(student.StudentId);
                    Console.ReadLine();
                    break;
                case Student student when student.StudentId == 3:
                    Console.WriteLine(student.StudentId);
                    Console.ReadLine();
                    break;
            }
        }
        static void Main(string[] args) {
            dynamic a = new Student();
            a.StudentId = 3;
            Program obj = new Program();
            obj.GetDetails(a);
        }
    }
    class Employee {
        public int Empid { get; set; }
    }
    class Student{
        public int StudentId { get; set; }
    }

Output:


Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...