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

This question already has answers here:
Closed 11 years ago.
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!

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.

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.

Related

Nullable Types in .Net Programming Language [duplicate]

This question already has answers here:
How does the assignment of the null literal to a System.Nullable<T> type get handled by .Net (C#)?
(5 answers)
Closed 8 years ago.
Recently while revising one of the concept I came across a situation which is interesting and I want to know why this happens.
We know one concept that we can't assign null values to Value types i.e. Struct, int, DateTime, etc.
In order to assign null we need nullable type i.e.
int i should be replaced with Nullable<int> i = null
But if we see Nullable<T> it is also of type struct then how come null can be assigned without stating any error? Why Microsoft contradicted it's own statement of "Null can't be assigned to value type"
If someone knows the reason behind this?
Because Nullable<T> has compiler-level support; null, in the context of Nullable<T>, is essentially a value with a HasValue flag of false, where-as a non-null value has a HasValue flag of true. And similarly, "lifted" operators are derived from the defined operators. Likewise, Nullable<T> has CLI support, in terms of boxing: a boxed Nullable<T> with HasValue of false becomes null, not a new box instance.
Basically: it all just comes down to because that is how they defined it should work.
People wanted nullable value types: Microsoft figured out a way for that to happen.

Meaning of operator ? after an object in C# [duplicate]

This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
Closed 9 years ago.
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?
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.
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:
http://blogs.msdn.com/b/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx
http://blogs.msdn.com/b/ericlippert/archive/2012/03/26/null-is-not-false.aspx

C# syntax questions [duplicate]

This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
Closed 9 years ago.
i have seen the following code in an c# example:
public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
{
screen.ControllingPlayer = controllingPlayer;
screen.ScreenManager = this;
screen.IsExiting = false;
}
and i have no clue what the ? is doing after PlayerIndex, it is an enum, and in the class every notice of it is with the ? behind it.
my question: what does it do, what is it called and why would you use it.
I have googled this, but it didn't get me far since i dont know the name of this coding and google filters out the question mark in the search query
The ? makes PlayerIndex a NULLABLE type.
That way controllingPlayer can be NULL even if it is an enum or a basic type like int.
The ? is a nullable type. This means that controllingPlayer can contain null or a value.
To check whether there is a value associated with the variable, you can use HasValue. To retrieve the actual value, use Value
if ( controllingPlayer.HasValue )
// now do something with controllingPlayer.Value
The question mark denotes that PlayerIndex is treated as a nullable type.
Probably PlayerIndex is not a class or struct but an enum or alias for int or something like that. If it's an alias, you should find something like this in the code:
using PlayerIndex= System.Int32;
It's short for Nullable<PlayerIndex> which means that you can pass a PlayerIndex value or null.
The ? is indicating that controllingPlayer can be null. It could also be written as Nullable<PlayerIndex> controllerPlayer.
This is useful when working with valuetypes which can not be null, like reference types can be. If you have a regular int, you cannot differentiate between a variable that is given the value 0 and a variable that is never written to. By wrapping it in a Nullable<>, you can now check if it has a value or not:
int notNullable; //will be initialized to 0 by default.
int? nullable; //will be initialized to null by default.
if (nullable.HasValue) //Do something if the variable has been given a value
{
return nullable.Value; //get the actual int-value
}
See msdn documentation for nullable types: http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx
It means that the value type in question is a nullable type
Appending ? to a type when defining something it is shorthand for wrapping in a Nullable<T> (see Nullable Types for further details). This means that value types, usually not able to express nullity, can be checked for a non-value instead of always resorting to their default (i.e., an int, rather than default to 0, will default to null).
You may check whether or not the thing has a value, and access the value thusly:
var hasValue = nullableThing.HasValue;
var underlyingValue = nullableThing.Value;
PlayerIndex? indicate that varible can be null value. The syntax T? is shorthand for System.Nullable, where T is a value type. please refer to Nullable Types (C# Programming Guide).

C# syntax question [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
? (nullable) operator in C#
What does the ? do used like this:
class Test
{
public int? aux;
}
It's probably a simple question for someone more familiar with c#, but I don't really know what to search for. I read an explanation but didn't fully understand it. I would also be interested in knowing what the "??" operator does. Some examples on where they would be useful would be of great help.
This (int?) is shorthand for Nullable<int>.
It can be any int value plus an additional null. See more info Nullable Types
The purpose of using Nullable int is while often using database operations, there are conditions where some value might be null and we have to express in code. For Example consider the folowing schema
Employee
- id, bigint, not null
- name, nvarchar(100), null
- locationId, bigint, null
Suppose locationId of an employee is not available, so in database the value of locationid of employee is NULL. On C# side, you know that int can not have a NULL value, so type, Nullable int(and few others) has been added, due to which we can easily show that locationId has no value.
"?" denotes a nullable type
int? means nullable integer: aux can have an int value or be null!!
As many others have already answered, the ? denotes nullable types. This means that a variable of type int? can be assigned null in addition to an integer values.
This is used when you want to distinguish the case of a variable which has not been initialized from the case in which it has been assigned the default value.
For instance you can consider the case of bool. When you declare a bool variable, it has the false value by default. This means that when its value is false you can't tell if this happens because someone has assigned the variable that value, of because nobody touched it. In this case a bool? is useful since it allows you to distinguish a "real" false value (explicitely assigned by some code) from the not initailized case.

Can assign null? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to check if an object is nullable?
Determine if reflected property can be assigned null
How can I properly identify if a variable (or a class member) of given type can take null? More specifically how to handle Nullable<T> since it is not a reference type? Or any other type that may have some weirdo implicit conversion defined on it.
My gut feeling is that the only sure way to find out is to try{} catch{} it and see if it blows up... But maybe there are some tricks to it.
It's not clear what information you do have about the type in question.
try/catch will do things at execution time, which isn't really what you want.
For a concrete type, you should be able to know just by knowing the variable type. It should be pretty obvious - or if it's not, you've got bigger problems than not knowing about nullity :)
For a generic type, I've found this is quite useful:
if (default(T) == null)
For a Type reference (e.g. if you're using reflection) you could use:
if (!type.IsValueType || Nullable.GetUnderlyingType(type) != null)
bool canBeNull = !type.IsValueType || (Nullable.GetUnderlyingType(type) != null);
Only reference types can actually hold a null reference; the special case of Nullable<T> is all syntactic sugar; the resulting value is not actually null (since Nullable<T> is a value type, too, so it cannot hold a null reference), it's just a Nullable<T> with default(T) as its Value and HasValue = false.
So it depends on what you're asking.
If you're trying to determine if the type qualifies for in-code assignment to the null (Nothing in VB.NET) literal, then it's:
All reference types
Nullable<T>
If you're trying to determine if the type qualifies to hold a bona-fide null reference, then it's
All reference types
As for using reflection to inspect a particular type at runtime, checking IsValueType should be enough to get you what you need, even if it's the former (just add a special case in your code for Nullable<T>).
Nullable exposes the HasValue property so if you want to check if a value type is nullable then just check if it exposes that Property.

Categories

Resources