Object Oriented Programming (OOP) is a very important part of any language.

Now, here is some information about OOP.

CLASS

Example

  1. namespace OPPS
  2. {
  3. class OppsDemo
  4. {
  5. static void Main(string[] args)
  6. {
  7. // declare variable, method
  8. }
  9. }
  10. }
Object

Objects are used in classes. In this declare a variable and it refers to creating an object or creating an instances of the class.

A variable is declared with the var keyword.

Here is one example:
  1. class OppsDemo
  2. {
  3. static void Main(string[] args)
  4. {
  5. Demo d = new Demo();
  6. }
  7. }
Note: The created class is available in any current project.

Also create a null object inside a class.
  1. Demo d = null;
In this, see the new keyword is for garbage collection. In other words, when we declare using the new keyword you create space for the new variable. Garbage Collection will automatically clean up the value in the .NET Framework.

Now we declare a variable in the Employee class as in the following:
  1. class Employee
  2. {
  3. static void Main(string[] args)
  4. {
  5. string Name;
  6. int Age;
  7. }
  8. }
Here for one class example as in the following:

Class Example

Output

Class Example output

Method

A method is one type code block with bracket { }. A method name display is unique because it's use in programmed. A program causes the statements to be executed by calling the method and specifying any required method arguments. A program causes the statements to be executed by calling the method and specifying any required method arguments. Also Pass arguments with method.

Method name declare in one word and first letter be Uppercase because method is one type action. It's a case sensitive.
  1. void Display()
  2. {
  3. }
Also in this, Access modifier concept use and make it public, private or internal.

Now we are see one calling method example,
  1. namespace OPPS
  2. {
  3. public class Addition
  4. {
  5. public int count(int a, int b)
  6. {
  7. int c;
  8. c = a + b;
  9. return c;
  10. }
  11. static void Main(string[] args)
  12. {
  13. int a = 10;
  14. int b = 5;
  15. int Result;
  16. Addition add = new Addition();
  17. //call method
  18. Result = add.count(a, b);
  19. Console.WriteLine("Addition Count:{0}",Result);
  20. Console.ReadLine();
  21. }
  22. }
  23. }
Method

Output:

method output

Now getting some information about Method type,

  1. Pass by Value
  2. Pass by References

1. Pass by Value

Now work with pass by value in method. This article provide basic example how to pass value with method.

In this method any value pass as parameter. Here given example:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class PersonInfo
  8. {
  9. private void Info (string Name, string city)
  10. {
  11. Console.WriteLine("Person Information:");
  12. Console.WriteLine("------------------------------------");
  13. Console.WriteLine("Person Name:");
  14. Console.WriteLine(Name);
  15. Console.WriteLine("------------------------------------");
  16. Console.WriteLine("Person City:");
  17. Console.WriteLine(city);
  18. }
  19. static void Main(string[] args)
  20. {
  21. PersonInfo P_Info = new PersonInfo();
  22. string Name = "Rakesh Chavda";
  23. string city = "Ahmedabad";
  24. P_Info.Info(Name, city);
  25. Console.ReadLine();
  26. }
  27. }
  28. }
Pass by Value

Output:

Pass by Value Output


In this example see two type static method. One is entry point in application and other method is Info, it's pass two parameter name and city and display.

Passing value. in this method value store and after display.

2. Pass by References

In this method passing any variable pass by reference.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class Info
  8. {
  9. public string Name;
  10. public string city;
  11. }
  12. public class PersonInfo
  13. {
  14. static void Main(string[] args)
  15. {
  16. Info i = new Info();
  17. i.Name = "Rakesh";
  18. i.city = "Ahmedabad";
  19. Console.WriteLine("Person Name: {0}",i.Name);
  20. Console.WriteLine("Person City: {0}",i.city);
  21. Console.ReadLine();
  22. }
  23. }
  24. }
Pass by References

Output:

Pass by References output


Here see class info string value declare as public. In side method declare object info and assign the both string value.

