In one of our previous tutorials Running the first C# program, we had explained how you could create a simple class in C#. In this tutorial, we will explain
how to create a class with its important components like instance variable and methods and how we can access them.
Important features of a class:
A C++ program may or may not contain a class.
A C++ class may have zero or more data members such as, instance variables,
which represent the class data.
A C++ class may have zero or more methods.
A class describes its behaviour through its methods,
where a method is a place where the logic of a class is stored, and it operates on the class data.
Advertisement
Creating a class with instance variable and method
Let's us create a class with its important components
such as, its instance variable, method and then call these components, by creating an object of the class.
A.cs
//C# Example of a class with instance variables and method
//and we are going create an object of a class.
using System;
class A
{
//Instance variable
public int a = 10; //declaring a data member of class named 'a' of type int
//Definition of the method Message() of class A, which returns void
public void Message()
{
Console.WriteLine("Hello from the class A");
}
//Defining the method Main()
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Accessing the value of instance variable, a, of the object of class A
//By using the dot operator
Console.WriteLine("The value of instance variable, a : " + ob.a);
//Calling the method Message() of the object of class A
//By using the dot operator
ob.Message();
}
}
Output
The value of instance variable, a : 10
Hello from the class A
Understanding the program
Most of you who have already read our tutorial Running the first C# program
should be able to understand a lot about what's happening in the program, so let's discuss the new part by dividing it into a few segments -
A class with the instance variable and method
class A
{
//Instance variable
public int a = 10; //declaring a data member of class named 'a' of type int
//Definition of the method Message() of class A, which returns void
public void Message()
{
Console.WriteLine("Hello from the class A");
}
In the class A, we have defined two class members such as -
An instance variable named a, which is initialized to a value 10.
A
method named Message(),
This method has a return type void, which means that this method won't return any value after its execution.
Both class members are marked with public access modifier, which makes them accessible from the outside of this class(just by using the object of this class).
For more on access modifiers, please read class member access modifiers.
The access modifier is always specified before the return type of method and just like a class, each method of class is defined within a pair of curly braces { }.
Access the instance variable and method of a class from its Main() method
//Defining the method Main()
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Accessing the value of instance variable, a, of the object of class A
//By using the dot operator
Console.WriteLine("The value of instance variable, a : " + ob.a);
//Calling the method Message() of the object of class A
//By using the dot operator
ob.Message();
}
A C# program begins its execution with a call to the Main() method of the class,
because it is an entry point to our program and that's why the Main() method contains
the multiple lines of codes which calls the rest of the sections in the program.
This Main() method first creates an object of class A, by using the new keyword.
Next, we have used the created object of the class A with a dot operator
to access the value of its instance variable named a.
Using the same object of class A
with a dot operator, its associated method named Message() is called from within the Main() method.