Meaning of the ? operator in C# for Properties [duplicate]
Possible Duplicate:
? (nullable) operator in C#
In System.Windows.Media.Animation I see the code as follows:
public double? By { get; set; }
What does the ? operator do here? Does anyone know?
I'v开发者_C百科e tried to google this but it's hard to search for the operator if you don't know what its called by name. I've checked the page on Operators (http://msdn.microsoft.com/en-us/library/6a71f45d(v=vs.80).aspx) but the ? operator is not listed there.
Thanks!
The ?
is a type decorator. T?
is the same as Nullable<T>
, i.e. a nullable value type.
The documentation of the By
property explains why it’s used here:
The property controls how A DoubleAnimation
progresses; but instead of setting the By
property, you can also set the From
and To
properties (or either) to control animation progress. Every combination of properties (except To
and By
) is allowed, so there needs to be a way to signal that a property is not set – hence it’s nullable.
Use the
By
property when you want to animate a value "by" a certain amount, rather than specifying a starting or ending value. You may also use theBy
property with theFrom
property.
The ? means it's nullable (the value can be set to null.
Nullable Types (C# Programming Guide)
This is not an operator. Rather, this is a special shorthand syntax for declaring nullable values.
It is nullable propertie, that means that you can set By = null
, without ?
you'll get error that double cant be null
?
stands for Nullable types, this qualifies By
in your case to hold a null value which is not possible for a value type
It means the type is nullable.
See this page for details.
this is syntactic sugar handled by the C# compiler.
It basically treat "double?" as Nullable which allows the value to be null. It basically wraps the double value inside another object.
精彩评论