Monday, January 14, 2019

Class in C#

What is Class in C#.
A class is blue print of object .its contain data and behaviour.
Class field define data and Method represent class behaviour.
Class Contain 
  • Class property
  • Method 
  • Constructor
  • And Destructor
Class :-

using System;

namespace CSharpLearnig
{
    public class Employee
    {
        private string fName;
        private string lName;

        // Parameterize Constructor 
        public Employee(string firstName,string LastName)
        {
            this.fName = firstName;
            this.lName = LastName;
        }

        //  Method
        public string getEmployeeFullName()
        {
            return string.Format("Employee Name is {0} {1}", this.fName, this.lName);
        }

       // Destructor
        ~Employee()
        {

        }
    }
}

 Create a class object by using new keyword 

using System;
using System.Collections.Generic;

namespace CSharpLearnig
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a class object by using new keyword
            Employee empObj = new Employee("Asp", ".Net");
            string EmpName= empObj.getEmployeeFullName();
            Console.WriteLine(EmpName);
            Console.ReadLine();
        }

    }
}

Output :

Constructor :-

  • The main use of constructor to initialise the class fields in a class.
  • Constructor  is a special method that is use to initialise the class field.
  • A constructor called at the time of class object creation .
  • Constructor name must be same as class name.

Types of Constructor :-
  • Default Constructor
  • Parameterised Constructor
  • Static Constructor
  • Copy Constructor 
  • Private Constructor 
Default Constructor:-
A parameter less constructor is called default constructor. If we do not create default constructor A class automatically created default constructor.

using System;

namespace CSharpLearnig
{
    public class Employee
    {
        private string fName;
        private string lName;

        // Default Constructor 
        public Employee()
        {
          
        }
    }
}

Parameterised Constructor:-
A Constructor have at least one parameter is called is called parameterised constructor.
using System;

namespace CSharpLearnig
{
    public class Employee
    {
        private string fName;
        private string lName;

        // Parameterize Constructor 
        public Employee(string firstName,string LastName)
        {
            this.fName = firstName;
            this.lName = LastName;
        }
    }
}

Static Constructor:-
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

  class Circle
    {
        public static float pi;
        public int radius;
        // Static Constructor
        static Circle()
        {
            pi = 3.14F;
        }

        public Circle()
        {
            radius = 4;
        }
        public static void Print()
        {
            Circle c = new Circle();
            Console.WriteLine("Pi is {0} and radius is {1}",pi, c.radius);
        }
    }

Copy Constructor:-
The constructor which creates an object by copying variables from another object is called a copy constructor. 
public class Employee
{
    public string firstName;
    public string lastName;
    public string position;
    public int salary;
    public Employee()
    {

    }
    // Copy constructor. 
    public Employee(Employee employee)
    {
        firstName = employee.firstName;
        lastName = employee.lastName;
        position = employee.position;
        salary = employee.salary;
    }

}


Private Constructor:- A  private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. The use of private constructor is to serve singleton classes. A singleton class is one which limits the number of objects created to one. Using private constructor we can ensure that no more than one object can be created at a time

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrivateConstructor_Demo
{
    public class Candidate
    {
        private Candidate()
        {

        }
        public static int CandidateVisitedForInterview;
        public static int CountCandidate()
        {
            return ++CandidateVisitedForInterview;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            // The following comment line will throw an error because constructor is inaccessible 
            //Candidate candidate = new Candidate(); 
            Candidate.CandidateVisitedForInterview = 20;
            Candidate.CountCandidate();
            Console.WriteLine("Interviewed candidates: {0}", Candidate.CandidateVisitedForInterview);
            Console.ReadLine();
        }
    }

}

Saturday, January 12, 2019

Difference Between var and dynamic in C#


Introduction:-
In this article I will teach you difference between var and dynamic keyword.

Var Dynamic
Var Introduce in C# 3 Dynamic introduce in C# 4
Var is using early binding  Dynamic is using late binding 
Var must need to initialise at the time of declaration No need to initialise at the time of declaration
Check error at compile time Check error at run time
Var will show all data type property after initialising  Dynamic can't show all data type property after initialising 
Example:
var obj="String";
Example:
dynamic obj = "String";

Var :-

  • var it show's all all property based on data type .
  • once you initialise data you can'n modify .
  • var verify datatype property if it's not exist it's throw a compile time error . 








Dynamic :-

  • dynamic is using late binding that why after data initialisation don't what kind of data is 
  • it's through error in run-time if property not exist in appropriate data type . 



Thank's for reading.



Yield Keyword in C#

Introduction:-
Dear friend in this article I will teach you how to use Yield  C-Sharp .

Use Of Yield:-
The main use of yield in C#

  • Custom iteration without temporary collection 
  • Stateful  iteration 
Custom Iteration :-
If I have list object i want to filter this object without using temp object so go with yield.

Ex: 1 to 5 number in list object .
Now i want to filter it if number is grater then 3


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpLearnig
{
    class Program
    {
        List<int> listObj = null;
        static void Main(string[] args)
        {
            Program obj = new Program();

            // load data in listObj 
            obj.loadData();
            // Custom Iteration 
            foreach (var item in obj.listFilter())
            {
                Console.WriteLine("{0} is grater then 3.",item);
            }
            Console.ReadLine();
        }

        private IEnumerable<int> listFilter()
        {
            foreach (var item in listObj)
            {
                if (item > 3)
                    yield return item; 
            }
        }

        private void loadData()
        {
            listObj = new List<int>() { 1, 2, 3, 4, 5 };
        }
    }
}

Output:
4 is grater then 3.
5 is grater then 3.










Stateful Iteration Exaample:-

I have number list object but I want running total of this object so in this scenario by using yield you can achieve this.
1 -> 1
2 -> 3
3 -> 6
4 -> 10
5 -> 15
like this .

using System;
using System.Collections.Generic;

namespace CSharpLearnig
{
    class Program
    {
        List<int> listObj = null;
        static void Main(string[] args)
        {
            Program obj = new Program();

            // load data in listObj
            obj.loadData();
            // Runing Total by using Statefull Iteration
            foreach (var item in obj.listFilter())
            {
                Console.WriteLine("{0}",item);
            }
            Console.ReadLine();
        }
        private IEnumerable<int> listFilter()
        {
            int sum = 0;
            foreach (var item in listObj)
            {
                sum += item;
                    yield return sum;
            }
        }
        private void loadData()
        {
            listObj = new List<int>() { 1, 2, 3, 4, 5 };
        }
    }
}
Output-











Thank for giving your valuable time .