Indexers in c#?

Answers were Sorted based on User's Feedback



Indexers in c#?..

Answer / supraja

C# introduces a new concept known as Indexers which are
used for treating an object as an array. The indexers are
usually known as smart arrays in C# community. Defining a
C# indexer is much like defining properties. We can say
that an indexer is a member that enables an object to be
indexed in the same way as an array.

The following is syntax

<modifier> <return type> this [argument list]
{
get
{
// Get codes goes here
}
set
{
// Set codes goes here
}
}

Is This Answer Correct ?    37 Yes 1 No

Indexers in c#?..

Answer / chaitanya.k

Indexers are used to retrieve the data faster than
retrieving the data from Arrays.

Is This Answer Correct ?    37 Yes 8 No

Indexers in c#?..

Answer / muhammad usman

Indexer is used to treat an object as an array

Is This Answer Correct ?    29 Yes 2 No

Indexers in c#?..

Answer / 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

Indexers in c#?..

Answer / srinath thanuku

Sample code to use Indexers when there are more than one
variable in a Class.

class IndexerExample
{
private string[] SkillSet = new string[5];
private string userName;
private int salary;
public string this[int indexrange]
{
set
{
SkillSet[indexrange] = value;
}
get
{
return SkillSet[indexrange];
}
}
public int sal
{
get
{
return salary;
}
set
{
salary = value;
}
}
public string name
{
get
{
return userName;
}
set
{
userName = value;
}
}

}

Creating an Object of the Above Class:
IndexerExample obj = new IndexerExample();
obj[0] = "C#";
obj[1] = "VB.Net";
obj[2] = "ASP.Net";
obj[3] = "SQL Server 2005";
obj[4] = "JScript.Net";
obj.sal = 1000;
obj.name ="Madhavi K.";

Is This Answer Correct ?    14 Yes 4 No

Indexers in c#?..

Answer / dhara

indexer are the smart array in c# which retrive records
faster than simple array

Is This Answer Correct ?    9 Yes 1 No

Indexers in c#?..

Answer / srinivasu

1.Indexer is a concept of using an object like an array.
2.indexers are similar to properties
3.Indexer is a collection of SET and GET methods.
4.Indexer name must be "this".
5.One class can have only one indexer.

syntax:
------

properties:
-----------
string s;
public string Pname
{
set { s=value;}

get { return s;}
}


Indexers:(example)
---------

class test
{
string [] x=new string[3];
public string this [int i]
{
set {x[i]=value;}

get {return x[i];}

}

public void print()
{
for(int i=0;i<x.lenght;i++)
messageBox.show(x[i]);

}

}
//using

test t=new test();
t[0]="vasu";
t[1]="liki";
t[2]="kavi";
t.print();

Is This Answer Correct ?    16 Yes 11 No

Indexers in c#?..

Answer / rajkumar

hi..all you said in the above example is by considering
only 1 variable in a class i.e., string. if suppose i have
multiple variables/members in class how can use indexer
conscept.

i think u got my question. ur only assing 1 value through
object. what if i had other variables in class.??

please clarify my doubt. very urgent

Is This Answer Correct ?    4 Yes 3 No

Post New Answer

More C Sharp Interview Questions

Does c# replace c++?

0 Answers  


What are the problem with .NET generics?

0 Answers   DELL,


What are anonymous methods ? why these methods are used and in what condition these methods are useful ?

1 Answers  


Why is main static in c#?

0 Answers  


Why do we need singleton pattern in c#?

0 Answers  






What is dataset c#?

0 Answers  


may we achieve polyphormsm through overloading a funtion?Is it right or wrong concept because i read polyphormism can be achieved through overloading?plz help me thnks

1 Answers  


What is the difference between C# 3.5 and C# 4.0?

0 Answers  


Can constructor be protected?

0 Answers  


What is a helper method in c#?

0 Answers  


What is Serialization and how do we implement (in real time scenario)

1 Answers  


What is winforms c#?

0 Answers  


Categories