Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, February 4, 2010 10:04 PM
public abstract class MyBaseClass<T> where T : class
{
public abstract void Foo(T s);
}
public class MyClass : MyBaseClass<String>
{
public void Foo(String s)
{
}
}
What am I doing wrong? This works fine for the value type constraint (i.e. "struct")
public abstract class MyBaseClass<T> where T : class
{
public abstract void Foo(T s);
}
public class MyClass : MyBaseClass<String>
{
public void Foo(String s)
{
}
}
All replies (4)
Friday, February 5, 2010 2:07 PM ✅Answered
Object can be nullable, you want the value type to be nullable, don't need to change anything in base class, see my example below:
public abstract class MyBaseClass<T>
{
public abstract void Foo(T s);
}
public class MyClassReference : MyBaseClass<string>
{
public override void Foo(string s)
{
}
}
public class MyClassValue : MyBaseClass<int>
{
public override void Foo(int s)
{
}
}
public class MyClassNullable : MyBaseClass<bool?>
{
public override void Foo(bool? s)
{
}
}
Friday, February 5, 2010 2:55 PM ✅Answered
According to C# Language Specification:
A primary constraint can be a class type or the reference type constraint class or the value type constraint struct. A secondary constraint can be a type-parameter or interface-type.
The reference type constraint specifies that a type argument used for the type parameter must be a reference type. All class types, interface types, delegate types, array types, and type parameters known to be a reference type (as defined below) satisfy this constraint.
The value type constraint specifies that a type argument used for the type parameter must be a non-nullable value type. All non-nullable struct types, enum types, and type parameters having the value type constraint satisfy this constraint. Note that although classified as a value type, a nullable type (§4.1.10) does not satisfy the value type constraint.
The intermediate solution what I will suggest is:
public abstract class MyBaseClass<T> where T : struct
{
public abstract void Foo(T? s);
}
public class MyClass : MyBaseClass<int>
{
public override void Foo(int? i)
{
}
}
Thursday, February 4, 2010 11:25 PM
It's the method Foo should have the 'method override':
public override void Foo(string s)
{
...
}
In order for the value type to work, you need to get rid of the 'class' constraint in the base class, I don't understand why you are saying the 'struct' worked, it should not work. Anyways, just based on your given code, get rid of the 'class' constraint should work for both value and reference types.
Friday, February 5, 2010 1:13 PM
In response to the first comment ... thank you for pointing out that I'm an idiot. ;-) I plain forgot. LOL
It is no longer an issue, but the reason for the constraint is that I need to support Nullable types. In order to support Nullable, I have to restrict the class to "struct" ... and if I want to have the same functionality for reference types (e.g. String) I have to build a separate class.
Is this wrong?
Todd