Example 7 24 a custom indexer class indexable public

Info icon This preview shows pages 271–273. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 7-24. A custom indexer class Indexable { public string this[int index] { get { return "Item " + index; } set { Console.WriteLine("You set item " + index + " to " + value); } } } This has the get and set parts we’d expect in a normal property, but the definition line is a little unusual: it starts with the accessibility and type as normal, but where we’d expect to see the property name we instead have this[int index] . The this keyword signifies that this property won’t be accessed by any name. It is followed by a parameter list enclosed in square brackets, signifying that this is an indexer property, defining what should happen if we use the square bracket element access syntax with objects of this type. For example, look at the code in Example 7-25 . Example 7-25. Using a custom indexer Indexable ix = new Indexable(); Console.WriteLine(ix[10]); ix[42] = "Xyzzy"; After constructing the object, the next line uses the same element access syntax you’d use to read an element from an array. But this is not an array, so the C# compiler will look for a property of the kind shown in Example 7-24 . If you try this on a type that doesn’t provide an indexer, you’ll get a compiler error, but since this type has one, that ix[10] expression ends up calling the indexer’s get accessor. Similarly, the third line has the element access syntax on the lefthand side of an assignment, so C# will use the indexer’s set accessor. List<T> | 247
Image of page 271

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
If you want to support the multidimensional rectangular array style of index (e.g., ix[10, 20] ), you can specify multiple parameters between the square brackets in your indexer. Note that the List<T> class does not do this—while it covers most of the same ground as the built-in array types, it does not offer rectangular multidimensional behavior. You’re free to create a jagged list of lists, though. For example, List<List<int>> is a list of lists of integers, and is similar in use to an int[][] . The indexer in Example 7-24 doesn’t really contain any elements at all—it just makes up a value in the get , and prints out the value passed into set without storing it any- where. So if you run this code, you’ll see this output: Item 10 You set item 42 to Xyzzy It may seem a bit odd to provide array-like syntax but to discard whatever values are “written,” but this is allowed—there’s no rule that says that indexers are required to behave in an array-like fashion. In practice, most do—the reason C# supports indexers is to make it possible to write classes such as List<T> that feel like arrays without nec- essarily having to be arrays. So while Example 7-24 illustrates that you’re free to do whatever you like in a custom indexer, it’s not a paragon of good coding style. What does any of this have to do with value types and immutability, though? Look at Example 7-26 . It has a public field with an array and also an indexer that provides access to the array.
Image of page 272
Image of page 273
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Sign up to access 24/7 study resources for your classes

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern