-2

I was looking at this question

Custom app.config section with a simple list of "add" elements

and I saw the following code for SwDevMan81 answer

 public class MyConfigInstanceCollection : ConfigurationElementCollection
 {
  public new MyConfigInstanceElement this[string elementName]
  {
     get
     {
        return this.OfType<MyConfigInstanceElement>().FirstOrDefault(item => item.Name == elementName);
     }
  }
}

and I cant really figure out what this is called.

It appears to be a property with a get accessor, but then it holds a new keyword in front of the object type. It appears to have an array indexer formed like method arguments. I tired looking at the ConfigurationElementCollection and ICollection but I didn't see anything that looked like this when I scanned the documentation.

What features is this ?

McFrank
  • 331
  • 3
  • 11
  • 2
    What feature is confusing you, exactly? The `new`? The indexer? – Blorgbeard Nov 27 '17 at 18:09
  • 1
    It's called an indexer. The [`new`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords) means that it's hiding the base class implementation. The base class `ConfigurationElementCollection` has it defined here (inherited from the ConfigurationElement class): [ConfigurationElementCollection.Item Property](https://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.item(v=vs.110).aspx) – Rufus L Nov 27 '17 at 18:19
  • @Blorgbeard it was the indexer, cant say i remember seeing it before in college and never messed with it before. Just when I thought I was getting kinda good at programming a simple concept humbles me – McFrank Nov 27 '17 at 20:03
  • @RufusL Thanks for the explanation of the new keyword as well that lead me down to understanding the hiding of the method and what was really going on If you make a answer i'll gladly accept it. I'm think with the msdn documentation you are trying to show me that is how the indexer are documented. I find that documentation a little confusing as I dont know the purpose of the Item word in the "Item[ConfigurationProperty]" part of it means. going to the ConfigurationElement documentation on it helped clarify it as a indexer. We can dive into it more if you wish. – McFrank Nov 27 '17 at 21:51
  • 1
    Cool, glad it helped! – Rufus L Nov 27 '17 at 21:52

1 Answers1

2

Thats an Indexer

Further reading: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/using-indexers

madoxdev
  • 3,770
  • 1
  • 24
  • 39