Advertisement



< Prev
Next >



C++ Class




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:


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 -

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement