8

Possible Duplicate:
a curious c# syntax

So I've seen some code around and a few of them use a ? after the type, like this:

private Point? loc = null;

So I'm wondering if Point? is different than Point (can't put a question mark at the end of my sentence or I'll confuse you guys ... :] ). The language I'm using is C# by the way.

Community
  • 1
  • 1
Dominic K
  • 6,975
  • 11
  • 53
  • 62
  • 2
    To write "code" inside normal sentences, use backticks, ` to flag text as "code". In this way, you can use angle brackets, and if you had put the question mark inside, you wouldn't have confused anyone either. – Lasse V. Karlsen Jan 16 '10 at 23:25
  • Dup of http://stackoverflow.com/questions/2069863/a-curious-c-syntax/2069873#2069873 – Eilon Jan 16 '10 at 23:26

3 Answers3

10

T? is a shorthand (in C#) for Nullable<T> - so Point? is another way of writing Nullable<Point> or example.

See sections 1.3 and 4.1 of the C# 3 language spec - and various other places, to be honest - for more details. See the docs for System.Nullable<T> for more information from the framework side of things. Or read chapter 4 of C# in Depth :) (Unfortunately it's not one of the free chapters.)

(This question is bound to be a duplicate, but I don't have the energy to find it right now.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    It is indeed a dup: http://stackoverflow.com/questions/2069863/a-curious-c-syntax/2069873#2069873 – Eilon Jan 16 '10 at 23:26
  • Another duplicate: http://stackoverflow.com/questions/2072482/in-c-what-is-the-in-the-type-datetime – Zach Johnson Jan 16 '10 at 23:28
  • Knew it was somewhere, but couldn't find it (I don't think the search can handle ? very well and I had no clue it was nullable... Sorry guys. – Dominic K Jan 16 '10 at 23:30
2

Point? is the same as Nullable<Point>. It allows you to assign null to value types, such as structs.

Thomas
  • 174,939
  • 50
  • 355
  • 478
0

It means the type can accept its' value and null.

Yevhen
  • 1,897
  • 2
  • 14
  • 25