In this also reference value assign with ref keyword. Here give example,
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class PersonInfo
  8. {
  9. static void PersonAge1(ref int age) // 1st Person
  10. {
  11. age = 25;
  12. }
  13. static void PersonAge2(ref int age) // 2nd Person
  14. {
  15. age = 28;
  16. }
  17. static void Main(string[] args)
  18. {
  19. int age = 0;
  20. PersonAge1(ref age);
  21. Console.WriteLine("1st Person Age : {0} ",age);
  22. PersonAge2(ref age);
  23. Console.WriteLine("2st Person Age : {0} ",age);
  24. Console.ReadLine();
  25. }
  26. }
  27. }
program

Output

program output

Pass by Out reference

In this also the reference value is assigned with the out keyword. Here, see in this example, the difference between ref and out.

Both check on the programmed compile time with the ref and out keyword values. In this the out reference is necessary given any type set output in a program. And with the ref assigning an output value or not it passes a parameter.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class PersonInfo
  8. {
  9. static void PersonAge1(ref int age) // 1st Person with ref
  10. {
  11. }
  12. static void PersonAge2(out int age) // 2nd Person with out
  13. {
  14. age = 28;
  15. }
  16. static void Main(string[] args)
  17. {
  18. int age = 0;
  19. PersonAge1(ref age);
  20. Console.WriteLine("1st Person Age : {0} ",age);
  21. PersonAge2(out age);
  22. Console.WriteLine("2nd Person Age : {0} ",age);
  23. Console.ReadLine();
  24. }
  25. }
  26. }
Pass by Out reference

Output

Pass by Out reference output

It does not declare a value ref type so it is given a 0 value for the output.

But if it does not declare a value out type it is given an error at compile time.

That's in both the main and is different at the compiler level.

Encapsulation

Encapsulation is a type access modifier. Encapsulation is for accessing and the visibility of a class member. Here is the csharp supported access specifiers list.
  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected Internal

Encapsulation is the main concept of data hiding or information hiding with the above list access modifier keywords. So, encapsulation is also known as data hiding. An access modifier keyword is set to the accessibility of the class, method, member funcation or member variable.

Now, we are getting some information about the access keyword one by one.

Public

The public access modifier keyword is a class variable or function that can be accessed by any other object or function. That us, any public member van be used from outside the class.

Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class PublicDemo
  8. {
  9. class Maths
  10. {
  11. // declare variable
  12. public int a;
  13. public int b;
  14. public int GetValue()
  15. {
  16. return a + b;
  17. }
  18. public void ShowResult()
  19. {
  20. Console.WriteLine("Value of A: {0}",a);
  21. Console.WriteLine("Value of B: {0}", b);
  22. Console.WriteLine("Result = {0}",GetValue());
  23. Console.ReadLine();
  24. }
  25. }
  26. static void Main(string[] args)
  27. {
  28. Maths m = new Maths();
  29. m.a = 5;
  30. m.b = 2;
  31. m.ShowResult();
  32. Console.ReadLine();
  33. }
  34. }
  35. }
public Encapsulation

Output

public Encapsulation output

Here see, the a and b variables are declared as public and it's accessed with the Maths class as m. Also created the function GetValue() and ShowResult() access variables.

Private

In this access modifier a class variable and a function can be hidden from any other object and function. It is only accessed from the same class, not another; it's private.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class PublicDemo
  8. {
  9. class Maths
  10. {
  11. // declare variable
  12. private int a;
  13. private int b;
  14. public void InsertValue()
  15. {
  16. Console.WriteLine("Enter value of A:");
  17. a = Convert.ToInt32(Console.ReadLine());
  18. Console.WriteLine("Enter value of B:");
  19. b = Convert.ToInt32(Console.ReadLine());
  20. }
  21. public int GetValue()
  22. {
  23. return a + b;
  24. }
  25. public void ShowResult()
  26. {
  27. Console.WriteLine("=============================");
  28. Console.WriteLine("Value of A: {0}",a);
  29. Console.WriteLine("Value of B: {0}", b);
  30. Console.WriteLine("Result = {0}",GetValue());
  31. }
  32. }
  33. static void Main(string[] args)
  34. {
  35. Maths m = new Maths();
  36. m.InsertValue();
  37. m.ShowResult();
  38. Console.ReadLine();
  39. }
  40. }
  41. }
