This tutorial is about how to define class in C#. Class can be defined as a template for the objects

Don't forget to check out our site http://howtech.tv/ for more free how-to videos!
http://youtube.com/ithowtovids - our feed
http://www.facebook.com/howtechtv - join us on facebook
https://plus.google.com/103440382717658277879 - our group in Google+

Introduction

Classes in C# are the main building blocks of the language. The syntax of define a class in c# is as follows:
Class (classname)
{
//members
}

The class keyword is used in c to declare a class and is followed by the name of the class. Class can be defined as a template for the objects. The delimiters or the braces are used to indicate the start and end of a class body that can hold its members. Members can be fields, methods, events and properties.

Step 1: Declaring a class
The following code snippet is an example on how to define class in C#:
class Info
{
int id;
string name;
int mark;

public void PrintDetails()
{
Console.WriteLine("Please Enter your Id \t:");
id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter your name \t:");
name = Console.ReadLine();
Console.WriteLine("Please Enter your mark \t:");
mark = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Id \t:" + id);
Console.WriteLine("Name \t:" + name);
Console.WriteLine("Mark \t:" + mark);
}
}
Here the variables are id, name and mark that are referred to as fields and the function is PrintDetails() that is referred to as method. Fields are the data belonging to the class and methods define the behavior of the class.

Step 2: Initializing a class
A class is a reference type. To implement the functionalities of a class, you need to create an object of the class. We have used the preceding code snippet of C, to declare a class named info. To create an object named myInfo of the info class, the following code snippet is given in the Main() method:
Info myInfo = new Info();

Step 3: Accessing a class member
The member functions of the class are accessed through an object of the class by using the dot operator, as shown in the following code snippet:
myInfo.PrintDetails();

Here the PrintDetails() method is invoked by the myInfo object of the class Info. This function gets the id, name and mark from the user and displays the same, respectively.

Conclusion
A collection of classes is called a namespace. C# provides certain predefined set of classes and methods. The member function that is invoked from within the Main() method is defined under the public access modifier. Access modifiers are used to determine if any other class can access the variables or functions of a class. You will learn more about them in the subsequent chapters.