c# - 'int' never equal to null for 'char' variables -
can answer me - why warning "the result of expression 'false' since value of type 'int' never equal 'null' of type 'int?'"
here code
private char classletter; public char classletter { { return classletter; } set { if (classletter == null) { classletter = value; raisepropertychanged("classletter"); } else throw new argumentoutofrangeexception(); } }
if use code no warning comes in
private char classletter; public char classletter { { return classletter; } set { if (classletter.tostring() == null) { classletter = value; raisepropertychanged("classletter"); } else throw new argumentoutofrangeexception(); } }
in short question this
how can int warning given char variable?
edit: 'char' should hold latin or cyrillic letter, no special symbols , no numbers allowed. how should filtered?
in first case, classletter
of type char
, can never null
because it's value type; need of type char?
. comparing classletter == null
doesn't make sense, compiler says.
in second case, imagine classletter
'x'
. when doing classletter.tostring()
"x"
, can compared null
because reference type. again, not want because classletter.tostring()
never null
.
if want allowing set value once, can this:
private char? classletter = null; // make nullable public char classletter { { if(classletter == null) { // if don't have value yet // something, throwing exception }else{ // if have value return classletter.value; // here return char, not char? } } set { if (classletter == null) { classletter = value; raisepropertychanged("classletter"); } else throw new argumentoutofrangeexception(); } }
Comments
Post a Comment