Count the interfaces...

Without running this code, what does this print?

public interface IStartable : IDisposable
{
   void Start();
}

public class Pipeline : IStartable
{ 
  // ...
}

Console.WriteLine(typeof(Pipeline).GetInterfaces().Length);

Print | posted on Thursday, January 10, 2008 10:25 PM

Feedback


Gravatar

# re: Count the interfaces... 1/10/2008 10:55 PM Eber Irigoyen

2...
what is the surprise?


Gravatar

# re: Count the interfaces... 1/10/2008 11:26 PM john

Something tells me its not the 1 IStartable interface...


Gravatar

# re: Count the interfaces... 1/10/2008 11:31 PM Avish

It's not always evident that GetInterfaces() gets all interfaces, including inherited ones (so the answer should be 2 unless IDisposable inherits another interface).
I only remember this because in Boo's type system, GetInterfaces() only returns the immediate interfaces. You know, just to spice up your development. :)


Gravatar

# re: Count the interfaces... 1/11/2008 1:03 AM Ayende Rahien

Eber, does it look like it is implementing two interfaces?
Take a look at the compiled IL,btw


Gravatar

# re: Count the interfaces... 1/11/2008 1:06 AM Eber Irigoyen

I don't know what the rules are, was just taking a logical (for me) guess, for me it does look like it implements 2 interfaces, you can cast that type to any of the two interfaces and pass it around


Gravatar

# re: Count the interfaces... 1/11/2008 1:11 AM Ayende Rahien

Eber, by that logic, GetBaseTypes() should return an array that contains all the inheritence tree.


Gravatar

# re: Count the interfaces... 1/11/2008 1:16 AM Michael Morton

From the help page for "Type.GetInterfaces Method", under "Return Value":

"An array of Type objects representing all the interfaces implemented or inherited by the current Type."


Gravatar

# re: Count the interfaces... 1/11/2008 1:25 AM Ayende Rahien

Interestingly, yes.
But take a look at the generated IL. Pipeline actually implements IDispoable directly.


Gravatar

# re: Count the interfaces... 1/11/2008 1:29 AM Eber Irigoyen

is it an optimization in this case?


Gravatar

# re: Count the interfaces... 1/11/2008 1:44 AM Michael Morton

What else would implement it?

An interface method cannot have an implementation so there is no way for IStartable to implement IDisposable, so it stands to reason that Pipeline would be providing the implementation for IDisposable, in some way or another.


Gravatar

# re: Count the interfaces... 1/11/2008 1:50 AM Andrey Shchekin

I think it is a simple solution to the following hierarchy:
IA
IB : IA
IC : IA
D : IB, IC

How many times should D implement methods of IA?
Once for each method.
Also, IB and IC can not implement or override a part of IA.

So it seems to be a simple solution for complex hierarchies.


Gravatar

# re: Count the interfaces... 1/11/2008 1:50 AM Andrey Shchekin

I think it is a simple solution to the following hierarchy:
IA
IB : IA
IC : IA
D : IB, IC

How many times should D implement methods of IA?
Once for each method.
Also, IB and IC can not implement or override a part of IA.

So it seems to be a simple solution for complex hierarchies.


Gravatar

# re: Count the interfaces... 1/11/2008 1:52 AM Andrey Shchekin

It is interesting that your blog always tells me that I am trying to post a comment twice, and sometimes it actually posts it twice.
Maybe there are both AJAX and postback postings at once?


Gravatar

# re: Count the interfaces... 1/11/2008 4:44 AM Brian

I agree with Michael Morton, what else do you have in the following code that would implement the inherited interface? An interface can't implement another interface, it can just inherit from it. So it is up to the object implementing the interface to also implement each inherited interface of the implemented interface. (that is hard to say).


Gravatar

# re: Count the interfaces... 1/11/2008 11:20 AM Stefan Wenig

> Eber, by that logic, GetBaseTypes() should return an array that contains all the inheritence tree.

sure, if such a method existed (which is not the case), you'd expect it to do just that. but not by analogy with GetInterfaces, but simply because there is no multiple inheritance, so that would be about the only way that the plural makes sense.


Gravatar

# re: Count the interfaces... 1/11/2008 11:57 AM Dave

"Without running this code, what does this print?"

Without running this code, nothing happens. It is essential that you run the code to make it do something :)


Gravatar

# re: Count the interfaces... 1/11/2008 12:05 PM Patrik

I think it makes perfect sense, I ask for the interfaces the class implements and I get the correct answer. It doesn't have anything to do with the fact that the class directly implements both interfaces, it's the fact that it DOES implement them. Look at the following code, the class B only directly implements ISerializable, but both IDisposable and ISerializable are returned by the GetInterfaces-method.

class Program
{
static void Main(string[] args)
{
Console.WriteLine(typeof(B).GetInterfaces().Length);
}
}

public class A : IDisposable
{
public void Dispose()
{

}
}

public class B : A, ISerializable
{

public void GetObjectData(SerializationInfo info, StreamingContext context)
{

}
}

Comments have been closed on this topic.