Indexers in c#?

Answer Posted / jignesh contractor

Indexer Concept is object act as an array.
Indexer an object to be indexed in the same way as an
array.
Indexer modifier can be private, public, protected or
internal.
The return type can be any valid C# types.
Indexers in C# must have at least one parameter. Else the
compiler will generate a compilation error.
this [Parameter]

{

get

{

// Get codes goes here

}

set

{

// Set codes goes here

}

}



For Example:



using System;

using System.Collections.Generic;

using System.Text;



namespace Indexers

{

class ParentClass

{

private string[] range = new string[5];

public string this[int indexrange]

{

get

{

return range[indexrange];

}

set

{

range[indexrange] = value;

}

}

}



/* The Above Class just act as array declaration using
this pointer */



class childclass

{

public static void Main()

{

ParentClass obj = new ParentClass();



/* The Above Class ParentClass create one
object name is obj */



obj[0] = "ONE";

obj[1] = "TWO";

obj[2] = "THREE";

obj[3] = "FOUR ";

obj[4] = "FIVE";

Console.WriteLine("WELCOME TO Jigs World\n");

Console.WriteLine("\n");



Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}
\n", obj[0], obj[1], obj[2], obj[3], obj[4]);

Console.WriteLine("\n");

Console.WriteLine("Dont Fear Jigs is here\n");

Console.WriteLine("\n");

Console.ReadLine();

}

}

}

Is This Answer Correct ?    11 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is list passed by reference c#?

475


What is dictionary and hashtable in c#?

469


Give examples for reference types?

541


How can I get the ascii code for a character in c#?

568


How do switch statements work?

482






Are constructors inherited c#?

513


What is class in oops with example in c#?

509


What is a generic c#?

495


What is the function of .IsDescendent()?

580


What are expressions c#?

482


How is a loop recorder monitored?

507


What is the difference between system.string and system.text.stringbuilder classes?

518


How can I create image pieces/sub image?

488


what is inheritance and an example in vb.net and c# of when you might use it?

528


How many parameters can a method have c#?

486