0

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
  • @sir psycho: remember you can't declare explicit parameterless constructors in structs in c# Commented Oct 15, 2008 at 11:18

4 Answers 4

5

_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
Sign up to request clarification or add additional context in comments.

Comments

1

Try declaring your variable like this:

int? yer;

Comments

0

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

Comments

0

How about nullable types:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.