Adam Vandenberg ([info]piehead) wrote,
@ 2005-11-11 12:52:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:c#, dotnet, programming

Explicit Interfaces in C#
C# lets a class implement an interface in two ways: the normal way, and "explicitly".

Explicit interfaces exist in the language so that a class can implement two separate interfaces even if both interfaces have a method with the same name.

Because of the "same name" issue, you can't call explicitly implemented methods directly on the class; you have to cast the class to the interface first. The manifests in the IDE in IntelliSense as explicitly implemented methods not showing up in the auto-complete list.

The C# langauge has array classes, like "string[]" and "MyClass[]". Behind the scenes these inherit from the .NET array class "Array". Array implements IList, but does so explicitly.

I hadn't made the mental connection until now, so I had been testing membership in an array such as:
string[] things = new string[]{"abc", "def", "ghi", "jkl"};

with code like:
bool found = -1 < Array.IndexOf(things, "abc");

Turns out this works too:
bool found = ((IList)things).Contains("abc");

Too bad it's also ugly, but at least that "-1" isn't hanging around.




Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…