Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Indexers in C#

781 Aufrufe

Veröffentlicht am

Here, we will demonstrate the practical use of Indexers in C# Programming Language

Veröffentlicht in: Bildung
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Indexers in C#

  1. 1. Indexers are one flawless component of C#. They permit us to get to a specific class as it is an array. They are like the properties such that they typify or encapsulate the calls to the methods into more advantageous representation of quality accessing in setting the values. In order to declare a property, we can use it by the name. The indexer is no more dissimilar to class, so it exhibits a type, i.e. you need to pronounce a variable of its sort (by instantiating the object type). Indexers are likewise called smart arrays in C# and can be utilized to regard our object as an array(a type having elements of same type). An indexer permits a class instance or a struct to be indexed like the way through which the array is accessed by LOOPING etc
  2. 2. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IndexerExample { public class WelcomeMessage { public static string[] messageelements = new string[5]; public WelcomeMessage() { for (int i = 0; i < messageelements.Length; i++) messageelements[i] = String.Format("{0}", i); }
  3. 3. public string this[ int myIndexer] { get { string tmp; if (myIndexer >= 0 && myIndexer <= messageelements.Length - 1) tmp = messageelements[myIndexer]; else tmp = ""; return (tmp); } set { if (myIndexer >= 0 && myIndexer <= messageelements.Length - 1) messageelements[myIndexer] = value; } }}
  4. 4. public class Program { public static void Main() { WelcomeMessage elements = new WelcomeMessage(); elements[0] = "Hello! "; elements[1] = "How "; elements[2] = "are "; elements[3] = "you "; elements[4] = "today?"; for (int i = 0; i < WelcomeMessage.messageelements.Length; i++) Console.Write(elements[i]); Console.ReadLine(); } } }
  5. 5. Hello! How are you today?