Published on

What are the different types of ๐—ฐ๐—น๐—ฎ๐˜€๐˜€๐—ฒ๐˜€ inย .NETย ?

Authors
  • avatar
    Name
    Muhammad Mustafa
    Twitter

A class is a blueprint for objects in the digital world. It defines their qualities (properties) and actions (methods) for members of the class.

๐—”๐—ฏ๐˜€๐˜๐—ฟ๐—ฎ๐—ฐ๐˜ ๐ŸŽจ:

A class that represents a contact for other classes that inherits from and provides common definition and implementation.ย 

  • It Cannot be instantiated. Can only be inherited.
  • It Contains both abstract and non-abstract methods.
  • A non-abstract class that inherits from an abstract class must implement all inherited abstract methods.

๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ: If we're defining multiple types of printers (Color Printer, Black and White Printer, etc). They are all in essence a Printer, they share some qualities and function. They all can Print() but each has different mechanism.ย  So every type of printer will inherit Abstract Printer and provide their own implementation.

๐—ฆ๐—ฒ๐—ฎ๐—น๐—ฒ๐—ฑ ๐Ÿ”’: The opposite of ๐—”๐—ฏ๐˜€๐˜๐—ฟ๐—ฎ๐—ฐ๐˜. They are used to prevent other classes from extending or modifying behavior.

๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ: The sealed keyword tells the CLR that there is no class further down to look for methods, and that can improve performance.

๐—ฃ๐—ฎ๐—ฟ๐˜๐—ถ๐—ฎ๐—น ๐Ÿงฉ:ย 

A class that could be divided into multiple pieces in different files. It allows dividing a class's properties and methods into multiple source files that are later compiled into a single file.

  • If you seal a specific part of a partial class then the entire class is sealed.
  • If a partial class is meant to be a base class, all of its parts collectively represent the base class.

๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ:

  • Multiple developers can work on the same class easily.ย 
  • Partials could implement different interfaces in separate files.ย 
  • This could be used in a Feature Driven Design where we want to limit a feature inside its own boundaries but still use functionality from outside.

๐—ฆ๐˜๐—ฎ๐˜๐—ถ๐—ฐ:ย 

A class that can't be instantiated, and its members are called directly using the class name.

๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ:

  • They can be used for Extension Methods, Constants, Configuration and Common Functionality (Similar to Math Library)ย 
  • They used to be used to create a Singleton implementation but it's better to use the Dependency Injection Container.

๐—”๐—ป๐—ผ๐—ป๐˜†๐—บ๐—ผ๐˜‚๐˜€ ๐ŸŽญ:ย 

A class that closely resembles a JavaScript object, a dynamic type.

Var object = new {Name: "Unknown", Age: 25}ย 

๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ: They are often used in LINQ queries or when returning ad-hoc data from methods.

๐—ฆ๐˜๐—ฟ๐˜‚๐—ฐ๐˜ ๐Ÿ—๏ธ: Similar to class except they are Immutable, Value Type and provide common implementations for some methods. (eg. ToString())

๐—ฅ๐—ฒ๐—ฐ๐—ผ๐—ฟ๐—ฑ ๐Ÿ“š: Are the middle ground between a Class and a Struct. They are immutable reference types with common implementations for some methods (eg. ToString())