Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Properties and Indexers in C# (OOP’s) – Part 8

Posted by Ravi Varma Thumati on July 13, 2009

Properties and Indexers in C#

Properties are known as smart fields and enable access to the fields of a class. Indexers are also called smart arrays in C# and can be used to use an object as an array. This article discusses both of these concepts with simple code examples.


Property

A property is like a “virtual field” that contains get and set accesses and provides an interface to the members of a class. They can be used to get and set values to and from the class members. Properties can be static or instance members of a class. The get access does not accept any parameter; rather it returns the value of the member that it is associated with. The set access contains an implicit parameter called ‘value’.

C# provides a built in mechanism called properties to do the above. In C#, properties are defined using the property declaration syntax. The general form of declaring a property is as follows.

<acces_modifier> <return_type> <property_name>
{
get
{
}
set
{
}
}

Where <access_modifier> can be private, public, protected or internal. The <return_type> can be any valid C# type. Note that the first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor.

The following code example illustrates a simple property.

Implementing properties

using System;

namespace ConsoleApplication

{

class Test

{

private int number;

public int MyProperty

{

get

{

return number;

}

set

{

number = value;

}

}

[STAThread]static void Main(string[]args)

{

Test test = new Test();

test.MyProperty = 100;

Console.WriteLine(test.MyProperty);

Console.ReadLine();

}

}

}

The following code shows a static property. The static property MyProperty can be accessed without instantiating the class. A static property can access static members of a class only.

Static Properties

C# also supports static properties, which belongs to the class rather than to the objects of the class. All the rules applicable to a static member are applicable to static properties also.

The following program shows a class with a static property.

//C# : static Property
using System;
class MyClass
{
private static int x;
public static int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 10;
int xVal = MyClass.X;
Console.WriteLine(xVal);//Displays 10
}
}

Remember that set/get accessor of static property can access only other static members of the class. Also static properties are invoking by using the class name.

Properties & Inheritance

The properties of a Base class can be inherited to a Derived class.

//C# : Property : Inheritance
//Author: rajeshvs@msn.com
using System;
class Base
{
public int X
{
get
{
Console.Write(“Base GET”);
return 10;
}
set
{
Console.Write(“Base SET”);
}
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.X = 10;
Console.WriteLine(d1.X);//Displays ‘Base SET Base GET 10’
}
}

The above program is very straightforward. The inheritance of properties is just like inheritance any other member.

Properties & Polymorphism

A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.

//C# : Property : Polymorphism
//Author: rajeshvs@msn.com
using System;
class Base
{
public virtual int X
{
get
{
Console.Write(“Base GET”);
return 10;
}
set
{
Console.Write(“Base SET”);
}
}
}
class Derived : Base
{
public override int X
{
get
{
Console.Write(“Derived GET”);
return 10;
}
set
{
Console.Write(“Derived SET”);
}
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays ‘Derived SET Derived GET 10’
}
}

Abstract Properties

A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.

If the abstract class contains only set accessor, we can implement only set in the derived class.

The following program shows an abstract property in action.

//C# : Property : Abstract
//Author: rajeshvs@msn.com
using System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(” GET”);
return 10;
}
set
{
Console.Write(” SET”);
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays ‘SET GET 10’
}
}

The properties are an important features added in language level inside C#. They are very useful in GUI programming. Remember that the compiler actually generates the appropriate getter and setter methods when it parses the C# property syntax.

Restricting the visibility

public class Employee

{

private int empID;

private string empName;

internal int ID

{

get

{

return empID;

}

set

{

empID = value;

}

}

internal string Name

{

get

{

return empName;

}

set

{

empName = value;

}

}

}

public class Test

{

public static void Main(string[]args)

{

Employee emp = new Employee();

emp.Name = “Joydip”;

emp.ID = 259;

System.Console.WriteLine(“The id is:” + emp.ID);

System.Console.WriteLine(“The name is:” + emp.Name);

}

}

The keyword “internal” implies that the property’s ID and Name can be accessed from the current assembly only as they are “internal” to the assembly in which they have been declared.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: