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 |
Whats the use of string builder?
what is Diff Gram
What is addressof operator?
Why data types are important?
What is the use of base keyword? Tell me a practical example for base keyword’s usage?
how to retrive a TextBox value in to Sql database using C# windows form application coding
What is a event in c#?
How do you specify a custom attribute for the entire assembly (rather than for a class)?
What does the keyword virtual mean in the method definition?
What is action in c# 3.5?
if i used stored procedure for retrieving the data from sql server.in front end i had used data reader.when 100 records are there in table.when it has displayed ten records in frontend database has been collapsed.then where should our data available...
if we are updating a database using thread, and mean while application crashes or thread being aborted then what will happen in Database? Rollback or Database will be updated? Please explain with different scenario.
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)