What is this line?
-
helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx
public int? Year;
-
helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx
public int? Year;
int is non nullable int? is nullable
Never underestimate the power of human stupidity RAH
-
helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx
public int? Year;
to elaborate on Mycroft's reply. This is often used when talking to the database. Suppose you have a column that contains a number, but the column can be null. In that case you would need to check if the value was
DBNUll.Value
and if so convert your variable (that maps to that column) to-1
or0
or something. When using the?
construct you don't need to convert it and can directly map the variable to the database column. Some goes if you want to write a value to that column. The ? is an 'override' so you can put null values in non-nullable variables (int, double, DateTime, ... but not strings, they can be null) Hope this helps. [EDIT]PS: don't be afraid to use it, but don't use it if you don't have to.[\EDIT]V.
-
helo guys... I am frequently wathcing the following line in tutorials online. What does this line mean? thnx
public int? Year;
The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.
Will Rogers never met me.
-
The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.
Will Rogers never met me.
Roger Wright wrote:
The ? operator can be used with any type, for what it's worth.
No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
Roger Wright wrote:
The ? operator can be used with any type, for what it's worth.
No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
Part of the reason it can't be used with strings is that strings are nullable by default. They don't need special treatment.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Part of the reason it can't be used with strings is that strings are nullable by default. They don't need special treatment.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
I just checked, cause I wasn't sure... string is just a reference type, that acts like value type. So it can point to a null reference of course. My mistake ;-)
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
Roger Wright wrote:
The ? operator can be used with any type, for what it's worth.
No. It can only be used with value types (int, double, decimal, DateTime). It cannot be used with reference types. And it cannot be used with strings.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
I didn't know about the reference type limitation, but then, I can't think of any value for the concept anyway and have never needed it. Strings are nullable by nature, so they don't count. Thanks for the info! :-D
Will Rogers never met me.
-
The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.
Will Rogers never met me.
? can be useful to determine if a choice has been made. To give an example, a person has 0 children because 1. they truely don't have any children or because 2. The value was never set? Null is a way of explicitly determining if the person has no children. The old way was to set the default to -1. I think null is a better way of expressing that the value was not set.
"You get that on the big jobs."
-
The ? after the variable type renders a variable that is nullable. What value that may have I haven't discovered, as I like my variables to have real values before I use them, but apparently someone has decided that a null integer is a good thing, and thoughtfully provided a way to declare such a beast. The ? operator can be used with any type, for what it's worth.
Will Rogers never met me.
To expand on the last reply, it is useful when a variable can validly take the whole range of the type in question, and can also have a 'not set' value. Typically we have either used an invalid value (e.g. testing getchar() for -1 in old school C; -1 is a common 'not set' value in ints still) or created another boolean variable to define whether or not we set a value:
bool customerLikedProduct;
bool customerResponded;People have also solved that one with a three way enum ...
enum NullableBool { True, False, Maybe }
... but when you do that you lose the ability to use boolean logical operators, compatibility with external data sources or applications, etc. In these cases, you would like to be able to represent the 'not set' value within one variable. That's what nullable types give you:
bool? customerLikedProduct;
As already mentioned, it's particularly useful for working with databases where null values are a common and standard thing. For me, using -1 or double.NaN is fine, as long as that can never be a valid value for the variable in question. But there are times when the whole valid range for the natural data type is valid and you should use nullable types.