If we want to create complex custom types rather than predefined data types, then we will go for the classes. Class contains two members
- Fields – represent the data
- Methods – represent the behavior
Characteristic | Description |
---|---|
Sealed | Specifies that another class cannot be derived from this type. |
Implements | Indicates that the class uses one or more interfaces by providing implementations of interface members. |
Abstract | Indicates that the class cannot be instantiated. To use it, you must derive another class from it. |
Inherits | Indicates that instances of the class can be used anywhere the base class is specified. A derived class that inherits from a base class can use the implementation of any public members provided by the base class, or the derived class can override the implementation of the public members with its own implementation. |
Exported or not exported | Indicates whether a class is visible outside the assembly in which it is defined. This characteristic applies only to top-level classes and not to nested classes. |
Class Types
Static class
It is the type of class that cannot be instantiated. In other words, we cannot create an object of that class using the new keyword, such that class members can be called directly using their name.
- 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 class cannot be inherited.
- It allows only a static constructor to be declared.
- The static class methods can be called using the class name without creating the instance.
Abstract class
- Use abstract key word
- Incomplete and cannot be instantiated
- Can only be used as base class
- Abstract class cannot be sealed
- May contains abstract members but that is not mandatory
- The class derived from abstract class should provide the implementation for the abstract members.
- 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.
Partial classes
It is a type of class that allows dividing their properties, methods, and events into multiple source files, and at compile time, these files are combined into a single class.
- 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 is sealed, the same as for an abstract class.
- Inheritance cannot be applied to partial classes.
Sealed classes
A Sealed class is a class that cannot be inherited and used to restrict the properties.
- Access modifiers are not applied to a sealed class.
- To access the sealed members, we must create an object of the class.
Nested classes
This feature enables the user to logically group classes that are only used in one place, thus this increases the use of encapsulation, and creates more readable and maintainable code.
- A nested class can be declared as a private, public, protected, internal, protected internal, or private protected.
- – Outer class is not allowed to access inner class members directly as shown in above example.
- – You are allowed to create objects of inner class in outer class.
- – Inner class can access static member declared in outer class