Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the ? operator in C# for Properties [duplicate]

Tags:

c#

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've 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!

like image 727
swinefeaster Avatar asked Jul 29 '11 06:07

swinefeaster


People also ask

What is operator in C with example?

In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical functions. It is a symbol that operates on a value or a variable. For example, + and - are the operators to perform addition and subtraction in any C program.

What does &= mean in C?

It means to perform a bitwise operation with the values on the left and right-hand side, and then assign the result to the variable on the left, so a bit of a short form.

What is operator and types of operator in C?

Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.


Video Answer


4 Answers

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 the By property with the From property.

like image 66
Konrad Rudolph Avatar answered Oct 30 '22 22:10

Konrad Rudolph


The ? means it's nullable (the value can be set to null.

Nullable Types (C# Programming Guide)

like image 41
Tim Avatar answered Oct 30 '22 21:10

Tim


This is not an operator. Rather, this is a special shorthand syntax for declaring nullable values.

like image 22
Anton Gogolev Avatar answered Oct 30 '22 20:10

Anton Gogolev


It is nullable propertie, that means that you can set By = null, without ? you'll get error that double cant be null

like image 45
Renatas M. Avatar answered Oct 30 '22 20:10

Renatas M.