What is Class in C#, Definition of class in C#

 What is Class in C#


Class is a real time entity in terms of Programming language. In C# we use to define a class with Class keyword. A class can contains data types, properties, methods, events and their definitions. Here is an example of C# class and used terms.

public class Program
{
    public string name = string.Empty;
    private int number;
    public int PropertyName{ get; set; }
    public Program()
         {
    }
    public void PrintName(string name)
    {
        Console.WriteLine("My name is {0}", name);
    }
}

  • In the above line of code "Program" is the name of the class followed by class code. There are two data type declare 1st one is name as string data type Which can contain only string type data. Another one is "number" which can contain only integer value. In the next step property has been defined of integer type named as "PropertyName". 

  • A class can contain constructor also which name will be same as Class name as shown in the below line of code . 

        public Program() { } 

  • Here Program() is constructor which name is as class name. The difference is that it defines as method. The main difference between and method is that a method can return a value but a constructor can't. 

  •  In the last we defined method inside the class. Here "PrintName(string name)" is a method.

No comments:

Post a Comment

If you have any doubt please let me know..