What's wrong with this generic type?

written by Ryan Olshan on Wednesday, December 21 2005

I love mysteries. However, this one is pretty simple.

Q: What is wrong with the following generic type?

VB.NET
Public Class BaseClass(Of K, V)
End Class

Public Class SubClass1
   Inherits BaseClass(Of K, V)
End Class

Public Class SubClass2
   Inherits BaseClass(Of Integer, Long)
End Class

C#
public class BaseClass<K, V> { }

public class SubClass1 : BaseClass<K, V> { }

public class SubClass2 : BaseClass<int, long> { }

A: If you were to compile the above code, the compiler would generate an error for SubClass1. SubClass1 is an open type and generates no point of reference for types K and V. SubClass2 is a constructed type and, since it has type arguments declared, will compile.

Kick this post on .NET Kicks

Similar Posts

  1. Generic Sub Classes
  2. What sets C++ Templates and .NET Generics apart?
  3. What does the ` character stand for?

Post a comment