Lecture #8 Properties, Indexers
Lecture #8 Properties, Indexers
• Properties
• Indexers
…
Introduction
• In the C# programming language, properties and indexers are two
key concepts.
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class
that is inherited from that class.
internal The code is only accessible within its own assembly, but
not from another assembly.
Internal -Access Modifiers
Overview
Introduction
Now we can access value of pi by using methods, where pi(data member) have restricted access
Properties : get and set accessor
public class test static void Main(string[] args)
{ {
private double pi = 3.14; test t = new test();
Console.WriteLine(t.piProperties);
public double piProperties
{ t.piProperties = 65.6;
get { return pi; }
set { pi = value; } Console.WriteLine(t.piProperties);
}
} }
}
Example
public class test
{
private double pi = 3.14;
static void Main(string[] args)
public double piProperties
{
{
test t = new test();
get {
if (pi <10)
Console.WriteLine(t.piProperties);
{return 0;
}
t.piProperties = 8.4;
return pi;
}
double data = t.piProperties;
set {
if (value > pi) {
Console.WriteLine(data);
pi = value;
}
}
}
}
Other data types
The data type of value will be implicitly changed as the data type of
property
Difference between methods and properties
Indexers
• An indexer allows us to access instances of a class using an index just like an array.
• In C#, we define an indexer just like properties using this keyword followed by [] index
notation.
For example,
public int this[int index] Here,
{
get
{ public - access modifier
return val[index]; int - return type of indexer
}
this - indicates we are defining indexer in current class
set int index - access values using integer index position
{
val[index] = value; get - ,method that returns values
} set - method that assigns values
}
Example – string
class MyClass
{
private string []data = new string[5]; MyClass mc = new MyClass();
mc[0] = "Ali";
public string this [int index] mc[1] = "Hamza";
{ mc[2] = "Javed";
get mc[3] = "Yasir";
{ mc[4] = "Zubair";
return data[index];
} for (int i = 0; i < 5; i++)
set {
{ Console.WriteLine("{0}", mc[i]);
data[index] = value; }
}
}
Example –int
class MyClass
{
MyClass mc = new MyClass();
private int[] data = new int[5];
mc[0] = 10;
mc[1] = 20;
public int this[int index]
mc[2] = 30;
{
mc[3] = 40;
get
mc[4] = 50;
{
return data[index];
for (int i = 0; i < 5; i++)
}
{
set
Console.WriteLine("{0}", mc[i]);
{
}
data[index] = value;
}
}
}
Difference
Properties Indexers
1. Properties are declared by giving a unique name. Indexers are declared without giving a name.
2. Properties are identified by the names While indexers are identified by the signatures.
3. Properties are invoked through a described name. Indexers are invoked using an index of the created object.
A get accessor of a property does not have any A get accessor of a property contains the list of same
5.
parameters. proper parameters as indexers.