Output

Private


In this example a and b are declared as private so here the InsertValue() and GetValue() functions are created to access it.

Protected

The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions, except child classes.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class MathsDemo
  8. {
  9. class Maths1
  10. {
  11. public int R;
  12. protected int J;
  13. }
  14. class Maths2 : Maths1
  15. {
  16. public int AddValue()
  17. {
  18. R = 5;
  19. J = 29;
  20. return R + J;
  21. }
  22. }
  23. static void Main(string[] args)
  24. {
  25. Maths2 m2 = new Maths2();
  26. m2.R = 5;
  27. m2.J = 29; //--------------------- Error -------------
  28. Console.WriteLine(m2.AddValue());
  29. Console.ReadLine();
  30. }
  31. }
  32. }
In the preceding example, see it does not call the object of the class Maths1, but inherits the class Maths1 to Maths2. Here given the error line because J is declared as protected, so the protected member cannot access outside the child class but only access inside the child class.

Internal

The internal access specifier allows a class to expose its member variables and member functions to other functions and objects. It can be accessed from any class or method defined with the application in which the member is defined. The default access specifier is internal for a class.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class InternalDemo
  8. {
  9. class Result
  10. {
  11. internal int a;
  12. internal int b;
  13. int Addition()
  14. {
  15. return a + b;
  16. }
  17. public void ShowResult()
  18. {
  19. Console.WriteLine("Value of A:{0}",a);
  20. Console.WriteLine("Value of B:{0}", b);
  21. Console.WriteLine("Addition: A + B = {0}",Addition());
  22. }
  23. }
  24. static void Main(string[] args)
  25. {
  26. Result R = new Result();
  27. R.a = 3;
  28. R.b = 4;
  29. R.ShowResult();
  30. Console.ReadLine();
  31. }
  32. }
  33. }
Internal

Output:

Internal output


In this example see Addition() is not declared with any access modifier.

Protected internal

The protected internal access modifier allows a method or member variable to be accessible to a class and it's derived classes inside the same assembly or namespace within a file. The member cannot be accessed from a class in another assembly or a file.

In other words, the protected internal access specifier allows its members to be accessed in a derived class, containing a class or classes within the same application.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class Protected_Internal
  8. {
  9. class DisplayWord
  10. {
  11. protected internal string word;
  12. public void ShowName()
  13. {
  14. Console.WriteLine("\n You are Enter Word :" + word);
  15. }
  16. }
  17. static void Main(string[] args)
  18. {
  19. DisplayWord ND = new DisplayWord();
  20. Console.WriteLine("Enter Some Word :");
  21. ND.word = Console.ReadLine();
  22. ND.ShowName();
  23. Console.ReadLine();
  24. }
  25. }
  26. }
Protected internal

Output

Protected internal output

Polymorphism

Polymorphism is different behavior in a different situation.

Polymorphism can be divided into the two parts, compile-time Polymorphism and run-time Polymorphism. It is known as static and dynamic Polymorphism.

In other words, any addition operation between two numeric values it's given a return numeric and after this addition operation between two string value it's given a string value. It is called simply Polymorphism.

Polymorphism is static or dynamic type and it's decided at run time and compile time. Polymorphism is used in method overloading and operator overloading.

Function Overloading (Compile time polymorphism)

Function overloading means, in simple words, the same (one) function but works in different situations.

