Understanding Classes, Methods Amp Properties using C#

 

A class is the fundamental topic to be discussed in any Object-Oriented Programming (OOP) language. This is true with C# also. Basically, we have to discuss the meaning of class with reference to an object also.

This is because these two words are more or less inter-related. A class is a combination of related objects whereas each object is an instance or a copy of the corresponding class.

From the figure given below (Figure 1), we can be able to judge the fact that Telephone is a combination of Sony, Ericcson, Panasonic and Siemens. Hence, Telephone is a class and the other four are its objects. Here, Compaq and HP can’t be an object of the class Telephone. In C#, a class is a user defined reference type. We create an object out of the classes by using the “new” keyword and by applying the general syntax as shown below:

Listing 1 

Classname objectname = new Constructor();

 

We will discuss about constructors later. Therefore, to create an object for our class Telephone, you have to code like this:

Listing 2

Telephone T= new Telephone();
 
<a href="http://www.learnxpress.com/en/images/2009/02/class-structure.jpg" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="" border="0" alt="" src="http://www.learnxpress.com/en/images/2009/02/class-structure-thumb.jpg" width="248" height="96"></a> 

To access any variable by using the above object name, we use the dot operator as shown in the listing below:

Listing 3

using&nbsp; System; 
class Access  
{ 
//Variables declared outside the main. 
int x = 100;  
int y = 200; 
public static void Main()&nbsp; 
{ 
//Object created 
Access a = new Access();   
//Instance variables called 
Console.WriteLine(a.x); 
Console.WriteLine(a.y); 
&nbsp;&nbsp;&nbsp; }   
}    

In the above piece of code, the variables are declared outside the main method. Hence, they are called as Instance Variables. You have to create an object only for accessing an Instance variable. The additional memory created out of the object creation are automatically destroyed by the C# Garbage Collector.

READ  What are Namespaces?

Methods

In this session, we will deal with Methods. The various types of methods will be discussed in detail.

Methods are blocks of code, which performs some kind of action, or carry out functions like printing, opening of a dialog box etc. There are two kinds of methods in C# as in Java. They are

Instance Method

Static Method

Let’s discuss each of these in detail. Instance Methods are methods declared outside the main method and can be accessed only by creating an object of the corresponding class. as shown in Listing 4:

Listing 4

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Instmethod 

{ 

//Method declared outside the main. 

void show() 

{ 

int x = 100; 

int y = 200; 

Console.WriteLine(x); 

Console.WriteLine(y); 

} 

public static void Main()

{ 

//Object created 

Instmethod a = new Instmethod (); 

//Instance method called 

a.show(); 

} 

} 

Class methods are also declared outside the main method but can be accessed without creating an object of the class. They should be declared with the keyword static and can be accessed using classname.methodname syntax. This is illustrated in Listing 5 below. Similarly, you can create class variables also.

Listing 5

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System;

class Statmethod 

{ 

//A class method declared 

static void show() 

{ 

int x = 100; 

int y = 200; 

Console.WriteLine(x); 

Console.WriteLine(y); 

} 

public static void Main()

{ 

// Class method called without creating an object of the class 

Statmethod.show(); 

} 

}   

Declaring Methods with Parameter

You can declare methods with a variable name or names as a parameter like as shown in the Listing 6 below:

READ  What do you mean by .NET Framework Class Libraries?

Listing 6

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">void display(int x) { }

At the time of accessing the method, you should pass a value to the same like as in the code snippet as shown below:

Listing 7

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">//object S created

S.display(50);


The complete code to illustrate the above concept is shown in Listing 8:

Listing 8

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System;

class Pmethod 

{ 

//Method declared with two parameters x and y of type integer 

void show(int x , int y) 

{ 

Console.WriteLine(x); 

Console.WriteLine(y); 

} 

public static void Main()

{ 

//Object created 

Pmethod a = new Pmethod (); 

//Method called by passing two integer values 

a.show(200,250); 

} 

}  


Method Overloading

In the above example, we declared a single method with a single parameter. But you can declare the signature of the same method once again in the same class but with different parameters. The parameters should have to be different. If not, the C# compiler would show errors. Listing 9 explains this concept clearly.

Listing 9

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Overloadmethod 

{ 

//Method declared with one integer parameter 

void show(int x) 

{ 

Console.WriteLine(x); 

} 

//Method declared with two integer parameters 

void show(int a, int b) 

{ 

Console.WriteLine(a); 

Console.WriteLine(b); 

} 

public static void Main()

{ 

//Object created 

Overloadmethod a = new Overloadmethod (); 

//Method’s called by passing respective values 

a.show(100); 

a.show(300,500); 

} 

} 

Properties

Properties provide added functionality to the .NET Framework. Normally, we use accessor methods to modify and retrieve values in C++, Visual Basic. If you had programmed using Visual Basic’s ActiveX technology, then this concept is not new for you. Visual Basic extensively uses accessor methods like getXXX() and setXXX() methods for creating user defined properties.

READ  Azure Media Player - Updates and Support Avenue Now Available

A C# property consists of

1. Field declaration

2. Accessor Methods (getter and setter methods).

Getter methods are used to retrieve the fields value and setter methods are used to modify the fields value. C# uses a special Value keyword to achieve this. Listing 10 declares a single field named zipcode and shows how to apply the field in a property.

Listing 10

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Propertiesexample 

{ 

//Field "idValue" declared 

public string idValue; 

//Property declared 

public string IdValue 

{ 

get 

{ 

return idValue; 

} 

set 

{ 

idValue = value; 

} 

} 

public static void Main(string[] args) 

{ 

Propertiesexample pe = new Propertiesexample(); 

pe.IdValue = "009878"; 

string p = pe.IdValue; 

Console.WriteLine("The Value is {0}",p); 

} 

} 

If you look at the MSIL generated by the above code, you can be able to view two methods like:

Listing 11

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">Address::get_Zip() and Address::set_Zip()

 

We have reproduced the code for your reference. On careful scrutiny, the set_zip() method takes a string argument. This is because we have passed a string value while accessing the property. It’s illegal to call these methods directly in a C# program. In the above code, the Address.zipcode property is considered as read and write property because both getter and setter methods are defined. If you want to make the property read only, omit the set block and for making write only, omit the get block.

Leave a Reply

Your email address will not be published.