tags:

views:

360

answers:

4

Hi,

Is there a way to declare a variable as Nullable in c#?

struct MyStruct {        
    int _yer, _ner;

    public MyStruct() {

        _yer = Nullable<int>; //This does not work.
        _ner = 0;
    }
}
+1  A: 

Try this:

int? yer;

Joel Cunningham
A: 

How about nullable types:

struct MyStruct
{
    private int? _yer, _ner;
    public MyStruct(int? yer, int? ner)
    {
     _yer = yer;
     _ner = ner;
    }
}
Kev
A: 

Try declaring _yer as type Nullable initially, rather than as a standard int.

workmad3
+5  A: 

_yer must be declare as int? or Nullable<int>.

    int? _yer;
    int _ner;

    public MyStruct(int? ver, int ner) {

        _yer = ver;
        _ner = ner;
    }
}

Or like this:

    Nullable<int> _yer;
    int _ner;

    public MyStruct(Nullable<int> ver, int ner) {

        _yer = ver;
        _ner = ner;
    }
}

Remember that structs cannot contain explicit parameterless constructors.

error CS0568: Structs cannot contain explicit parameterless constructors
smink