0% found this document useful (0 votes)
71 views8 pages

Indexer

An indexer in C# allows an object to be indexed like an array. Indexers must have at least one parameter and can be public, private, protected or internal. They are defined using a this[] accessor along with get and set blocks. Indexers can be inherited in derived classes and overridden using the virtual and override keywords. Abstract indexers define the interface without implementation, requiring subclasses to provide the get/set logic.

Uploaded by

gingeevimal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views8 pages

Indexer

An indexer in C# allows an object to be indexed like an array. Indexers must have at least one parameter and can be public, private, protected or internal. They are defined using a this[] accessor along with get and set blocks. Indexers can be inherited in derived classes and overridden using the virtual and override keywords. Abstract indexers define the interface without implementation, requiring subclasses to provide the get/set logic.

Uploaded by

gingeevimal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Indexer:

1.
2.
3.
4.
5.
6.
7.
8.
9.

The new concept in C# it is an object that acts as an array


It is an object that is to be indexed as an array
Indexer modifier can be private, protected, public or internal
The return type can be any valid C# data types
Indexers in C# must have atleast one parameter
Indexers are also known as smart arrays
Ref and Out parameters must not be specified
C# can overloaded just like member functions
Indexer cannot be static

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]);
}
}
}
}

Indexer and Inheritance


Just like other class members the indexers also can be inherited.
Example:
using
using
using
using

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#

1. Properties are also known as the smart fields in C#


2. They are the extensions of the C# data fields
3. In a class we declare the datafields as private and will provide the public SET/GET methods to
access the data fields

4. The access modifier can be private,public ,protected or internal


5. The return type can be any valid C# data type
Syntax of Properties
<acces_modifier> <return_type> <property_name>
{
get
{
}
set
{
}
}
Example:
using
using
using
using

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);
}
}
}

You might also like