The following is one example of function overloading.

Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class Polymorphism
  8. {
  9. void DataDisplay(int n)
  10. {
  11. Console.WriteLine("Display numaric value:{0} ", n);
  12. }
  13. void DataDisplay(string str)
  14. {
  15. Console.WriteLine("Display String Value:{0} ", str);
  16. }
  17. static void Main(string[] args)
  18. {
  19. Polymorphism P = new Polymorphism();
  20. P.DataDisplay(404);
  21. P.DataDisplay("CSharp");
  22. Console.ReadKey();
  23. }
  24. }
  25. }
Function Overloading

Output

Function Overloading output

Here in this example see DataDisplay() provides different output with different arguments.

But in this same DataDisplay() function.

Operator Overloading Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class RectAngal
  8. {
  9. private int height;
  10. private int width;
  11. public void Rectheight(int h)
  12. {
  13. height = h;
  14. }
  15. public void Rectwidth(int w)
  16. {
  17. width = w;
  18. }
  19. public int RectArea()
  20. {
  21. return height * width;
  22. }
  23. public static RectAngal operator +(RectAngal rH, RectAngal rW)
  24. {
  25. RectAngal Rect = new RectAngal();
  26. Rect.height = rH.height + rH.height;
  27. Rect.width = rH.width + rW.width;
  28. return Rect;
  29. }
  30. }
  31. public class OperatorOverload
  32. {
  33. static void Main(string[] args)
  34. {
  35. int Area1, Area2;
  36. RectAngal R_Angal_1_Area = new RectAngal();
  37. RectAngal R_Angal_2_Area = new RectAngal();
  38. R_Angal_1_Area.Rectheight(5);
  39. R_Angal_1_Area.Rectwidth(4);
  40. R_Angal_2_Area.Rectheight(3);
  41. R_Angal_2_Area.Rectwidth(4);
  42. Area1 = R_Angal_1_Area.RectArea();
  43. Area2 = R_Angal_2_Area.RectArea();
  44. Console.WriteLine("Rectangal 1 Area :{0} ", Area1);
  45. Console.WriteLine("Rectangal 2 Area :{0} ", Area2);
  46. Console.ReadLine();
  47. }
  48. }
  49. }
Output

Operator Overloading


Dynamic Polymorphism

Dynamic polymorphism is run time Polymorphism. It is also known as method overriding. Method overriding means having two or more methods with the same name and the same signature but with different implementations. In this method override so it is also called method overriding.

Method override uses the inheritance principle using the virtual and override keywords.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class Person
  8. {
  9. public virtual void display()
  10. {
  11. Console.WriteLine(" Person ");
  12. }
  13. }
  14. class Student : Person
  15. {
  16. public override void display()
  17. {
  18. Console.WriteLine(" Student ");
  19. }
  20. }
  21. class Program
  22. {
  23. static void Main(string[] args)
  24. {
  25. Person P ;
  26. P= new Person();
  27. P.display();
  28. P = new Student();
  29. P.display();
  30. Console.ReadLine();
  31. }
  32. }
  33. }
Dynamic Polymorphism

Output:

Dynamic Polymorphism Output

Abstract Class and Abstract Method

Abstract Class:

The abstract keyword can be used for class and method.

An abstract class cannot be instantiated as an object and is only provided for the purpose of deriving subclasses.Abstract class have some restriction,it's cannot be constructed. Abstract method have no body.

Declare Abstract Class:
  1. public abstract class className
  2. {
  3. }
When a class is declared as abstract class, then it is not possible to create an instance for that class. But it can be used as a parameter in a method.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public abstract class BaseClass
  8. {
  9. public abstract string GetAbstractData();
  10. public virtual string GetVirtualData()
  11. {
  12. return "Base Virtual Data";
  13. }
  14. public string GetData()
  15. {
  16. return "Base Get Data";
  17. }
  18. }
  19. public class DerivedClass : BaseClass
  20. {
  21. public override string GetAbstractData()
  22. {
  23. return "Derived Abstract Data";
  24. }
  25. public override string GetVirtualData()
  26. {
  27. return "Derived Virtual Data";
  28. }
  29. public string GetData()
  30. {
  31. return "Derived Get Data";
  32. }
  33. }
  34. class Program
  35. {
  36. static void Main(string[] args)
  37. {
  38. BaseClass ABS = new DerivedClass();
  39. Console.WriteLine(ABS.GetAbstractData());
  40. Console.WriteLine(ABS.GetVirtualData());
  41. Console.WriteLine(ABS.GetData());
  42. DerivedClass DRD = new DerivedClass();
  43. Console.WriteLine(DRD.GetAbstractData());
  44. Console.WriteLine(DRD.GetVirtualData());
  45. Console.WriteLine(DRD.GetData());
  46. Console.ReadLine();
  47. }
  48. }
  49. }
