C# - Indexers Using String as an Index Last Updated : 08 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: Properties in C# Indexers allow instances of a class or struct to be indexed just like arrays. By using indexers class will behave like a virtual array. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters. Indexers are almost similar to the Properties. The main difference between Indexers and Properties is that the accessors of the Indexers will take parameters. Indexers using a string as an index: This method will give you more information, readability. If we want to retrieve information using a string as an index. Example: ic["username"] = "user12"; ic["password"] = "12345"; This will give more readability than the code given below. ic[0] = "user12"; ic[1] = "12345"; Syntax: [access_modifier] [return_type] this [argument_list] { get { // retrieval code or code to get the value } set { // code for setting value to member } In the above syntax: access_modifier: It can be public, private, protected, or internal.return_type: It can be any valid C# type. In this case, it will be of string type.this: It is the keyword which points to the object of the current class.argument_list: This specifies the parameter list of the indexer.get{ } and set { }: These are the accessors. Example: C# // C# program to illustrate the Indexers // Using String as an Index using System; // class declaration class IndexerPOC { // class members private string[] val = new string[4]; // indexer array private string[] indices={"username","password","email","Book"}; // Indexer declaration // Here public is the modifier // string is the return type of // Indexer and "this" is the keyword // which refer to the calling object. // having parameters list public string this[string index] { // get Accessor // retrieving the values // stored in val[] array // of strings using string indexer. get { return val[Array.IndexOf(indices,index)]; } // set Accessor // setting the value at // passed i of val set { // value keyword is used // to define the value // being assigned by the // set indexer. val[ Array.IndexOf(indices,index)] = value; } } } // Driver Class class Program { // Main Method public static void Main() { // creating an object of parent class which // acts as primary address for using Indexer IndexerPOC ic = new IndexerPOC(); // Inserting values in ic[] // Here we are using the object // of class as an array ic["username"] = "user12"; ic["password"] = "12345"; ic["email"] = "[email protected]"; ic["Book"]="CSHARP"; Console.Write("Printing values stored in objects used as arrays\n"); // printing values Console.WriteLine("UserName = {0}", ic["username"]); Console.WriteLine("Password = {0}", ic["password"]); Console.WriteLine("Email = {0}", ic["email"]); Console.WriteLine("Book = {0}", ic["Book"]); } } Output: Printing values stored in objects used as arrays UserName = user12 Password = 12345 Email = [email protected] Book = CSHARP Comment More infoAdvertise with us Next Article C# - Overloading of Indexers M mvikas886 Follow Improve Article Tags : C# CSharp-Indexers & Properties Similar Reads C# String.IndexOf( ) Method | Set - 1 In C#, the IndexOf() method is a String method. Used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found. This method can be overloaded by passing different pa 6 min read C# | Arrays of Strings An array is a collection of the same type variable. Whereas a string is a sequence of Unicode characters or array of characters. Therefore arrays of strings is an array of arrays of characters. Here, string array and arrays of strings both are same term. For Example, if you want to store the name of 4 min read C# | Index of first occurrence in StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.IndexOf(String) method is used to search the specified string which returns the ze 2 min read C# - Overloading of Indexers In C#, Indexer allows an instance of a class or struct to be indexed as an array. When an indexer is defined for a class, then that class will behave like a virtual array. It can be overloaded. C# has multiple indexers in a single class. To overload an indexer, declare it with multiple parameters an 3 min read C# String Properties In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the string class:Chars[Int32]: Used to get the Char object at a spec 4 min read C# String Class In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c 9 min read Like