3

I've used this quite innocently for a while now without actually knowing much about it, but basically it's:

Private _SomeFieldDate As DateTime?

or...

Private _SomeFieldInt As Int32?

I realise that the question mark on the end allows the definition of a Null or rather Nothing value to be assigned to the field, but what exactly happens here, and what is the term for this question mark?

(PS: I did try looking for this on SO but couldn't find it; if this has previously been posted then would you mind popping a link to the post in an answer, please?)

Paul
  • 4,160
  • 3
  • 30
  • 56
  • 1
    http://msdn.microsoft.com/en-us/library/ms235245.aspx – Tim Schmelter Oct 08 '13 at 08:22
  • 1
    Adding the question mark turns it into a nullable type, which means that either it is a variabletype object, or it is null – Vinay Pratap Singh Bhadauria Oct 08 '13 at 08:22
  • There is no terminology for that `?`, it's basically, as you state, as marker to declare a nullable type. – Jason Evans Oct 08 '13 at 08:22
  • One possible duplicate: [What does the ? mean after a type?](http://stackoverflow.com/questions/2079334/what-does-the-mean-after-a-type) Note that C# and VB.NET are not different on this topic. – Tim Schmelter Oct 08 '13 at 08:23
  • http://msdn.microsoft.com/en-us/library/vstudio/2cf62fcy.aspx – Vinay Pratap Singh Bhadauria Oct 08 '13 at 08:23
  • possible duplicate of [Is there such a thing as a nullable bool in vb.net](http://stackoverflow.com/questions/1225471/is-there-such-a-thing-as-a-nullable-bool-in-vb-net) – CodeCaster Oct 08 '13 at 08:24
  • Thanks everyone - that's cleared that up. I see that the question mark can actually be either side of the assignment, also, so that you can declare your field/variable so: `myvar? As Boolean`, or the type `myvar As Boolean?`. – Paul Oct 08 '13 at 08:30

2 Answers2

3

It's called a nullable and the question mark is short for

Nullable<T>

More info on msdn

Kristof
  • 3,267
  • 1
  • 20
  • 30
1

Primitive types such as integers and booleans cannot generally be null, but the corresponding nullable types (nullable integer and nullable boolean, respectively) can also assume the NULL value. NULL is frequently used to represent a missing value or invalid value, such as from a function that failed to return or a missing field in a database, as in NULL in SQL.

Source : http://en.wikipedia.org/wiki/Nullable_type

Nullable types in C# - http://msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx

Anuraj
  • 18,859
  • 7
  • 53
  • 79