C# Classes


  • A class is a user-defined data type and it is the base of the object oriented programming language.
  • A class is simply a abstract model in order to define new data type.
  • A class encapsulates the variable and the method inside it.

Defining a Class


  • A class is defined using the ‘class’ keyword.
  • Basic syntax of the class is given below:
  • class class-name
    {
    Variable-declaration;
    Method declaration;
    }

In the above syntax access specifier decides the access level of the class in the whole program. The default access specifer for the class is ‘internal’. You can study the access specifier in details in the further section.


Adding variable to Class


  • Data is encapsulated in a class by putting data fields inside the body of a class.
  • These variables are referred as instance variables.
  • Here is basic syntax to declare variables in the class:
class class-name
{
data-type variable1;
data-type variable2;
…………………………………………………………….
…………………………………………………………….
data –type variablen;
}

access specifier of the variable decides its access level and by-default the access specifier for the variable is ‘private’. data-type defines the type of the variable.

Adding Methods to a Class


  • Methods are used to perform operations on the variables. So variables are useless without methods.
  • The syntax for declaring a method is as follows:
class class-name
{
//variable declaration
data-type variable1;
data-type variable2;
…………………………………………………………….
…………………………………………………………….
data –type variablen;
//method declaration
return-type method-name(parameter-list)
{
Method body
}
…….
……..
}

Access specifier of method defines the access rule for the method. By-default access specifier for method is ‘private’. return –type defines the type of value return by the method .


The example given below shows the complete definition of a class:


Example

public class addition
{
int a=2;//by default private
int b=5;//by default private
int c;//by default private
public void add()
{
c=a+b;
………….
}
}

Creating Object of Class


  • Object is instance of class which can access all the members of class.
  • A class is an abstract concept means it is not exist in really but object is a realistic concept which contains all properties of the class. For example if vegetable is a class then potato, leady-finger, bringle etc are its object.
  • The actual instance of class is created by making its objects.
  • All objects of the class have their own copies of variables and method defined in the class.
  • The basic syntax to declare an object of class is as follows:
  • class-name object-name=new class-name();

  • After creating an object of class we can access the members of class through the object.
  • The basic syntax for accessing the member of class through object is as follows:
  • object-name.variable-name;
    object-name.method-name();

Here is an example which is sufficient to understand the concept of class:


Example

using System;
namespace classexample
{
public class addition
{
//variable declaration
public int a = 2;
public int b = 5;
int c;//by default private; cannot access directly through object
//method declaration
public void add(int a, int b)
{
c = a + b;
Console.WriteLine("Inside the add method:");
Console.WriteLine("Result of addition of a={0} and b={1} is:{2}",a,b,c);
}
}
class program
{
static void Main()
{
addition obj = new addition();//creating object of class
int a, b;
//access the varibles of class through object
a = obj.a;
b = obj.b;
Console.WriteLine("Inside the main method:");
Console.WriteLine("values of variables of class\na={0},b={1}", a, b);
//access the method of class through object
obj.add(5, 7);
Console.ReadLine();
}
}
}

The output of the above program is as follows:


Output

Inside the main method:
values of variable of class
a=2, b=5
Inside the add method
Result of addition of a=5 and b=7is:12

C# Constructors


  • A constructor is a special method of class that is used to initialize the data members or variables of class.
  • It is executed when the instance or object of the class is created in which it is declared.
  • It has the name as that of class in which it is declared.
  • It has no return type and a class can have multiple constructors.
  • It is always public.

Types of constructor:


In C# constructor can be of two types as follows:


  • Non-parameterized constructor:It is also called default constructor. It does not contain any parameter.
  • Parameterized constructor:Parameterized constructors are the constructors which contains parameters.

Here is an example which shows the concept of constructors:


Example

using System;
namespace classexample
{
public class addition
{
//variable declaration
public int a;
public int b;
int c;//by default private ;cannot be access directly through object
public addition()//non paramertized constructor
{
a = 2;
b = 5;
}
public addition(int x, int y)//parametrized constructor
{
a = x;
b = y;
}
//method declaration
public void add()
{
c = a + b;
Console.WriteLine("Inside the add method:");
Console.WriteLine("Result of addition of a={0} and b={1} is:{2}",a,b,c);
}
}
class program
{
static void Main()
{
addition obj1 = new addition();//calling non-parameterized constructor
addition obj2 = new addition(5, 7);//calling parameterized constructor
Console.WriteLine("Inside the main method:");
Console.WriteLine("For First Object");
obj1.add();
Console.WriteLine("For Second Object");
obj2.add();
Console.ReadLine();
}
}
}

The output of the program will be as follows:


Output

Inside the main method:
For First Object
Inside the add method:
Result of addition of a=2 and b=5 is:7
For Second Object
Inside the add method:
Result of addition of a=5 and =7 is:12

C# Destructors


  • Destructors are just opposite to the constructor.
  • These are executed when an object is about to be destroyed (destroyed by the garbage collector) at the ending of the scope of class.
  • It has the name as that of class in which it is declared but prefixes it with the (~) tilde sign.
  • It has no return type.
  • It can not contain any parameter.
  • It cannot be overloaded and inherited.
  • A class can only have one destructor.

Here is an example of destructor as follows:


Example

using System;
namespace LineApplication
{
class destructing
{
public destructing()
{
Console.WriteLine("Object is created");
}
~destructing()
{
Console.WriteLine("Object is destroyed");
Console.ReadLine();
}
public void method1()
{
Console.WriteLine("Inside method1");
}
static void Main()
{
destructing obj = new destructing();//calling constructor;
Console.WriteLine("Inside main function");
obj.method1();
}
}
}

The output of the above program will be as follows:


Output

Object is created
Inside main function
Inside method1
Object is destroyed

Static Members of Class


  • Static members of class are of two types: static variables and static methods.
  • Static members are accessed using the class name in which they are declared.
  • Static members are associated with the class itself instead of the object of class.
  • This is a syntax to declare the static variable:
  • static data-type variable-name;

  • This is syntax to declare static member:
  • static access-specifier return-type method-name;

  • It should be remember that static member can contain only static variables in its body and exists before creating the object of class.

Here is an example of static members of class:


Example

using System;
namespace staticexample
{
class statictesting
{
//static variable declaration
static public int a;
static public int b;
static public int c;
//static method declaration
static public void add()
{
c = a + b;
Console.WriteLine("Result of addition of a={0} and b={1} is c={2}", a, b, c);
}
}
class staticmembers
{
static void Main()
{
//accessing the static variables
statictesting.a = 5;
statictesting.b = 6;
//accessing the static method
statictesting.add();
Console.ReadLine();
}
}
}

The output of the above program is as follows:


Output

Result of addition of a=5 and b=6 is c=11