-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 ?

5
  • 2
    What feature is confusing you, exactly? The new? The indexer? Commented Nov 27, 2017 at 18:09
  • 1
    It's called an indexer. The new 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 Commented Nov 27, 2017 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 Commented Nov 27, 2017 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. Commented Nov 27, 2017 at 21:51
  • 1
    Cool, glad it helped! Commented Nov 27, 2017 at 21:52

1 Answer 1

2

Thats an Indexer

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.