This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
What does the ? operator mean in C# after a type declaration?
(7 answers)
Closed 3 years ago.
I am reading this article and it has this example
int? length = people?.Length; // null if people is null
My question is if the first question mark after the int a typo ? If not what is the purpose of introducing a ? after an int. I understand why its introduced after people. The reason for that is if people is null then it will return a null. Can someone explain the purpose of the ?
Related
This question already has answers here:
Shortest way to check for null and assign another value if not
(12 answers)
Closed 2 years ago.
Is it possible to execute a function within a short statement if the statement is true?
Something like that:
myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
yes:
if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);
At just 1 single character more than your original.
This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 5 years ago.
I've encountered this operator in C# when dealing with custom events: MyEvent?.Invoke(this, new EventArgs());. What is the purpose of the ?. portion of this statement?
It's making sure it's not null. They have these in swift and their called optionals. If the variable is null then it returns null
This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 6 years ago.
I recently came across this while going through someone else's code
var name = Product.Buyer?.FirstName + " " + Product.Buyer?.LastName;
What does this(?.)mean in c#
The operator ?. is called Null-conditional Operators, which is introduced in C# 6.0.
Used to test for null before performing member access (?.) or index
(?[) operation. These operators help you write less code to handle
null checks, especially for descending into data structures.
see the documentation and an example here
This question already has answers here:
Nullable Object must have a value #2
(6 answers)
Closed 9 years ago.
I am trying to assign 'null' value to Datetime? object. But it is throwing "nullable value must have a value" exception.
//item.PlannedStartDate value is nothing.
fact_Initiative.Start_Date = If([String].IsNullOrEmpty(item.PlannedStartDate),
DBNull.Value, CType(Convert.ToDateTime(item.PlannedStartDate),
System.Nullable(Of Date)))
How to solve this
fact_Initiative.Start_Date =If(String.IsNullOrEmpty(item.PlannedStartDate), CType(Nothing, DateTime?), DateTime.Parse(item.PlannedStartDate))
This question already has answers here:
When should one use nullable types in c#?
(10 answers)
Closed 8 years ago.
the null-able short hand property
public long? id{get;set}
what is the incentive of using it, other than if you are reading it from a database, is there a chance the value might be null...?
also, if you want to get the id,
int a = id.value;
what is the performance impact on this? and in which situation will you be compelled to use this shorthand. Please share your thoughts on this
The advantage is the direct check if is null or no
long v = id ?? v2; //if id is null v will get the v2 value
And the performance is pratically the same of a normal one