Articles → CSHARP → Indexers In C#
Indexers In C#
- Introduction
- Syntax
- Creation of XML file
- Creating a class to implement an indexer
- Code explanation
- Reading the XML file
- Conclusion
Introduction
Syntax
<modifier> <return type > this[argument list] {
get {
// Get codes goes here
}
set {
// Set codes goes here
}
}
Creation Of XML File
<root>
<subject value = ".Net"></subject>
<subject value = ".Net Web Service"></subject>
<subject value = "ADO.NET"></subject>
<subject value = "IBM Lotus Notes"></subject>
<subject value = "JavaScript"></subject>
<subject value = "Silverlight"></subject>
<subject value = "Software Testing"></subject>
<subject value = "SQL Server"></subject>
<subject value = "WPF"></subject>
</root>
Creating A Class To Implement An Indexer
class Subject {
private Dictionary < string,
string > _subjectDictionary = new Dictionary < string,
string > ();
/// <summary>
/// Indexer
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this[int index] {
get {
string key = _subjectDictionary.Keys.Where((n, i) = >i == index).FirstOrDefault();
return string.Format("{0}", _subjectDictionary[key]);
}
}
/// <summary>
/// Get List of subjects
/// </summary>
private void GetListOfSubjects() {
DataSet ds = new DataSet();
ds.ReadXml("subject.xml");
for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++) {
_subjectDictionary.Add(i.ToString(), ds.Tables[0].Rows[i]["value"].ToString());
}
}
/// <summary>
/// Constructor
/// </summary>
public Subject() {
if (_subjectDictionary.Count == 0) {
GetListOfSubjects();
}
}
/// <summary>
/// Subject Count to save from
/// index out of range
/// </summary>
public int SubjectCount {
get {
return _subjectDictionary.Count;
}
}
}
Code Explanation
- First of all an object of type dictionary is created
private Dictionary<string,string> _subjectDictionary = new Dictionary<string,string>();
- A property "SubjectCount" is created to get the count of items in "_subjectDictionary". This property is optional but it is better to create it, because when you loop through "_subjectDictionary", then this property ensures that you won’t get any index out-of-range run time error. I will come back to this point when I will show the list of subjects in the "div" tag
- A method "GetListOfSubjects()" is created. The purpose of this method is to get a list of subjects from XML (It should be noted that I have ignored the validation part deliberately to make the code simple and easy to understand)
- A constructor to call the "GetListOfSubjects()" method
- Indexer is defined
public string this[int index] {
get {
string key = _subjectDictionary.Keys.Where((n, i) = >i == index).FirstOrDefault();
return string.Format("{0}", _subjectDictionary[key]);
}
}
Reading The Xml File
Subject oSubject = new Subject();
StringBuilder sbMenu = new StringBuilder(string.Empty);
sbMenu.Append("<ul id = \"MenuLeft\">");
for (int i = 0; i < oSubject.SubjectCount; i++) {
sbMenu.Append("<li>");
sbMenu.Append(oSubject[i].ToString());
sbMenu.Append("</li>");
}
sbMenu.Append("</ul>");
divLeftMenu.InnerHtml = sbMenu.ToString();
Conclusion