English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Classes (Class)

C# Object-Oriented Programming (OOP)-Classes are like blueprints for specific objects. In the real world, each object has some color, shape, and function

For example, the luxury sports car Ferrari. Ferrari is an object of the luxury car type. Luxury car is a class that indicates certain characteristics, such as speed, color, shape, interior, etc. Therefore, any company that manufactures cars that meet these requirements is an object of the luxury car type. For example, each car of BMW, Lamborghini, Cadillac is an object of the 'luxury car' class. Here, 'luxury car' is a class, and each physical car is an object of the luxury car class. Here, 'luxury car' is a class, and each physical car is an object of the luxury car class. Here, 'luxury car' is a class, and each physical car is an object of the luxury car class.

Classes allow you to create custom types by grouping variables of other types, methods, and events. In object-oriented programming, classes define properties, fields, events, methods, and more. A class defines the type of data and the functionality that the objects will have.

In C#, a class can be defined using the class keyword.

public class MyClass
{
    public string myField = string.Empty;
    public MyClass()
    {
    }
    public void MyMethod(int parameter1, string parameter2)
    {
        Console.WriteLine("First Parameter {0}, second parameter {1}1, parameter2;
    }
    public int MyAutoImplementedProperty { get; set; }
    private int myPropertyVar;
    
    public int MyProperty
    {
        get { return myPropertyVar; }
        set { myPropertyVar = value; }
    } 
}

The following diagram shows the important building blocks of C# classes.

C# Class

C# Access Modifiers

Access modifiers are applied to declarations of classes, methods, properties, fields, and other members. They define the accessibility of the class and its members. Public, private, protected, and internal are access modifiers in C#. We will understand this in the keyword section.

C# Field

This field is a class-level variable with a value. Typically, field members should have a private access modifier and be used with properties.

C# Constructor

A class can have parameterized or non-parameterized constructors. When creating an instance of a class, the constructor is called. The constructor can be defined using access modifiers and class names:

<access modifiers> <class name>() { }
class MyClass
{
    public MyClass()
    {
    }
}

C# Method

The following template can be used to define a method:

{access modifier} {return type} MethodName({parameterType parameterName})
public void MyMethod(int parameter1, string parameter2)
{
    // Write your method code here. 
}

Properties

Properties can be defined using getters and setters as follows:

private int _myPropertyVar;
public int MyProperty
{
    get { return _myPropertyVar; }
    set { _myPropertyVar = value; }
}

Properties encapsulate a private field. They provide getters (get {}) to retrieve the value of the base field, and setters (set {}) to set the value of the base field. In the above example, _myPropertyVar is a private field that cannot be accessed directly. It can only be accessed through MyProperty. Therefore, MyProperty encapsulates _myPropertyVar.

You can also apply some other logic in get and set, as shown in the following example.

private int _myPropertyVar;
public int MyProperty
{
    get {
        return _myPropertyVar / 2;
    }
    set {
        if (value > 100)
            _myPropertyVar = 100;
        else
            _myPropertyVar = value;
    }
}

Auto-implemented properties

From C#3Starting with .NET 3.5, if you do not want to apply certain logic in get or set, you can easily declare the property.

Below is an example of an auto-implemented property:

public int MyAutoImplementedProperty { get; set; }

Please note that there are no private backing fields in the above property example. Supported fields will be automatically created by the compiler. You can use auto-implemented properties just like regular class properties. Auto-implemented properties are implemented automatically just to easily declare properties when there is no need for additional logic in the property accessors.

Namespace

A namespace is a container for a group of related classes and namespaces. Namespaces are also used to provide unique names for classes within the namespace. Namespaces and classes are separated by a dot (.)

In C#, the namespace keyword can be used to define a namespace.

namespace CSharpTutorials
{
    class MyClass
    {
    }
}

In the above example, the fully qualified name of MyClass is CSharpTutorials.MyClass.

Namespaces can contain other namespaces. Internal namespaces can be separated by dots (.).

namespace CSharpTutorials.Examples
{
    class MyClassExample
    {
    }
}

In the above example, the fully qualified name of MyClassExample is CSharpTutorials.Examples.MyClassExample.