Everything You Need To Know About Classes in CSharp

Uzoamaka Nweze
6 min readNov 19, 2022

--

Photo by Kenny Eliason on Unsplash

Okay. Let’s get into understanding what classes are. What are classes? According to GeeksForGeeks, “A class is a user-defined blueprint or prototype from which objects are created.”

Let’s try to understand this using real-world explanation.

To a fashion designer, a class is the design of that dress you made in your book that will guide you while making the dress. That design makes it possible for you to keep recreating copies of that particular dress. The class is that design on paper. The copies of the dresses you make are objects of that class.

To a gym instructor, a class is that routine that you have designed that is guaranteed to build the abs of any user that follows the routine. When you start implementing these designs at the gym by making a gym user who wants abs follow this routine of yours, it becomes an object

To a four-year-old, a class is when you tell me the type of ice cream you want, “white chocolate + vanilla flavour + strawberries”, then when the ice cream is handed over to you, the way you described it, it becomes an object. So, now, enjoy your yummy chocolate + vanilla + strawberry ice cream.

A class can detect how visible it wants to be for other classes, with the help of access modifiers. There are six types of access modifiers in C#.

There are the public, private, protected, internal, protected internal, and private protected access modifiers.

The public access modifier makes whatever it is attached to visible to everyone. And this can also mean that everyone who has access to it can manipulate it.

The private access modifier makes its contents accessible on the class level only. No other class can access this class’ private members. Its members are for that class and that class alone.

The Protected access modifier gives access to the members of the class and to any class that inherits that class, but not to the general public.

The internal access modifier gives access to the members of the same assembly. A summary of an assembly is a project. So, the members of the same project can access the class with an internal access modifier.

The protected Internal access modifier is a combination of protected(can be accessed by the parent class and the child class) and internal(can be accessed by members of the same assembly). So, a protected internal access modifier allows access to members of a different assembly who inherits from it.

The private protected access modifier does not grant access to a class that inherits it but is not in the same assembly.

A class contains:

Fields, Properties, Constructors and/or Methods.

Fields: according to Microsoft, “A field is a variable of any type that is declared directly in a class or struct.”

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field
}

Properties: according to Microsoft, “A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they’re public data members, but they’re special methods called accessors.” You can get the value of a field and/or set the value of the same field

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field

public int Length // This and everything inside it is a property
{
get{return _length;}
set{_length = value;}
}
}

Constructors: Constructors are special methods of a class that get created automatically when a new object is invoked.

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field

public int Length // This and everything inside it is a property
{
get{return _length;}
set{_length = value;}
}
public Rectangle(int length, int width)// This is a parameterized constructor
{
_length = length;
_width = width;
}
}

The code above is an example of a parameterized constructor. This type of constructor allows the user to pass in parameters when it’s called, else it’s going to throw in an error. There is also another type of constructor called the default constructor. It doesn’t take in parameters and it’s created automatically for you when you create a class and when you do not declare a parameterized constructor. This is how it’s declared when you insist on declaring it yourself.

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field

public int Length // This and everything inside it is a property
{
get{return _length;}
set{_length = value;}
}
public Rectangle()// This is a default constructor
{

}
}

Methods: according to Mircosoft, “A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method.”

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field

public int Length // This and everything inside it is a property
{
get{return _length;}
set{_length = value;}
}
public Rectangle(int length, int width)// This is a parameterized constructor
{
_length = length;
_width = width;
}
public void GetArea()// This is a method
{
int area = Length * Width;
Console.WriteLine($"The area of a rectangle is {area}");
}
}

There are a few different types of classes.

  1. Concrete class
  2. Partial class
  3. Abstract class
  4. Sealed class
  5. Static class

Concrete class: this is a class that can be inherited and/or instantiated by an object. It doesn’t have anything attached to it.

public class Rectangle
{
private int _length;// This is a field
private int _width;// This is another field

public int Length // This and everything inside it is a property
{
get{return _length;}
set{_length = value;}
}
public Rectangle(int length, int width)// This is a parameterized constructor
{
_length = length;
_width = width;
}
public void GetArea()// This is a method
{
int area = Length * Width;
Console.WriteLine($"The area of a rectangle is {area}");
}
}

This example is a perfect example of a concrete class.

Partial class: the partial keyword is prefixed before a class to show that the classes with these keywords are one and the same. Here’s a code example.

public partial class Rectangle
{
public void GetArea()
{
}
}

public partial class Rectangle
{
public void GetPerimeter()
{
}
}

This class is the same Rectangle class, split into two different classes. When you run this class, it will contain all the methods in both classes. The partial method is mostly used when a class is becoming too big and you need to split it for clarity of code.

Abstract class: the keyword abstract is prefixed before the type, class to show an abstract class. Abstract classes can not be instantiated, meaning that you cannot create an object out of that class. It can only be inherited.

Here’s an article I wrote about Inheritance. This will give you a better understanding of what inheritance is all about.

abstract class Rectangle
{

public abstract void Display();
}


public class AreaOfARectangle : Rectangle
{
private int _length;
private int _width;

public int Length
{
get{return _length;}
set{_length = value;}
}
public Rectangle(int length, int width)
{
_length = length;
_width = width;
}
public override void Display()
{
Console.WriteLine($"Length is{Length}");
Console.WriteLine($"Width is{Length}");
}

public void GetArea()
{
int area = Length * Width;
Console.WriteLine($"The area of a rectangle is {area}");
}
}

In the example above, a non-abstract class inherited an abstract class and its abstract method. And this is done by overriding the abstract method and providing an implementation for that particular method.

Sealed class: a sealed class is prefixed with the sealed keyword. This class cannot be inherited but it can be instantiated.

sealed class Rectangle{

public int GetArea(int length, int width)
{
return length * width;
}
}

class Program {


static void Main(string[] args)
{

Rectangle rect = new Rectangle();

int area = rect.GetArea(6, 4);
Console.WriteLine("Area = " + area);
}
}

Static classes: these classes are utility classes. They can neither be inherited nor instantiated, they are really not about that life. Also, static classes can only have static members.

static class Rectangle
{
public static int _length;
public static int _width;

static Rectangle(int length, int width)
{
_length = length;
_width = width;
}
public static void GetArea()
{
int area = _length * _width;
Console.WriteLine($"The area of a rectangle is {area}");
}
}

Thank you for reading.

SOURCES

https://www.geeksforgeeks.org/c-sharp-class-and-object/

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/

https://medium.com/@uzonweze/all-about-csharp-inheritance-c04e9cfc0ae5

--

--