Indexer
Indexer
1.
2.
3.
4.
5.
6.
7.
8.
9.
Syntax:
<modifier> <return type> this [argument list]
{
get
{
// Get codes goes here
}
set
{
// Set codes goes here
}
}
Example:
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace IndexerDemo
{
class ParentClass
{
private string[] range = new string[5];
public string this[int index]
{
get
{
return range[index];
}
set
{
range[index] = value;
}
}
}
class ChildClass
{
static void Main(string[] args)
{
ParentClass pc = new ParentClass();
pc[0] = "Mandala";
pc[1] = "Vivek";
pc[2] = "Ananda";
pc[3] = "Swamy";
pc[4] = "MVS";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(pc[i]);
}
}
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace IndexerDemo
{
class IndexerClass
{
private string[] range = new string[5];
public string this[int index]
{
get
{
return range[index];
}
set
{
range[index] = value;
}
}
}
class IndexerDemo1 : IndexerClass
{
}
class IndexerTest
{
static void Main(string[] args)
{
IndexerDemo1 id = new IndexerDemo1();
id[0] = "Mandala";
id[1] = "Vivek";
id[2] = "Ananda";
id[3] = "Swamy";
id[4] = "MVS";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(id[i]);
}
}
}
}
Indexers can be overridden in the derived class but you must use the virtual and
override keywords in order to override the indexer in the derived clas
Example:
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace IndexerDemo
{
class BaseClass
{
private string[] b = new string[5];
public virtual string this[int index]
{
get
{
return b[index];
}
set
{
b[index] = value;
}
}
}
class DerivedClass:BaseClass
{
private string[] d = new string[10];
public override string this[int index]
{
get
{
return d[index];
}
set
{
d[index] = value;
}
}
}
class IndexerTest
{
static void Main(string[] args)
{
BaseClass dc = new DerivedClass();
dc[0] = "12";
dc[1] = "23";
dc[2] = "34";
dc[3] = "45";
dc[4] = "56";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(dc[i]);
}
}
}
}
Abstract Indexers
The Indexers in the class must be declared by the keyword Abstract.The abstract indexer does not
contain any code.If the indexer contains only set or get then the derived class can implement only the
set or get.
Example:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IndexerDemo
{
abstract class AbstractIndexer
{
private string[] a = new string[5];
public abstract string this[int index]
{
get;
set;
}
}
class DerivedClass : AbstractIndexer
{
private string[] b = new string[5];
public override string this[int index]
{
get
{
return b[index];
}
set
{
b[index] = value;
}
}
}
class IndexerTest
{
static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc[0] = "132";
dc[1] = "34";
dc[2] = "43";
dc[3] = "46";
dc[4] = "87";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(dc[i]);
}
}
}
}
Properties in C#
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace PropertiesDemo
{
class PropertyDemo
{
private int x;
public PropertyDemo(int i)
{
x = i;
}
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
class PropertyTest
{
static void Main(string[] args)
{
PropertyDemo pd = new PropertyDemo(40);
Console.WriteLine("pd.x=" + pd.X);
}
}
}
C# supports static properties.All rules applicable to static members are also applicable to static
properties also
Properties and Inheritence
The properties of the base class can be inherited to the derived class
Example:
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace PropertiesDemo
{
class PropertyBase
{
public virtual string X
{
get
{
return "Base Class";
}
}
}
class PropertyDerived : PropertyBase
{
public override string X
{
get
{
return "Derived Class";
}
}
}
class PropertyTest
{
static void Main(string[] args)
{
PropertyBase pb = new PropertyDerived();
Console.WriteLine("pd.x=" + pb.X);
}
}
}
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
Example:
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace PropertiesDemo
{
abstract class PropertyBase
{
public abstract string X
{
get;
}
}
class PropertyDerived : PropertyBase
{
public override string X
{
get
{
return "Derived Class";
}
}
}
class PropertyTest
{
static void Main(string[] args)
{
PropertyBase pb = new PropertyDerived();
Console.WriteLine("pd.x=" + pb.X);
}
}
}