How does IDataErrorInfo.this[string propertyName] work in C#?
Asked Answered
T

2

5

I've always implemented the IDataErrorInfo interface without actually wondering what this line means and how it works.

string IDataErrorInfo.this[string propertyName]
{
    get { return this.GetValidationError(propertyName); }
}

How does .this[string propertyName] work, and when/how does this property get called?

Toreador answered 21/6, 2013 at 13:27 Comment(3)
I think this is explicit interface implementation of an indexer and it would be called whenever you have an explicitly typed IDataErrorInfo object where you write: string myPropertyError = myDataErrorInfo["SomePropertyName"];Saltzman
Yup, it's just explicit interface implementation. Are you aware of that in general? (If not, just look it up.)Mastrianni
@JonSkeet I have a rough idea of what it means to implement an interface explicitly (I asked on Programmers.SE about that a while back), but the .this[string propertyName] was the main part I didn't quite understand.Toreador
K
3

this[key] is in fact an indexer, and is somewhat of a cross between a property and a method. It acts like a property since you can bind to it, but as opposed to regular properties, it receives a parameter.

Behind the scenes it's implemented as a method - get_Item(key), and if you'd want to access it via reflection you'd need to use Item for a name. For example:

typeof(MyClass).GetProperty("Item");

This is also important to know when implementing INotifyPropertyChanged, in which case, "Item[]" or Binding.IndexerName should be used as a property name in order to update the UI.

Knickers answered 21/6, 2013 at 13:45 Comment(0)
S
8

This is explicit interface implementation of an indexer. (EDIT: The IDatatErrorInfo. part of the signature signifies the explicit interface implementation, and the .this[...] part signifies an indexer.)

It would be called whenever you have an explicitly typed IDataErrorInfo object and you use square brackets on it to retrieve/get a value while passing a string in. For example:

IDataErrorInfo myDataErrorInfo = GetErrorInfo();
string myPropertyError = myDataErrorInfo["SomePropertyName"];

Note that since it's an explicit interface implementation, it will only be accessible when the type is known exactly as an IDataErrorInfo. If you have it typed as your subclass, it won't be accessible unless that class exposes it:

MyDataErrorInfoImpl myDataErrorInfo = GetErrorInfo();
string myPropertyError = myDataErrorInfo["SomePropertyName"]; //compiler error!
Saltzman answered 21/6, 2013 at 13:35 Comment(3)
I'm sorry I wasn't very clear in my post. I was more interested in finding out how .this[string propertyName] works, which you mentioned was an indexer. I'm looking that up now :) I do understand implicit vs explicit interfaces.Toreador
@Toreador Ahh, I've edited my answer to be clear which part of the signature refers to the explicit interface implementation and the indexer. EDIT: Indexers essentially let you implement array-style square bracket syntax but let you use any type besides just int. They behave just like properties/methods and follow the same rules for overloading/overriding/new/virtual/abstract/etc.Saltzman
Thank you. I was having trouble googling variations of "what does .this[string propertyName] mean" to come up with what it actually is. Now that I know it's referred to as an indexer, I am having a much easier time finding material about it :)Toreador
K
3

this[key] is in fact an indexer, and is somewhat of a cross between a property and a method. It acts like a property since you can bind to it, but as opposed to regular properties, it receives a parameter.

Behind the scenes it's implemented as a method - get_Item(key), and if you'd want to access it via reflection you'd need to use Item for a name. For example:

typeof(MyClass).GetProperty("Item");

This is also important to know when implementing INotifyPropertyChanged, in which case, "Item[]" or Binding.IndexerName should be used as a property name in order to update the UI.

Knickers answered 21/6, 2013 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.