C# Class and Object

A class is a blueprint or a template for creating objects that define the properties and methods of the object. 

- A class is a user-defined data type that encapsulates data members (fields) and member functions (methods) into a single unit.

- A Class is an entity that encapsulates all the properties of its objects an instance as a single unit.

C# has 5 types of classes

1. Concrete Class: is a class that allows creating an instance or object using the new keyword. Unlike abstract class which can't be directly instantiated, a concrete class can be used to create objects directly.

2. Static Class: is defined by the keyword 'static' doesn't allow inheritance, therefore you can't create an object for a static class.

- We can directly use static class in our program.

- Sealed: Static classes are implicitly sealed, which means they cannot be inherited.

- Static Members Only: All members of a static class must be static. This includes methods, properties, fields, and events.

- Memory Allocation: The memory for static members is allocated once and shared across all uses of the class.

- Constructor: Yes, but it invoked once when program executes

- Parametrized constructor: not allowed

3. Partial Class: is defined by the keyword 'partial' allows its members to partially divided or shared (.cs) files

- It can break the functionality into many files.

- Access Modifiers: Partial classes can span multiple files, but all parts must have the same access modifier. For example, if one file declares the class as   public, all other parts must also be public.

- Partial Methods: You can also define partial methods within partial classes. These methods are declared in one part and implemented in another. If the method is not implemented, it has no effect on performance or functionality.

- When using Entity Framework, especially in the database-first or model-first approaches, the EF tools generate entity classes that represent your database tables. These generated classes can be marked as partial, which allows you to extend them with additional properties, methods, or functionality in separate files without modifying the auto-generated code.

Why? Partial Classes

Code Organization: Separate auto-generated code (e.g., designer files) from manually written code.

Large Classes: Break down large classes into manageable sections.

Team Development: Allow multiple developers to work on different parts of the same class simultaneously.

4. Abstract Class: is a class that can't be instantiated where you can't create objects, abstract classes work on the OOPs concept of abstraction. Abstraction helps to extract essential details and hide the unessential ones.

- Access Modifiers: Properties in abstract classes can have various access levels (e.g., public, protected, private), which control their visibility and accessibility.

- You cannot directly create an instance of an abstract class.

- We can create constructor of abstract class, but it will invoke when subclass's constructor invoked.

5. Sealed Class: is a class that can't be inherited, use the keyword sealed to restrict access to users to inherit that class.

- We can create constructor of the Sealed class.

- We can create object of the Sealed class.

Why? Sealed Classes

Sealed classes are particularly useful in scenarios where a definitive and limited set of classes is required. For example, in a library where you have a set of error classes, and you want to ensure that no additional error types can be introduced outside the library. Sealed classes can also be used for performance optimization since they allow for certain run-time optimizations by the compiler.


---------------------------------------------------------------------------------------------------------------

In C#, an object is an instance of a class that is created dynamically using the new keyword. An object has its own state and behavior, which are defined by the fields and methods of the class.

An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection.

Comments

Popular posts from this blog

C# | Association, Aggregation and Composition

Throw vs Throw ex in C#

C# Extension Method