What is Class in C#
Class is a user define data type, a class defines an object with the help of variables and methods. 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 .
- 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.
Object
C# object of class is a runtime entity. A class properties can access using the object of that class. To create an object class we use "new" keyword.
Through below statement, we can create an object of Program class.
Program nameOfObject = new Program();
Here name of object can be any valid keyword such as program, prog or prm a valid keyword.
Once an object created for any class it allocate memory of that class. An allocate memory of all existing variables declare or used into that class also.
In C# mainly it has four type of classes available
Partial Class
Partial classes are those class which can use in multiple files with the same name. Compile code of C# Partial class always generate single file. Main benefits of partial class, on same partial class multiple developers can work parallel with multiple files in the same partial class during big project. Partial class define by class name followed by Partial keyword. As shown in below lines of code.
partial class Program
{
static void Main(string[] args)
{
Console.Write("This is 1st C# Program");
}
}
In the above line of code partial keyword used before class.
Abstract Class
An abstract class in C# does not contains full definition so in that case it must inherit into its child class. We can't directly create an object of an abstract class reason of not able to create an object of abstract class that an abstract class is incomplete class by it self so an incomplete line of code will give an error.
Remember :: An abstract class must inherit into it child class.
An abstract example shown in below line of code. Here Father is an abstract class. It contains two method FatherDetails() and Details(). FatherDetails() method does have implementation into the same class, so it required to define it with abstract class otherwise it will give an error.
public abstract class Father {
public abstract void FatherDetails();
public void Details()
{
}
}
partial class Program : Father
{
Father father = new Father(); // Can't create an object of abstract class
public override void FatherDetails()
{
}
static void Main(string[] args)
{
Console.Write("This is 1st C# Program");
}
}
In the above line of code Father is abstract class and it is inherited into child class Program. The implementation of abstract method FatherDetails() is mandatory into its child class i.e. Program (child class).
Father father = new Father();
line of code will give an error as we know that we can not create the object of Abstract class.
Remember ::
- An abstract class must inherit into its child class.
- Object can't create for an abstract class.
- An abstract class defined with abstract keyword.
- Abstract class always contains an abstract method which mandatory to implement into child class.
Static Class
An static class can not have instance. We can directly access using name of the class. It allows to create static member inside that class only. We can not inherit static class. We use static key word to create an static class as shown in the below example: -
public static class Program
{
static string value;
public static int Sum(int i, int j)
{
return (i + j);
}
}
In the above example Program is an static class. It contains a string type static variable "value" and one static method i.e. Sum(int i, int j) which can access out side the class using ClassName.MethodName() .
Some of the main feature of static class are: -
- We can't create an instance of an static class.
- An static class create using static keyword.
- Only static member or member function may exist in a static class.
- An static class can't be inherited.
- In side static class only static constructor allowed.
Sealed Class
No inheritance allowed from a sealed class. It use to restrict the inheritance in C#. Sealed access modifier use to declare a class as sealed. To access the members of sealed class it need to create the instance of that class.
Here is an example of sealed class
public sealed class Program
{
int number;
static string value;
public int Sum(int i, int j)
{
return (i + j);
}
}
Some of the key points for a sealed class are
- Access modifier not allowed over the sealed class.
- Sealed class defined with sealed keyword.
- Sealed class restrict inheritance in C#.
- We must create the instance of sealed class to access their properties or methods.