Output

Abstract Class

Dynamic Polymorphism with Abstract Class and virtual method:

Some keyword is used for inheritance in Object Oriented Programming language.

Abstract and virtual use in csharp. And also override any method using this.

Here given one example for Abstract class and virtual method.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public abstract class BaseClass
  8. {
  9. public abstract string GetAbstractData();
  10. public virtual string GetVirtualData()
  11. {
  12. return "Base Virtual Data";
  13. }
  14. public string GetData()
  15. {
  16. return "Base Get Data";
  17. }
  18. }
  19. public class DerivedClass : BaseClass
  20. {
  21. public override string GetAbstractData()
  22. {
  23. return "Derived Abstract Data";
  24. }
  25. public override string GetVirtualData()
  26. {
  27. return "Derived Virtual Data";
  28. }
  29. public string GetData()
  30. {
  31. return "Derived Get Data";
  32. }
  33. }
  34. class Program
  35. {
  36. static void Main(string[] args)
  37. {
  38. BaseClass ABS = new DerivedClass();
  39. Console.WriteLine(ABS.GetAbstractData());
  40. Console.WriteLine(ABS.GetVirtualData());
  41. Console.WriteLine(ABS.GetData());
  42. DerivedClass DRD = new DerivedClass();
  43. Console.WriteLine(DRD.GetAbstractData());
  44. Console.WriteLine(DRD.GetVirtualData());
  45. Console.WriteLine(DRD.GetData());
  46. Console.ReadLine();
  47. }
Output: 1

Output 1

Now, see one more outputs with this example but remove.
  1. GetVirtualData()
  2. GetData()
Method from Deriverd Class and see the output, what's happenning.

Output: 2

Output 2

Sealed Class

A Sealed Class means, in normal words, your class is sealed or restricted from being inherited by any other class. A Sealed class is created with the sealed keyword with the class name.

A Sealed class cannot be derived from.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class Program
  8. {
  9. public abstract class Triangle
  10. {
  11. private double W;
  12. private double H;
  13. public Triangle(double length = 0D, double height = 0D)
  14. {
  15. W = length;
  16. H = height;
  17. }
  18. public virtual double Area()
  19. {
  20. return W * H / 2;
  21. }
  22. public void Display(string D = "")
  23. {
  24. Console.WriteLine("Triangle {0}", D);
  25. Console.WriteLine("--------------------------");
  26. Console.WriteLine("Triangle Width: {0}", W);
  27. Console.WriteLine("Triangle Height: {0}", H);
  28. Console.WriteLine("Triangle Area: {0}", Area());
  29. }
  30. }
  31. sealed public class SealedDemo : Triangle
  32. {
  33. public SealedDemo(double Width, double Height): base(Width, Height)
  34. {
  35. }
  36. }
  37. static void Main(string[] args)
  38. {
  39. SealedDemo SD = new SealedDemo(5.3, 8.5);
  40. SD.Display(" OUTPUT");
  41. Console.ReadLine();
  42. }
  43. }
  44. }
Output

Sealed Class

Inheritance

Inheritance is one of the most important primary concepts of OOP. Inheritance provides existing code reusability in a programming language.

