Indexers in c#?
Answers were Sorted based on User's Feedback
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 are used to retrieve the data faster than
retrieving the data from Arrays.
| Is This Answer Correct ? | 37 Yes | 8 No |
Answer / muhammad usman
Indexer is used to treat an object as an array
| Is This Answer Correct ? | 29 Yes | 2 No |
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 |
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 |
Answer / dhara
indexer are the smart array in c# which retrive records
faster than simple array
| Is This Answer Correct ? | 9 Yes | 1 No |
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 |
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 |
What is the difference between values and reference types?
What is Lambda Expression?
how to store the value in textbox using delegates if we have two user control. the value will be called from one user control to another user control. Loading and unloading will be done through delegates.
HOw to judge a person for a team if you are allowed to ask only 2 questions to him?
can we assign null value to value type in c#?
12 Answers Idea, Microsoft, Wipro,
In a memory when you Box and Unbox a value-type what happens?
What is enum in c#?
What are the basics of c#?
What is private protected in c#?
Explain ACID rule of thumb for transactions.
Difference between a sub and a function in c#.
Hi to all..I have to create an intranet application on C#.NET windows Application so please please let can you people help me as iam new in .NET and if u have any samples or website address from where i can get sample please let know.
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)