Regular Class


In this article we are going to discuss the class in C#. There are many blogs, articles are available on the internet regarding class but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the class in C#.

Class Introduction

  1. In C#, a class is a template/blueprint for creating objects. It is a fundamental building block of object-oriented programming (OOP) that encapsulates data for the object and behaviors (methods) that operate on the data.
  2. A software system may consist of many classes (A real time system may have thousand or more classes). Working with so many classes is not so easy, so we require some way, so that they can be managed properly or efficiently.
  3. We can say that a class combines the fields and methods(member function which defines actions) into a single unit.
  4. In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes.
  5. By default modifier of the class is internal.

Class Bluprint - A Class is like the blueprints for a house. It defines the properties of a house like shape, floors, the works. An Object is more like the house itself. It's the end product built by the properties defined in the Class.

Example

Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.

A class can be declared using following access specifier which limits the accessibility of classes to other classes.

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected internal

A Class contains fields, methods, Constructor and properties.

Abstract Class

1. Constructoris - A constructoris a method in the class which gets executed when a class object is created.
2. Field - A field is a variable of any type. It is basically the data.
3. Property  - A property is a member that provides helps in read and write of private field.
4. Method  - A method is a code block that contains a series of statements.

Code defined as folowing:

Abstract Class

Declaring a Class in C#

A class is defined using the class keyword followed by the class name. The body of the class contains data members (fields or properties) and methods.

In general, class declarations can include these components, in order.

  1. Modifiers - A class can be public or internal etc. By default modifier of the class is internal.
  2. Keyword class - A class keyword is used to declare the type class.
  3. Class Identifier - The variable of type class is provided. The identifier(or name of the class) should begin with an initial letter which should be capitalized by convention.
  4. Base class or Super class - The name of the class’s parent (superclass), if any, preceded by the : (colon). This is optional.
  5. Interfaces - A comma-separated list of interfaces implemented by the class, if any, preceded by the : (colon). A class can implement more than one interface. This is optional.
  6. Body - The class body is surrounded by { } (curly braces).

The following code define the declaration of class.

// declaring public class
public class MyClass
{
    // Data members (fields)
    private int myField;

    // Method
    public void MyMethod()
    {
        Console.WriteLine("My Method");
    }
}

Types of classes in C#

In C#, classes can be categorized into several types based on their functionality and purpose. Some listed classes name as following:

Abstract Class
  1. Regular Classes
  2. Abstract Classes
  3. Static Classes
  4. Partial Classes
  5. Sealed Classes
  6. Nested Classes
  7. Generic Classes

The below code defines the declaration of various type classes.

public class MyClass
{
    // Fields, properties, methods, etc.  // Regular Classes 
}

public abstract class AbstractClass
{
    public abstract void AbstractMethod();  // Abstract Classes 
}

public static class UtilityClass
{
    public static void DoSomething() { }  // Static Classes 
}

public partial class PartialClass
{
    // Members in this file            // Partial Classes 
}

// In another file:
public partial class PartialClass
{
    // Members in another file     
}

public sealed class SealedClass
{
    // Members                   // Sealed Classes     
}
public class OuterClass
{
    public class NestedClass
    {
        // Members           // Nested Classes 
    }
}

public class GenericClass
{
    // Members                    // Generic Classes 
}

Class Advantage in C#

Classes in C# offer several advantages, There are some of the key advantages of using classes in C# include.

  1. Encapsulation - Classes enable encapsulation, allowing you to bundle data (fields) and behaviors (methods) together within a single unit. This helps in hiding the internal state of an object and exposing only the necessary functionality, which enhances data security and prevents accidental modification.
  2. Code Organization - Classes provide a structured way to organize code by grouping related data and methods together. This improves code readability, maintainability, and scalability.
  3. Code Reusability - By defining classes, you can create reusable components that can be instantiated multiple times in different parts of your program. This promotes code reuse, reduces redundancy, and facilitates modular programming, leading to more efficient development and easier maintenance.
  4. Inheritance - C# supports class inheritance, allowing you to create new classes (derived classes) based on existing classes (base classes). Inheritance promotes code reuse by inheriting properties and methods from base classes, thus facilitating the creation of hierarchical class structures and reducing duplication of code.
  5. Polymorphism - Classes enable polymorphism, a key concept in object-oriented programming. Polymorphism allows objects of different classes to be treated uniformly based on a common interface or base class. This facilitates flexibility in code design and implementation, as it enables dynamic method invocation and runtime binding of objects.
  6. Modularity and Extensibility - Classes promote modularity by encapsulating related functionalities into cohesive units. This makes it easier to extend and modify the functionality of your program without affecting other parts of the code.

Next