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

}

No comments:

Post a Comment