Here we will discuss on types of classes in c#.
There are four different types of classes available in C#. They are as follows:
1. Static class
2. Abstract class
3. Partial class
4. Sealed class
Static class:
A class with static keyword that contains only static members is defined as static class. A static class cannot be instantiated.Characteristics of static class
- Static class cannot instantiated using a new keyword.
- Static items can only access other static items. For example, a static class can only contain static members, e.g. variable, methods etc.
- A static method can only contain static variables and can only access other static items.
- Static items share the resources between multiple users.
- Static cannot be used with indexers, destruction or types other than classes.
- A static constructor in a non-static class runs only once when the class is instantiated for the first time.
- A static constructor in a static class runs only once when any of its static members accessed for the first time.
- Static members are allocated in a high frequency heap area of the memory.
Abstract class
A class with abstract modifier indicate that class is abstract class. An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
A class with sealed keyword indicates that class is sealed to prevent inheritance. Sealed class cannot inheritance.
Characteristic of Abstract class
- An abstract class cannot be instantiated.
- An abstract class may contain abstract methods and accessorizes.
- It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
- A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
Partial class
- The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.
Characteristic of Partial class
- All the partial class definitions must be in the same assembly and namespace.
- All the parts must have the same accessibility like public or private, etc.
- If any part is declared abstract, sealed or base type then the whole class is declared of the same type.
- Different parts can have different base types and so the final class will inherit all the base types.
- The Partial modifier can only appear immediately before the keywords class, struct, or interface.
- Nested partial types are allowed.
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.