Classes are ubiquitous in Object-Oriented Programming & Systems. Classes are very important in C# programming as well; we cannot assume a single program in OOPS without class. Then let's go through the tutorial, class in C#.
- What are classes?
- Defining a class in C#
- What constitutes a class with C# class example
- Types of classes in C#
What are classes?
Let's take an example of a real-world entity. You ask your friend, which movie they last saw? One friend might say, "I saw Hindi movie Dhoom3 - Aamir Khan acting suburb, Krishna Acharya has directed, one & half hour long and U/A certificate etc".
So the movie entity has Name, Actors, Director, Duration and U/A, etc attribute and these attributes define a movie. If you ask another friend, more or less he will also give the details of the movies in the form of same attributes. So A set of attributes are defining a movie and these set of attributes vary from movie to movie.
If movie entity has to be represented in OOPS, we could create Movie Class with all the common attributes defined as properties; and create different movies like Dhoom3, Titanic, Avatar, etc. So, varying values for set of attributes are defining different movies.
So, classes are the nothing but a template or blueprint used to create objects/instances of a certain entity. We can also say a class represents, an entity and defines how their the different instances looks and behaves.
Defining a class in C#
{
}
- In C# .NET, a class will be defined using the class keyword.
- Every class will have name;in the above C# code sample, class name is Movie.
- The class will have its entire definition defined within the { flower brackets }.
What constitutes a class with C# class example
In short class will have a name, attributes defined as properties, behaviors /capabilities defined as methods and constructors to create instances of the class.
Let's go through an example of class, SmtpClient to understand what constitutes a class. SmtpClient class is under System.net.Mail namespace which is present in .NET framework. In an high level the SmtpClient class looks like below:{
public class SmtpClient
{
// Properties which defines class.
public string Host { get; set; }
public int Port { get; set; }
// Constructor to create class instance
public SmtpClient(string host, int port)
{
// Constructor logic the goes here...
}
public void Send(string from,string recipients,
string subject,string body)
{
// method logic goes here...
}
}
}
- Class name: In the above c# code sample, SmptClient is the class name. Class name used to uniquely identify a class.
public class SmtpClient
- Namespace: Namespace used to group related classes together. Class names should be unique in a given namespace. In the above C# class example, System.Net.Mail is the namespace.
- Properties: Properties helps to build a instance of the class. If you have two different set of values for a given entity, two different instances/objects will be created; i.e class instances vary by their properties values.
public string Host { get; set; }
public int Port { get; set; }The above C# class code sample, has two properties Host & Port.
Consider another example of Person class. Two different persons will have different name, profession, address, etc. So name, profession and address, etc represent a Person. Hence Person class developer defines class with Name, Profession and Address as properties.
- Constructors: Constructors are special methods in a class used to create an instance of the class.
public SmtpClient(string host, int post)
{
// Constructor logic the goes here...
}Constructors should take all the required/mandatory parameters as arguments; So that, after you have the objects created using constructors, the object should be usable and shouldn't throw exceptions when a method is invoked using the object.
- Methods: The capabilities of a class will be exposed as methods. For example, in the above SmtpClient class, Send() method sends the email out.
public void Send(string from,string recipients,
string subject,string body)
{
// method logic goes here...
}The Send() method takes parameters which tells, what an email should be sent and for whom an email should be sent. i.e method arguments defines how the capabilities of the class will be used to get the desired result.
- Class access specifier: Last but not the least, SmtpClient is defined with public access specifier. Meaning its visible to all.
public class SmtpClient
We can specify different access specifiers for the class i.e. public, internal, private, etc
Types of classes in C#
In C# we can define classes as static, abstract, sealed and partial. Let's go through one by one:
- Abstract class: Abstract classes cannot be instantiated. These are mainly created to act as base class.
- Static class: Static classes cannot be instantiated and no one can inherit from static classes. Mainly used to act as helper classes.
- Sealed class: Sealed classes cannot be inherited. In case if you don't want to make class to be inherited, consider marking a class as sealed .
- Partial class: Partial classes is not really a different type of class. Partial classes allows some part of the class definition coded in file and another part of the class coded in different file.
For example, partial classes are used by Visual Studio Windows form designer, where in which form designer partial class will be generated by Visual Studio and code behind partial class is created by developer.