Nullable Value and Generic

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



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

 


But it can't.  Why

Please help me....




Answer this question

Nullable Value and Generic

  • Afia

    Thank you for your help.

    I can solve my problem

    public class DataField<T> where T:INullableValue



  • MartinUSS

    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.



     



  • Chuck S.



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

     


    is that your mean
    you can do like this


  • RichardWu

    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.

  • mms02

    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

  • ScottWK

    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....

  • Nullable Value and Generic