C# Classes Explained
(Page 1 of 6 )
Continuing our series about C# programming, in this article we discuss classes, which lie at the heart of any modern Object-Oriented Programming language. Among other things, you will learn some of the ways in which C# is different from C and C++ in this vital area.
In this article we will discuss C# classes and how to create a class in C#. We will also discuss Class Members like fields, constants and static members; I will also discuss the Access Modifiers available in C#.
A class is the core of any modern Object Oriented Programming language such as C#; most of the time, you will be writing classes. If you have a background in C or C++, many class aspects are similar, but some are different. For example, C# doesn't permit functions to exist outside the class declaration like C++; also, C# use the .NET Framework Class Library, so you have access to thousands more functions than the class libraries available to C++.
But the concept of data encapsulation is the same in all the modern OOP languages, so a class in C# contains data (fields for example) and operations that manipulate the data (methods). For example, a class that describes an employee would include fields like firstName, lastName, dateOfBirth, basicSalary and department, and methods that operate on the data, such as CalculateSalary() which calculates the employee's basic salary plus the overtime, for example, and another method like RaiseBasicSalary() that raises the basic salary of the employee.
To create a class, you simply use the keyword "class" followed by the class name:
class Employee
{
}
You can precede the class keyword with an access modifier keyword (which we will discuss later in the article):
public class Employee
{
}
All of the class members must exist between the curly braces, so you don't have global functions like C++. You can create more than one class definition in one physical file, but it's not a good programming practice; it's better to create each class in one file with the extension .cs
Note that all classes in C# directly or indirectly inherit from one base class, System.Object, and you don't have to explicitly define the inheritance, because the C# compiler enforces it. So the following class:
public class Employee
{
}
is the same as:
public class Employee : System.Object
{
}
You can add a class to your project using Visual Studio.NET; simply right click the project icon in Solution Explorer window (you can view the Solution Explorer windows using CTRL+ALT+L), then click on Add --> Add Class, and the Add New Item windows will be shown:

Note that you can add a class, Windows Form, Control, Component and many other variations; this is one feature of the Visual Studio.NET tool. For example, when you add Windows Form, VS.NET will create the Form (it's a class too) and generate code that the Form requires.
Modify the name of the class to Employee.cs and click Open. VS.NET will create the file, and generate basic class declaration code:
using System;
namespace Company
{
/// <summary>
/// Summary description for Employee.
/// </summary>
public class Employee
{
public Employee()
{
//
// TODO: Add constructor logic here
//
}
}
}
VS.NET declares the class as public, and creates an empty default constructor along with some comments. These are special kinds of comments that are used for generating documentation -- another feature of VS.NET. For now, delete the documentation and the default constructor, so you will show the Employee class as:
using System;
namespace Company
{
public class Employee
{
}
}
Before we discuss class members you should know that every member must have an access modifier, and if you didn't supply one, C# will consider this member a private member. It's better, however, that you explicitly define it private with the keyword "private." As you can see, there's no semicolon needed after the class declaration, although you can put it there if you're used to doing so. There are no more #include files, because the C# compiler automatically resolves project dependencies. In C# the operators -> and :: (unlike C++) don't exist, so if you want to access class members, use the dot operator ( . )
C# classes are reference types, so they are never allocated on the Stack; they are allocated on the Managed Heap. The Managed Heap is an area of memory that is managed by the Common Language Runtime, which has the ability to free unused memory blocks (objects) in a process known as Garbage Collection. When you have a variable that's not attached to an object:
Employee Michael;
You will not be able to access the object's members, and the variable Michael will reference to a block of memory which contains nothing, so we say of this variable that "it has a null reference" which means we can't use it. You must instantiate an object in order to use it:
Employee Michael = new Employee();
As we have said before, the new operator creates the object on the Managed Heap and returns the reference, which will be stored in the variable Michael.
Next: Access Modifiers >>
More C# Articles
More By Michael Youssef