Interface

T

totto

Hi,
Can anyone please explain to me how it's possible to make an object from an
Interface.
I thought Interfaces was just for inheritance, but in the following code I
have made an instance of a Interface, myEnumerator.
The code unchecks all checked items in a checkedListBox.

IEnumerator myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
while(myEnumerator.MoveNext())checkedListBox1.SetItemChecked((int)myEnumerator.Current,false);

Best regards
Totto
 
J

Jon Skeet [C# MVP]

totto said:
Can anyone please explain to me how it's possible to make an object from an
Interface.
I thought Interfaces was just for inheritance, but in the following code I
have made an instance of a Interface, myEnumerator.
The code unchecks all checked items in a checkedListBox.

IEnumerator myEnumerator =
checkedListBox1.CheckedIndices.GetEnumerator();

You haven't so much got an instance of IEnumerator as an instance of a
type which implements IEnumerator.

Think of it a bit like System.Object. You can do:

object x = new MyType();

but that doesn't create an instance of just plain System.Object - it
creates an instance of MyType, which derives from (eventually)
System.Object.
 
G

Guest

Hi Totto,

You actually do not create an instance of an interface, you create an
instance of an object that implements the interface. After you create an
instance of this object you can return the object by its interface.

HTH,

Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top