Implement Base class and Derived Class. And Initialize Base Class from Derived Class. C# supports single class inheritance only.

Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class RectangleValue //Base Class
  8. {
  9. protected int height, width;
  10. public void Rect_Hight(int h)
  11. {
  12. height = h;
  13. }
  14. public void Rect_Width(int w)
  15. {
  16. width = w;
  17. }
  18. }
  19. class Rectangle : RectangleValue // RectangleValue Resue
  20. {
  21. public int RectangleArea()
  22. {
  23. return (height * width);
  24. }
  25. }
  26. class Inheritance
  27. {
  28. static void Main(string[] args)
  29. {
  30. Rectangle R = new Rectangle();
  31. R.Rect_Hight(5);
  32. R.Rect_Width(8);
  33. Console.WriteLine("RectAngle Area:{0}", R.RectangleArea());
  34. Console.ReadLine();
  35. }
  36. }
  37. }
Output

Inheritance


In this see the preceding example RectangleValue class create one time and use in other class for finding Rectangle Area. C# is not support multiple inheritance.

So in C# provide multi inheritance using interface.

Interface

Interface like one type of class but not implement, but only declare with any method or properties. Program easily maintain with interface. Here give interface inheritance example.using with the preceding example.

Example
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class RectangleValue //Base Class
  8. {
  9. protected int height, width;
  10. public void Rect_Hight(int h)
  11. {
  12. height = h;
  13. }
  14. public void Rect_Width(int w)
  15. {
  16. width = w;
  17. }
  18. }
  19. public interface TriAngle
  20. {
  21. int TriAngleArea(int area);
  22. }
  23. class Rectangle : RectangleValue, TriAngle // using interface class
  24. {
  25. public int RectangleArea()
  26. {
  27. return (height * width);
  28. }
  29. public int TriAngleArea(int area)
  30. {
  31. return area/2;
  32. }
  33. }
  34. class Inheritance
  35. {
  36. static void Main(string[] args)
  37. {
  38. int area;
  39. Rectangle R = new Rectangle();
  40. R.Rect_Hight(5);
  41. R.Rect_Width(8);
  42. area = R.RectangleArea();
  43. Console.WriteLine("RectAngle Area:{0}", R.RectangleArea());
  44. Console.WriteLine("TriAngle Area:{0}", R.TriAngleArea(area));
  45. Console.ReadLine();
  46. }
  47. }
  48. }
Output

Interface

Constructor And Destructor

Constructor

A constructor is one method of initializing a class.

A constructor has no return type. A constructor is called automatically by the object of the class and it initializes the class member.

Private Constructor

A private constructor cannot be inherited. And a private constructor class is accessed with a private access modifier. It is not possible to directly create the class object.

Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. class Private
  8. {
  9. public class PageHit
  10. {
  11. private PageHit()
  12. {
  13. }
  14. public static int currentCount;
  15. public static int IncrementCount()
  16. {
  17. return currentCount++;
  18. }
  19. }
  20. static void Main(string[] args)
  21. {
  22. PageHit.currentCount = 20;
  23. PageHit.IncrementCount();
  24. Console.WriteLine("Number of time Page Hit: {0}", PageHit.currentCount);
  25. Console.ReadLine();
  26. }
  27. }
  28. }
Output

Private Constructor


In this example see PageHit.currentCount = 20 and when you run program +1 count and given output 21.

Destructor

A destructor is used to destroy a class. Any one class has only one destructor.

A destructor is a method in the class with the same name as the class preceded with the ~ symbol.

Command:

Example

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace OPPS
  6. {
  7. public class DemoClass
  8. {
  9. public DemoClass()
  10. {
  11. Console.WriteLine("Create Class");
  12. }
  13. ~DemoClass()
  14. {
  15. Console.WriteLine("Class Destroyed");
  16. }
  17. }
  18. class Destructor
  19. {
  20. public static void DC_Create()
  21. {
  22. DemoClass DC = new DemoClass();
  23. }
  24. static void Main(string[] args)
  25. {
  26. DC_Create();
  27. GC.Collect();
  28. Console.ReadLine();
  29. }
  30. }
  31. }

Output