How do we create a class?
The procedure of creating a class in OOP is explained
A class is defined using the keyword `class'. The syntax is -
access-modifier class classname
{
define properties and functions of the class here
}
For example -
public class Person
{
}
In the above example, a class named `Person' is created. The keyword `public' is the access-modifier. The `public' keyword indicates that there is no restriction on accessing this class. Other access-modifiers are - private, protected, internal and protected internal. However, we will stick with `public' access-modifier, as we want our classes to be accessed without restriction.
The class that we have defined is an empty class. It does not contain anything. We will now add some fields to this class.
public class Person
{
private string name;
private int age;
private string gender;
}
We have added some variables inside the class. They are known as the fields. Fields store the data for the class. But there is one problem. Notice the keyword `private' in every field declaration. This implies that we cannot access these variables directly in our application. In other words, we cannot use them in our code with statements like:
age=32;
Why have we made them private? How can we access them for storing data? One way to access them is to make them public, i.e. use the keyword `public' in place of `private'. But the principles of object-oriented programming suggest that the fields of a class should be private. So what is the solution?
The solution is to use Properties. Let us modify the class:
public class Person
{
private string name;
private int age;
private string gender;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
We have added a property to the class called `Name'. This property is used to store value in the `name' field. Note that it starts with a capital `N' while the field `name' starts with a small `n'. In naming fields, it is customary to begin the name with a lowercase first letter and the properties with an uppercase first letter. Since C# is a case sensitive language, `name' and `Name' are two different entities.
The property is public and can therefore be accessed by our application. Inside the property, we have a `get' block and a `set' block. The `set' block is used to store values in the fields. The statement:
name= value; will store a value in the field `name'.
The `get' block returns the value stored in a field. In our example, it will return the value stored in the `name' field.
We will add two more properties. One for the `age' field and one for the `gender' field:
public class Person
{
private string name;
private int age;
private string gender;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Gender
{
get
{
return gender;
}
set
{
gender=value;
}
}
public int Age
{
get
{
return age;
}
set
{
age=value;
}
}
}
Now our class has three properties to access the fields.
We will now use this `Person' class in a program to see how to implement classes in Visual C# 2005:
- Start a new console application project. In the solution explorer, right click on the project name and select - Add Class...
- In the Add New Item dialog box, select `Class' in the templates section. Provide a name to the class (we have used `Person' in this example). Do not change the extension name. it should be .cs. Click on the Add button to continue.
- A new tab will be added in the IDE. The name of the tab is the name of our class (Person.cs in our example). This tab contains a structure for the class we are going to create.
- Inside the braces of the class declaration, type the code for our person class:
public class Person { private string name; private int age; private string gender;
public string Name { get { return name; } set { name = value; } }
public string Gender { get { return gender; } set { gender = value; } } public int Age { get { return age; } set { age = value; } } }
We now have a class called `Person' in our program. The next step is to use this class.
- Switch to the program file (Program.cs) by clicking on its tab in the IDE. Type the following code inside the Main() method:
class Program { static void Main(string[] args) { Person p1 = new Person(); Person p2 = new Person();
Console.WriteLine("Enter name:"); p1.Name = Console.ReadLine();
Console.WriteLine("Enter Gender:"); p1.Gender = Console.ReadLine();
Console.WriteLine("Enter Age:"); p1.Age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter name:"); p2.Name = Console.ReadLine();
Console.WriteLine("Enter Gender:"); p2.Gender = Console.ReadLine(); Console.WriteLine("Enter Age:"); p2.Age = int.Parse(Console.ReadLine()); Console.WriteLine("{0} is {1} years old {2}", p1.Name, p1.Age, p1.Gender); Console.WriteLine("{0} is {1} years old {2}", p2.Name, p2.Age, p2.Gender); } }
In our code, we have first declared two objects of the class `Person', p1 and p2. Then, using the properties of the class, we have first received the values (name, gender and age) for both p1 and p2 and then displayed the same. Since we have declared the field's name, gender and age as private in our class definition, we cannot access them directly from the Main() method. However, using the properties of the class, we can read and write the field values.
The following figure shows a sample run of the program -



