.NET Framework Bookmark and Share   
 index > .NET Base Class Library > hot to constraint a subtype to implement a generic interface
 

hot to constraint a subtype to implement a generic interface

i, this is my question:

i have an interface or an abstract class, i want that all the derived type implements for example IEquatable<T>
where T is the subtype, can i do that? Or have somebody another solution?

//pseudo code
IMyInterface : IEquatable<T> (where T is the subtype) so

Object1 : IMyInterface
{
...
Equals(Object1 o) {}
}

Object2 : IMyInterface
{
...
Equals(Object2 o) {}
}
korkless

No, that's not possible.

To suggest other options, you have to explain to us why you have that requirement.

Mattias Sj枚gren

Would this work for you?

Code Snippet

public abstract class BaseClass<T> : IEquatable<T> where T : BaseClass<T>

{

public abstract bool Equals(T other);

}

public class DerivedClass : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

// implementation goes here!

return false;

}

}

What we have there is an abstract base class that impliments IEquatable<T> using an abstract method defintion. It's followed by a type constraint that indicates that the generic type used in the base class must be derived from that base class.

This doesn't completely constrain the type as you described... It would be possible to pass a second derived type as the generic type parameter.

See this example:

Code Snippet

public abstract class BaseClass<T> : IEquatable<T> where T : BaseClass<T>

{

public abstract bool Equals(T other);

}

public class DerivedClass : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

// implementation goes here!

}

}

// This compiles, but doesn't make much sense...

// Here IEquatable<T> is using the wrong type parameter

// (DerivedClass instead of DerivedClass2) because

// DerivedClass is also derived from BaseClass, and so

// passes through the type consrtaint.

public class DerivedClass2 : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

}

}

So, you could at least minimize the type inconsistency to only be within the scope of types derived from your base class.

Anyhow, if you're careful about passing the correct type name as the generic parameter to the base class, then it will work fine... but the compiler won't protect you from making mistakes like the one I described. If you do make those mistakes, it should be easy to debug, since anything that wants DerivedClass2 to implimentIEquatable<DerivedClass2> will fail to find it (at compile time).

Hope that helps,

Troy

thoward37

No, that's not possible.

To suggest other options, you have to explain to us why you have that requirement.

Mattias Sj枚gren

Would this work for you?

Code Snippet

public abstract class BaseClass<T> : IEquatable<T> where T : BaseClass<T>

{

public abstract bool Equals(T other);

}

public class DerivedClass : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

// implementation goes here!

return false;

}

}

What we have there is an abstract base class that impliments IEquatable<T> using an abstract method defintion. It's followed by a type constraint that indicates that the generic type used in the base class must be derived from that base class.

This doesn't completely constrain the type as you described... It would be possible to pass a second derived type as the generic type parameter.

See this example:

Code Snippet

public abstract class BaseClass<T> : IEquatable<T> where T : BaseClass<T>

{

public abstract bool Equals(T other);

}

public class DerivedClass : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

// implementation goes here!

}

}

// This compiles, but doesn't make much sense...

// Here IEquatable<T> is using the wrong type parameter

// (DerivedClass instead of DerivedClass2) because

// DerivedClass is also derived from BaseClass, and so

// passes through the type consrtaint.

public class DerivedClass2 : BaseClass<DerivedClass>

{

public override bool Equals(DerivedClass other)

{

}

}

So, you could at least minimize the type inconsistency to only be within the scope of types derived from your base class.

Anyhow, if you're careful about passing the correct type name as the generic parameter to the base class, then it will work fine... but the compiler won't protect you from making mistakes like the one I described. If you do make those mistakes, it should be easy to debug, since anything that wants DerivedClass2 to implimentIEquatable<DerivedClass2> will fail to find it (at compile time).

Hope that helps,

Troy

thoward37
i have an interface

IMyInterface

I would like to have a compilation time constraint that impose the implementation of IEquatable<SpecificClass> for every
SpecificClass that inherits from IMyInterface.

This is not possible so i will use a solution similar to thoward37 response.

thanks both for your replyes



korkless

You can use google to search for other answers

Custom Search

More Threads

• msi .NET dependency
• Custom configuration section
• Where do I put my miscellaneous BLL methods and functions?
• Is it possible to obtain instance name from the type?
• Optimizing performance when enqueuing and dequeuing data
• I need help using the serial port in vb net 2005
• set focus on process?
• Launching Notepad
• Reg:Getting Exception while closing file
• Additional interfaces in System.AddIn contracts and Pipeline Builder