[C# Basics] What are Classes and how to use them


Staff member
May 28, 2020
United States
C# is a programming language that runs on top of the Microsoft .NET Runtime (there are alternatives such as Mono and the new dotnet core runtime however they will all be merged into .NET 5 late 2020) and it is one of the most popular programming languages. It has strong support for Microsoft SQL Server which makes it a good programming language for business data-driven applications. This tutorial post discusses what C# classes are, how to create them, and why you should use them.

Background: A common anti-pattern in JavaScript isn’t allowed in C#

The programming language JavaScript does not require you to use a class to create an object although it does allow you to create a class and it’s recommended.

Constructing an object directly (e.g. without a class) in JavaScript is a common anti-pattern and should be avoided. Look at the following example where I constructed an object without a class.

// this is valid JavaScript
var myObject = {
    a: "1",
    b: "2"

As you can imagine I might later on need to write several very similar objects and attach methods to each of them. Over time my code could get messy and if I forget to update a method across every single object I could get some undesired behavior.

Thankfully C# does not allow developers to take this shortcut and requires you to use classes if you want to use objects. You cannot construct an object directly, rather you must use a class and instantiate it for each object. While it may be frustrating for a new developer at first, by using a class you ensure a certain well defined object structure reducing bugs if you create copies of the object with different data later on.

What is a class?

A class is a block of code which defines the structure an an object, defines methods (such as getters and setters) to interact with said object, and provides a structured way to construct and instantiate an object. When you create a new object with the class you ensure the behavior is consistent with other objects of the same class. This also means that if you define a method, it’s defined on all of the objects from the class automatically.

An easy way to think about classes is to compare them to real life. A class is like a recipe: If you need to bake five birthday cakes for a special event you will probably want them all to be the same, to do this you’ll follow the same recipe for each cake, then you’ll put icing on the cake (for example Happy Birthday Mike may be on these cakes, but Happy Birthday Emily may be on next week’s cakes, the recipe provides for these small changes without needing a new recipe, the icing is like your class’s properties).

How do I create a class?

To create a class you must name the class, define any properties, and write a constructor (you can also add additional methods to do certain things but they are outside the scope of this post). Take a look at the following code example (a class I wrote for work a few weeks ago). I defined several properties, a constructor, and even used a few generics.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Your.Application.Models
    public class PagedResults<T>
        public int ItemCount { get; }
        public int PageCount { get; }
        public int Skip { get; }
        public int Take { get; }
        public IEnumerable<T> PageOfResults { get; }
        public int FirstPage { get; }
        public int LastPage { get; }
        public int NextPage { get; }
        public int CurrentPage { get; }
        public int PreviousPage { get; }

        public PagedResults(IQueryable<T> results, int pageNumber, int resultsPerPage)
            ItemCount = results.Count();
            PageCount = (int) Math.Ceiling((double) ItemCount / resultsPerPage);
            Skip = (pageNumber - 1) * resultsPerPage;
            Take = resultsPerPage;
            PageOfResults = results.Skip(Skip).Take(Take).ToList();
            FirstPage = 1;
            LastPage = LastPage = PageCount == 0 ? 1 : PageCount;
            NextPage = Math.Min(pageNumber + 1, LastPage);
            CurrentPage = pageNumber;
            PreviousPage = Math.Max(pageNumber - 1, FirstPage);

Why should I create a class?

You should create a class (aside from the fact C# requires you to) to ensure your application has consistent behavior across it’s related objects. When constructing objects directly, human error will occur and create difficult to trace bugs, and by using classes you avoid this issue altogether.
  • Like
Reactions: Jinder