C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have discussed about
namespaces in
C#. Now, let's see the definition of class and class objects in this chapter.
Objective
The main objective of this article is to learn about classes in C#, creating objects to a class and class inheritances in
C# programming.
Class
Class is a reference type which enables to create own custom types by grouping the variables of other types, events and functions.
Syntax:
class MyClass
{
.......; // class body
......; // class body
}
- They are declared by using '
class
' keyword. - '
MyClass
' in the above syntax is your class name which follows the base class and interfaces. - Class body (with in the braces) can have all the class members such as properties, methods, fields, constructors, nested types, finalizer etc.
- Class defines the behavior and data of a type.
- Class supports inheritance (OOPS fundamental).
Creating Objects to a Class
Object is an instance of a class (However both are different). Class defines an object but it cannot be defined as an object.

- To create an object,
new
keyword is used. For example,
MyClass obj1 = new MyClass(); // Object 'obj1' for 'MyClass' is created.
In the above example, we have created an instance (object) to the class (MyClass
). - After that (creation of object), a reference to that object is passed back to the programmer.
- In the above example,
obj1
is a reference to an object based on MyClass
which refers to the new object without any data. - We can create reference without creating the object. For example,
MyClass obj2; // Reference is created but not object
Note: new keyword is necessary to create an object. - We can also refer multiple object references to the same object. For example,
MyClass obj3 = new MyClass(); // creating reference for new object
MyClass obj4 = obj3; // creating reference for the same object.
MyClass obj5 = obj3;
- Since objects are referred to the classes by reference, classes are known as reference types.
Class Inheritance
Inheritance is a mechanism of creating a class (child or derived) from another class (parent or base), which inherits the data and behavior from that class (parent).
Inheritance allows the derived (child) classes to overload all the methods from their base (parent) class.
Syntax:
public class MyDerived : MyBase
{
// All the methods, fields, events, properties etc. are inherited
// from 'MyBase' class to 'MyDerived' class
}
In the above example,
MyDerived
is the derived class which gets all the properties of
MyBase
.
Note: Constructors cannot be inherited from base class to derived class.
In C#, a class cannot inherit multiple base classes directly. For example,
public class MyDerived : MyBase1, MyBase2 // Not possible
{
..........;
........;
}
But, a class may inherit multiple base classes indirectly such as in the below figure,
From the above figure,
public class MyBase2 : MyBase1
{
// Base1 methods will be inherited (not private methods)
}
And,
public class MyDerived : MyBase2
{
// Base2 methods will be inherited (not private methods)
// Base1 methods also inherited indirectly through Base2 (not private methods).
}
The above code explains inheriting properties from multiple base classes in C#.
Note: Inheritance doesn't have ability to inherit properties from a 'private
' class.
Conclusion
In this article, we have seen class in C# with creating objects and class inheritances. Hope you understand.
Thanks for reading.
Regards,
Krishna.