What are the types of classes in C#?
Loading
What are the types of classes in C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Onkar SharmaPosted Nov 13, 2022, 2:43 PM
Hi Naresh,
To know more about the Types of Classes in C#, visit
Thanks!
Rajanikant HawaldarPosted Oct 31, 2022, 4:07 PM
1) Class : A type that is defined as a class is a reference type. Classes are declared by using the
classkeyword followed by a unique identifier, as shown in the following example:At run time, when you declare a variable of a reference type, the variable contains the value
nulluntil you explicitly create an instance of the class by using thenewoperator, or assign it an object of a compatible type that may have been created elsewhere, as shown in the following example:When the object is created, enough memory is allocated on the managed heap for that specific object, and the variable holds only a reference to the location of said object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as garbage collection. However, garbage collection is also highly optimized and in most scenarios, it does not create a performance issue. For more information about garbage collection.
The
classkeyword is preceded by the access level. Becausepublicis used in this case, anyone can create instances of this class. The name of the class follows theclasskeyword. The name of the class must be a valid C# identifier name. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.2) Nested Class: A type defined within a class, struct or interface is called a nested type. For example
Regardless of whether the outer type is a class, interface, or struct, nested types default to private; they are accessible only from their containing type. In the previous example, the
Nestedclass is inaccessible to external types.Nested types of a class can be public, protected, internal, protected internal, private or private protected.
3) Static class : a static class is a class that cannot be instantiated. The main purpose of using static classes in C# is to provide blueprints of its inherited classes. Static classes are created using the static keyword in C# and .NET. A static class can contain static members only. You can‘t create an object for the static class.
https://www.c-sharpcorner.com/UploadFile/74ce7b/static-class-in-C-Sharp/
4) Abstract class:
An abstract class is an incomplete class or special class we can't be instantiated. The purpose of an abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.
We can use an abstract class as a base class and all derived classes must implement abstract definitions. An abstract method must be implemented in all non-abstract classes using the override keyword. After overriding the abstract method is in the non-Abstract class. We can derive this class in another class and again we can override the same abstract method with it.
https://www.c-sharpcorner.com/UploadFile/annathurai/abstract-class-in-C-Sharp/
5) Partial class: It is possible to split the definition of a class, a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable:
* When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
* When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
* When using source generators to generate additional functionality in a class.
To split a class definition, use the partial keyword modifier, as shown here:
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.
https://www.c-sharpcorner.com/UploadFile/3d39b4/partial-classes-in-C-Sharp-with-real-example/
6) Sealed class: When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.
You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.
https://www.c-sharpcorner.com/article/sealed-class-in-C-Sharp/