Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use memberless interface?

What is the point of writing an interface without members ?

INamingContainer is one example in .NET Framework. And it's described in MSDN as :

Identifies a container control that creates a new ID namespace within a Page object's control hierarchy. This is a marker interface only.

Is it used for just this kind of blocks :

if (myControl is INamingContainer)
{
    // do something
}

Or are there other advantages of it ?

EDIT : It was called Marker Interface Pattern (thanks Preet)

like image 525
Canavar Avatar asked Mar 17 '09 07:03

Canavar


1 Answers

Memberless interfaces are used to provide mixin-like capabilities in C#. So given a class A:

class A : B { ... }

You can give it extra functionality (a-la multiple inheritance) by defining an interface IStuff:

interface IStuff {}

then 'implementing' it in A:

class A : B, IStuff { ... }

and then adding the extra features

class Methods {
  public static void Something(this IStuff stuff) {
    // some functionality here
  }
}
like image 65
Dmitri Nesteruk Avatar answered Nov 20 '22 07:11

Dmitri Nesteruk