Introduction to Classes in C#

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

C# Programming Guide

Classes are the primary building blocks of object-oriented programming (OOP) in C#. They provide a way to create new types and organize code for those types. Classes can have their own data members and methods, but also inherit from other classes and even implement interfaces.

This programming tutorial discusses classes and how you can work with them in C#.

Read: Best Online Courses for .NET Developers

What is a Class in C#?

A class is a template that combines data and operations inside a single unit. Objects, meanwhile, are instances of classes. In a class, programmers define the behavior and structure of an object. A class contains data (the characteristics of an object) and methods (the actions that can be performed on that data).

The objects created from classes are called instances. Whenever a class instance is created, it has its own copies of its members. When you create an instance of a class, it is called instantiation, and there can be multiple instances of each type, at any given time.

In a class, developers define the behavior and structure of an object. Whenever a class instance is created, it has its own copies of its members.

You can learn more about class members in our programming tutorial: Class Members in C#.

What is an Abstract Class in C#

Abstract classes are a special type of class that cannot be instantiated but can be used to define a common interface or implementation that can be used by classes that extend the abstract class. For example, you may want to create an abstract class called Shape that defines some basic properties and methods.

This allows all shapes to have those properties and methods, even if they are implemented differently by each specific type of shape. You can also use abstract classes in the same way as interfaces: by defining a common set of members that must be implemented by any subclasses you define for an abstract class.

The following code example shows how you can define an abstract class in C#:

    abstract class MyAbstractClass
    {
        public abstract void MyAbstractMethod();
    }

What is a Partial Class in C#?

In C#, a partial class enables developers to divide a class, struct, interface, or method declaration across two or more source files. At the time of compilation, all these pieces are integrated. You might want to take advantage of partial classes in these scenarios:

  • When working on large-scale projects, dividing a class into different files allows numerous developers to work on it concurrently.
  • You should also leverage partial classes when utilizing source generators to add new functionality to a class.
  • Partial classes can also be used to extend a class with additional methods, properties, fields, or events. For example, you could create a partial class for a Form class and add new methods to the partial class. When the Form class is compiled, the compiler combines all the files that contain the partial class into a single file.

Partial classes must have the same accessibility (for example, public or internal) and they must also be in the same namespace. In addition, all partial-type declarations must use the same type name.

Here is how you can define a partial class in C#:

    partial class MyPartialClass
    { 
      //Members of the partial class
    }

What is a Sealed Class in C#?

A sealed class is defined as a class that cannot be extended any further. However, you can create instances of a sealed class just like you do with other non-abstract classes. A sealed class in C# is analogous to a final class in Java.

The sealed keyword is used in front of a class definition to make it a sealed class. A sealed class cannot be extended by inheriting from it, but an existing class can be extended to become a sealed class by using the sealed keyword in front of its definition.

The following code example illustrates how a sealed class can be defined in C#:

sealed class MySealedClass
{ 
    //Write the members of the sealed class here
}

What is an Interface in C#

An interface is yet another type of a special class that can only contain method declarations. You cannot have data members in an interface due to which it can never be serialized. Remember, we serialize data and not the operations. However, note that, with the recent versions of the C# programming language (C# 8.0 and above), programmers can now have concrete methods in an interface.

Interfaces in C# are used to define a contract between objects, so that they know how to communicate with each other. You can think of it as a contract between two objects: one object has to implement all of the methods defined in the interface or none at all, and the other object can use those methods in its code.

The following code example illustrates how you can create an interface in C#:

 interface MyInterface
{
      void MyInterfaceMethod();
}

An interface consists of a set of methods and properties that a class must implement in order to be compatible with the interface. An interface is not an actual class, but rather a blueprint for how classes should behave. Interfaces are a way of specifying the behavior of an object without having to specify how it is implemented. The idea is that you can define what a class or struct must do, without defining how it accomplishes this task.

This makes it easier to create reusable code, because developers do not have to worry about the implementation details of how something works; instead, you can focus on what it does. Interfaces are a way to specify what functions your code should be able to perform. Interfaces are often used in class hierarchies as a way to specify an abstract base class (a purely abstract class actually) that contains functionality that all classes in the hierarchy must implement.

Final Thoughts on Classes in C#

Classes in C# provide a great way to organize and reuse code. By creating classes, programmers can group together related data and methods. The C# programming language provides support for several types of classes such as abstract, sealed, partial, etc. Not that an interface is also a class in C# – if you look at the generated IL code, you will see it represented as a class.

You can learn more about classes and objects in our programming tutorial: C# Object Oriented Programming.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Previous article
Next article

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read