blog

Home / DeveloperSection / Blogs / Concept of Class in C#

Concept of Class in C#

Jonas Stuart 1790 17-Sep-2016

This is the place where the designing or structure or blueprint of the program with variable is store.  C# provides the platform to create variable of different types , methods and events. It can be used to declare variable, function, event etc along with datatype and access specifier.

How to Design Class ?

For designing of class we need to use Class keyword.

< Access specifier > Class Demo {

                              }

What is an object for the class?

We can define object of the class , same as variable is created for the datatype. 

Syntax   // classtest a = new classtest(); 

Why to create object of the class?

By creating an object for the class, a memory is allocated according to the length of variables or .We can create many object of the class.

But we make Static class by using Static Keyword before class. Then we can’t create object for that class because by using static keyword instances for the memory is already allocated in the memory. It can’t be inherited.

Example :

Static Class demo
{
Static Void fun1()
{
Console.WriteLine(“New Version”);
}
}
Class program
{
Demo D1 = new demo(); // error  will come.
}
We don’t need to create any object . directly use the name of the class for any function or variable.
For example
 Class program
{
Demo.fun1()
}
Main(string[] args)

Points to be remember: All the member of Static class must be Static.

Default Access Specifier for the class is Internal and for the class member default  is Private.

If you are beginner then you must thinking that " Shall we passs parameter while initiating the object".

Answer of this question is "Yes".

Question may arrise how ?

For this answer you must have knowledge about Constructor and Destructors.

For the object there is a default constructor used to initialize memory. We can create constructor also it should have the same name as of class.

syntax :      

   class Line
                    {
 
                   public int Loan;   // Length of a line
                   public Line(int a) // constructor
                   {
                   Console.WriteLine("Object is being created");
                   Loan +=a;
                   }

for the above class we need to pass the parameter while initalising the object for the class. Such contructor is called parameterized constructors. 

Destructors is just reverse of Contructor. In simple word it is used for releasing the memory after the cexecution of the class. we can create Destructor also it

should have the same name as of class with prefix of special charactor "~".

Syntax

      ~Line() //destructor
      {
         Console.WriteLine("Object is being deleted");
      }

 

When we don't specify, assess specifier for the Class then by default it is "Public" and default access specifier for its member is "Private".   

When we assigned any member as Static and if we can to create many object for the class then also only one memory is allotted for that member 

For example : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class StaticV
    {
        public static int num = 0;
        public void count()
        {
            num++;
        }
        public void print()
        {
            Console.WriteLine("Variable num : {0}", num);
        }
    }
    class StaticTester
    {
        static void Main(string[] args)
        {
            StaticV s1 = new StaticV();
            StaticV s2 = new StaticV();
            s1.count();
            s2.count();
            s1.print();
            s2.print();
            Console.ReadKey();
        }
    }
}

 

This blog is to provide some basic knowledge about the class and it is also helpful to provide clear answer for the doubts related to c#.


Updated 16-Mar-2018

Leave Comment

Comments

Liked By