Classes in C#

                Class is a collection of  related variables and methods. It represents the state and behavior of an object.
A class definition can contain
                1.Fields
                2.Properties(accessors,mutators)
                3.Methods
                4.Constructor

Properties are special public methods that allow a user of the class to " get " a variables value, or to a " set " a value

(e.g)
using System;

public class Program
{
public static void Main()
{
EmployeeDetails employeeDetails = new EmployeeDetails();
employeeDetails.EmpAge=25;
employeeDetails.EmpName="ssn";
employeeDetails.ShowDetails();
}
}

class EmployeeDetails
{
//Fields
private string empName;
private int empAge;

//Properties
public string EmpName
{
get{ return empName;} //accessor
set {empName= value;} //mutator
}

public int EmpAge
{
get{return empAge;}  //accessor
set {empAge=value;}  //mutator
}

//Methods
public void ShowDetails()
{
Console.WriteLine(empName);
Console.WriteLine(empAge);
}

}


 Class is limited the access by using access modifiers.However some classes don't require any access modifiers. There are 5 types of access modifiers in c#,namely
  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected internal
The default access modifier of class is Internal  and the default access modifier of the variable is private and method is private.  Directly declaring the private class inside the namespace is not allowed.

Types of Classes 
                              There are 4 types of classes available in c#.They are
  1. Abstract class
  2. Partial class
  3. Sealed class
  4. Static class
1.Abstract Class
       
  • Abstract class is a half-defined parent class . The full implementation is defined in the child classes.
  • Abstract classes are declared using the abstract keyword.
  • We cannot create an object of an abstract class.
  •  Abstract class must be inherited in a subclass to use.
  • An Abstract class contains both abstract and non-abstract methods.
  • The methods inside the abstract class can either have an implementation or no implementation.
  • We can inherit two abstract classes.In this case the base class method implementation is optional. 
  • An Abstract class has only one subclass.
  • Methods inside the abstract class cannot be private.
  • If there is at least one method is abstract in a class then the class must be abstract.
      (e.g)
Abstract class Employee
{

}

2.Partial Class
                         It is a type of class that allows dividing their properties, methods and events into multiple source files and at compile time these files are combined into a single class.

  • All the parts of the partial class must be prefixed with the partial keyword.
  • If you seal a specific part of a partial class then the entire class is sealed, the same as for an abstract class.
  • Inheritance cannot be applied on partial classes.
  • The classes that are written in two class files are combined together at run time.
      (e.g)
Partial class Employee
{

}

3.Sealed Class
                   A Sealed class is a class  that cannot be inherited and used to restrict the properties. 
  • A Sealed class is created using the sealed keyword.
  • Access modifiers are not applied to a sealed class.
  • To access the sealed members we must create an object of the class.
      (e.g)
Sealed class Employee
{

}

4.Static Class
                          we cannot create an object of that class using the new keyword, such that class members can be called directly using their class name.
  • Created using the static keyword.
  • Inside a static class only static members are allowed, in other words everything inside the static class must be static.
  • We cannot create an object of the static class.
  • A Static class cannot be inherited.
  • It allows only a static constructor to be declared.
  • The methods of the static class can be called using the class name without creating the instance.
    (e.g)
Static class Employee
{

}

Comments

Popular posts from this blog

Types of Architects

Basic measurements

Search html table contents based on its td using Jquery