1. Where are indexers used in .NET :
To store or retrieve data from session state or application state variables, we make use indexers.
Session["sessiondata"] = "sessiondata";
2. Defination :
An indexer allows an object to be indexed such as an array
Syntax :
A one or multi param indexer has the following syntax:
element-type this[int index, ..]
{
// The get accessor.
get
{
// return the value specified by index
}
// The set accessor.
set
{
// set the value specified by index
}
}
Points to remember:
1. We then created an indexer using "this" keyword. This indexer takes employeeId as parameter and returns employee name.
public string this[int employeeId]
2. Just like properties indexers have get and set accessors.
3. Indexers can also be overloaded.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Demo
{
public class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
}
public class Orgnization
{
private List lEmployees;
public Orgnization()
{
lEmployees = new List();
lEmployees.Add(new Employee { EmpId = 101, Name = "iliyas"});
lEmployees.Add(new Employee { EmpId = 102, Name = "patel"});
lEmployees.Add(new Employee { EmpId = 103, Name = "kabir"});
}
public string this[int empId]
{
get
{
return lEmployees.FirstOrDefault(emp => emp.EmpId == empId).Name;
}
set
{
lEmployees.FirstOrDefault(emp => emp.EmpId == empId).Name = value;
}
}
}
}
Using indexer:
Orgnization org = new Orgnization();
console.writeline(org[0]);
console.writeline(org[1]);
console.writeline(org[2]);
org[2] = "Name of employee updated here ";
3. Overloaded Indexers :
Indexers can be overloaded.
Example:
namespace Demo
{
public class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
public string Salarycode { get; set; }
}
public class Orgnization
{
private List lEmployees;
public Orgnization()
{
lEmployees = new List();
lEmployees.Add(new Employee { EmpId = 101, Name = "iliyas", Salarycode = "MP012"});
lEmployees.Add(new Employee { EmpId = 102, Name = "patel" , Salarycode = "MP032"});
lEmployees.Add(new Employee { EmpId = 103, Name = "kabir" , Salarycode = "MP022"});
}
public string this[string SaCode]
{
get
{
return lEmployees.FirstOrDefault(emp => emp.Salarycode == SaCode).Name;
}
set
{
lEmployees.FirstOrDefault(emp => emp.Salarycode == SaCode).Name = value;
}
}
}
}
Using indexer:
Orgnization org = new Orgnization();
console.writeline(org["MP012"]);
console.writeline(org["MP032"]);
console.writeline(org["MP022"]);
org[2] = "sal updated here ";
To store or retrieve data from session state or application state variables, we make use indexers.
Session["sessiondata"] = "sessiondata";
2. Defination :
An indexer allows an object to be indexed such as an array
Syntax :
A one or multi param indexer has the following syntax:
element-type this[int index, ..]
{
// The get accessor.
get
{
// return the value specified by index
}
// The set accessor.
set
{
// set the value specified by index
}
}
Points to remember:
1. We then created an indexer using "this" keyword. This indexer takes employeeId as parameter and returns employee name.
public string this[int employeeId]
2. Just like properties indexers have get and set accessors.
3. Indexers can also be overloaded.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Demo
{
public class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
}
public class Orgnization
{
private List
public Orgnization()
{
lEmployees = new List
lEmployees.Add(new Employee { EmpId = 101, Name = "iliyas"});
lEmployees.Add(new Employee { EmpId = 102, Name = "patel"});
lEmployees.Add(new Employee { EmpId = 103, Name = "kabir"});
}
public string this[int empId]
{
get
{
return lEmployees.FirstOrDefault(emp => emp.EmpId == empId).Name;
}
set
{
lEmployees.FirstOrDefault(emp => emp.EmpId == empId).Name = value;
}
}
}
}
Using indexer:
Orgnization org = new Orgnization();
console.writeline(org[0]);
console.writeline(org[1]);
console.writeline(org[2]);
org[2] = "Name of employee updated here ";
3. Overloaded Indexers :
Indexers can be overloaded.
Example:
namespace Demo
{
public class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
public string Salarycode { get; set; }
}
public class Orgnization
{
private List
public Orgnization()
{
lEmployees = new List
lEmployees.Add(new Employee { EmpId = 101, Name = "iliyas", Salarycode = "MP012"});
lEmployees.Add(new Employee { EmpId = 102, Name = "patel" , Salarycode = "MP032"});
lEmployees.Add(new Employee { EmpId = 103, Name = "kabir" , Salarycode = "MP022"});
}
public string this[string SaCode]
{
get
{
return lEmployees.FirstOrDefault(emp => emp.Salarycode == SaCode).Name;
}
set
{
lEmployees.FirstOrDefault(emp => emp.Salarycode == SaCode).Name = value;
}
}
}
}
Using indexer:
Orgnization org = new Orgnization();
console.writeline(org["MP012"]);
console.writeline(org["MP032"]);
console.writeline(org["MP022"]);
org[2] = "sal updated here ";
Comments
Post a Comment