Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a questionmark (?) mean in a function declaration in C# [duplicate]

Possible Duplicate:
What does “DateTime?” mean in C#?
What does the ? mean after a type?

I had a function declaration including a questionmark after the datatype like:

private TimeSpan? sometime()
{

}

What does this mean?

like image 775
CloudyMarble Avatar asked Jan 10 '12 11:01

CloudyMarble


2 Answers

TimeSpan? is shorthand for System.Nullable<TimeSpan>.

A TimeSpan is a value type, which cannot take a null value. By wrapping it in a System.Nullable<> it can be null. Without that ? it would be illegal to return null from the function.

like image 67
Anders Abel Avatar answered Sep 27 '22 22:09

Anders Abel


Nullable Structure

Represents an object whose underlying type is a value type that can also be assigned null like a reference type.

Instead of writing Nullable<TimeSpan>, you can write TimeSpan?.

like image 29
CD.. Avatar answered Sep 28 '22 00:09

CD..