Question

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;
    }
}
Was it helpful?

Solution

_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

OTHER TIPS

Try declaring your variable like this:

int? yer;

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

How about nullable types:

struct MyStruct
{
    private int? _yer, _ner;
    public MyStruct(int? yer, int? ner)
    {
        _yer = yer;
        _ner = ner;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top