--
As it is known C# is an object-oriented programming language that provides us the ability to reuse existing code. Classes are fundamental parts of object-oriented programming language. A class is a blueprint for creating an object- a particular data structure that includes both data (often known as attributes or properties) and code (functions or methods) that operate on that data. Objects are instances of classes, and a class defines the common behavior and structure that its objects share. Some important key points regarding classes in C# are given below.
- Classes are reference types that hold the object created dynamically in a heap.
- System.Object is the base type for all the classes.
- If we do not declare the access modifier for a class, then by default it is internal.
- If we do not declare the access modifier for the methods and variables in a class, then by default it is private.
- Directly inside the namespaces, declarations of private classes are not allowed
In his article, we will explain the types of classes in C#.
Types of Classes in C#
There are four types of classes in C#.
- Abstract Class
- Partial Class
- Sealed Class
- Static Class
Abstract Class:
An abstract class is a class that provides the definition for its subclasses. An abstract class can not be instantiated. An abstract class is used to provide a base for child classes to extend and implement abstract methods and override or use the implemented methods in the abstract class. Some key points regarding abstract class are mentioned below.
- An object can not be created from the abstract class.
- We use
abstract
keyword to declare an abstract class. - To use the abstract class, it must be inherited by a subclass.
- An Abstract class contains both abstract and non-abstract methods.
- The methods inside the abstract class can either have an or no implementation.
- We can inherit two abstract classes; in this case, implementation of the base class method is optional.
- An Abstract class has only one subclass.
- Methods inside the abstract class cannot be private.
- If there is at least one method abstract in a class, then the class must be abstract.
Example:
abstract class AbstractClass
{}
Partial Class:
A partial class is used to divide properties, methods, and events into multiple partial classes that are joined in a single class at compile time. A partial class is useful when the class definition is too large or you are working on a complex model or feature and you want to divide the responsibility of implementation into more than one developer. Some key points regarding partial class are mentioned below.
- All the parts of the partial class must be prefixed with the
partial
keyword. - If you
seal
a specific part of a partial class, the entire class issealed
, the same as for anabstract
class. - Inheritance cannot be applied to partial classes.
- The classes written in two class files are combined at run time.
Example:
partial class PartialClass
{}
Sealed Class:
A sealed class is a class that can not be inherited. In C#, sealed classes are used to prevent inheritance from other classes. This helps to prevent security issues. For example, the methods in a sealed class cannot be manipulated from other classes. Some key points regarding sealed classes are mentioned below.
- A Sealed class is created using the
sealed
keyword. - Access modifiers are not applied to a sealed class.
- To access the sealed members, we must create an object of the class.
- When casting objects, the runtime must check the type of the object at runtime. When casting to a sealed type, the runtime must only check the type of the object, so it is faster.
- Sealed classes are best used when you have a class with static members, such as the “
Pens
” And “Brushes
” classes of the System.
Example:
sealed class SealedClass
{}
Static Class:
Static classes are the classes that can not be instantiated, this means you can create an object (instance) of a static type class using the new
keyword. You can directly call static class members using their names. Some key points regarding static class are mentioned below.
- It was created using the
static
keyword. - Only static members are allowed; in other words, everything inside the class must be static.
- We cannot create an object of the static class.
- Static classes are sealed and therefore cannot be inherited. They are also not available for use in the same way as a regular class.
- It allows only a static constructor to be declared.
- The static class methods can be called using the class name without creating the instance.
- Static classes are a good choice when you only need a utility class that contains several utility methods. For example, the methods of the Math class in the System namespace are organized in a static class.
- Static classes can be used to write your own instance wrapper around the file system API.
- Static classes are used to create data and functions that can be accessed without creating an instance of the class.
- Static classes consume fewer resources.
Example:
static class StaticClass
{}