Explicit interface implementation
-
When I implement an interface explicitly like this
int IComparable.CompareTo(byte other) { ... }
I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
When I implement an interface explicitly like this
int IComparable.CompareTo(byte other) { ... }
I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
When I implement an interface explicitly like this
int IComparable.CompareTo(byte other) { ... }
I am unable to call it my self from another class, it does not show up in intellisence. Is this normal, if so why is it like this? I know that when I implement an interface like this is becomes explicitly public but I still cannot call it. This is for a value type also (I dont know if that makes any difference or not).█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
Explicit interface implementation allows you to implement an interface without adding public methods to your class. You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other) This is mainly used in two cases: - there is another public method that does the same job and should be called instead (FileStream.Close vs IDisposable.Dispose) - the method is expected to be only called by some other code that always operates on the interface and doesn't know about your class, and you don't want that the methods show when using your object. (often used with ICustomTypeDescriptor)
-
Explicit interface implementation allows you to implement an interface without adding public methods to your class. You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other) This is mainly used in two cases: - there is another public method that does the same job and should be called instead (FileStream.Close vs IDisposable.Dispose) - the method is expected to be only called by some other code that always operates on the interface and doesn't know about your class, and you don't want that the methods show when using your object. (often used with ICustomTypeDescriptor)
Daniel Grunwald wrote:
You will be able to call the method by casting an object instance to the interface type. ((IComparable)myInstance).CompareTo(other)
I remember now. I did this last night... IComparable test = this; test.[Intellisence found it]; It compiled without errors. Thanks for the information.
█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██