Nullable Value and Generic

I want to mark a generic class to support nullable value. The following is my class



publicclass DataField<T> where T:System.Nullable<T>
{
public T Value;
}

But it can't. Why?

Please help me....

[527 byte] By [EagleTsui] at [2008-2-15]
# 1


public class NullableClass<T>
{
// empty
}
public class UseNullableClass
{
private NullableClass<int?> nullVariable;
}

is that your mean ?
you can do like this

UnquaLeX at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
That's because Nullable<T> is a Value Type (struct), and you can't inherit Value Types.

You can implement System.INullableValue. However, by default all reference types have a concept of a null value. For example:

DataField<String> field = null;

Field now contains a null reference. What are you trying to achieve?

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Sorry,

I misread your post, I thought you were inheriting Nullable<T>.

For some reason, this restriction was placed for generics.

Replace Nullable<T> with INullableValue, which Nullable<T> inherits.

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

Thank you for your help.

I can solve my problem

public class DataField<T> where T:INullableValue

EagleTsui at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
I have several issues for this approach

this is my class
public class DataField<T> where T:INullableValue
{
public T Value;
public DataField() { }
}

1. I cannot use string in this approach
DataField<int?> field=new DataField<int?>; work
DataField<long?> field=new DataField<long?>; work
DataField<string?> field=new DataField<string?>; not work
DataField<string> field=new DataField<string>; not work

How to solve this problem. I want to enforce the data type is nullable value when create DataField object

2. I cannot add the DataField into collection
Dictionary<int, DataField<INullableValue>> dataFields= new Dictionary<int, DataField<INullableValue>>();

DataField<int?> x = new DataField<int?>();
dataFields.Add(1,x);
DataField<long?> y = new DataField<long?>();
dataFields.Add(2,y);

It does not work.....Tongue Tied
Please help me....

EagleTsui at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

1. string? is not actually valid. Nullable<T> has a struct constraint so only value types can be used with Nullable<T>.

As far as I'm aware there is no way to use constraints to say: Only allow reference types and nullable types. The only way I can see to do this is to remove the constraints and run some sort of type check within the static constructor of the generic type.

2. Generics is a bit tricky here. Is your situation DataField<int?> is not actually assignable to DataField<INullableValue>, even though int? implements INullableValue. The same works for base and derived classes. For example, If you have the following classes:


public class MyClass<T>
{
}

public class A
{
}

public class B : A
{
}

This will not compile:


MyClass<A> myclass = new MyClass<B>();


The only common base class that MyClass<A> and MyClass<B> have is System.Object. Object is also the only common base class that DataField<int?> and DataField<INullable> have in common.

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...