3

I am trying to find the meaning of the symbol/operator ? in C#.

Usage example:

private Point? _point = null;

I think it has to do something with the null value.

of course I looked on MSDN C# Operators page but and didn't find an answer there.

Can someone give me a link or explain this operator?

ColinE
  • 68,894
  • 15
  • 164
  • 232
Natan Braslavski
  • 819
  • 8
  • 18
  • 1
    You didn't find the `?` on operator page, because it's not an operator. It just a syntactic sugar. Read http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx – MarcinJuraszek Dec 28 '13 at 07:41
  • 1
    Also a duplicate of [this](http://stackoverflow.com/questions/2079334/what-does-the-mean-after-a-type), [this](http://stackoverflow.com/questions/2069863/a-curious-c-sharp-syntax-with-a-question-mark), [this](http://stackoverflow.com/questions/109859/what-does-datetime-mean-in-c) and of course [this](http://stackoverflow.com/questions/2699373/c-sharp-basic-question-what-is). And probably a lot more. – Pierre-Luc Pineault Dec 28 '13 at 08:00
  • @Pierre-LucPineault when developing programming languages, they need to ensure the constructs are easy to google! – ColinE Dec 28 '13 at 08:59
  • The duplicate link is broken and is it still to be marked as duplicate?? – Sandip Bantawa Jan 13 '15 at 08:18
  • This should be a duplicate of https://stackoverflow.com/questions/2690866/what-is-the-purpose-of-a-question-mark-after-a-type-for-example-int-myvariabl. The current dupe target has been deleted. – Ilmari Karonen Feb 20 '19 at 22:57

2 Answers2

12

It is a shorthand for Nullable<T>, i.e. Point? is the same as Nullable<Point>. This allows you to assign a null value for value types.

See the MSDN reference on Using Nullable Types.

NOTE, in this context it is not an operator, it is a shorthand syntax.

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • So bool? would also take null value beside true and false. – pbies Dec 28 '13 at 07:43
  • "This allows you to assign a null value for value types." is the correct answer. This works for all valuetypes (struct, enumeration,..) – Florian Dec 28 '13 at 07:45
2

Adding to previous answer

In a simple way, usually in C#, we have value type and reference type, reference types have reference and can be assigned null values but value type cannot be assigned null because they don't have any reference, by term reference its simply 32 or 64 bit number which is not address of virtual memory space of the process that the referred-to object lives whereas pointer is actual virtual memory space of the process that the referred-to object lives, C# has concept of both pointer (unsafe) as well as reference.

Coming back to question, T? is syntactic sugar for Nullable<T>

Huge reason why we use it is sql accepts null value for int, float or any types that are value types in C# so the problem usually arises when someone tries to get value from sql which is an int but has null, we get error when we try to enter null on the value types so the solution is nullable type.

References:

  1. http://blogs.msdn.com/b/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
  2. http://blogs.msdn.com/b/ericlippert/archive/2012/03/26/null-is-not-false.aspx
Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47