This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
Closed 5 years ago.
I know ? checks for null when placed before a . member access and ?: for conditional statements. Although, I think ?? checks for null as well but I'm not very sure
I can't find useful information about ?? on https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
PS. Actually I didn't look well at the MSDN reference very well. I've just seen its definition now.
I though of closing this post before but I won't for the sake of anyone who wouldn't think of referring to ?? as double question marksin their question
that operator is sugarSyntax for operations with nullable operands
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
int? counter = null;
int backup = counter ?? 0;
in this case backup will be assigned with counter value IF counter is different to null, for something ELSE then backup will be assigned with 0
note that I bold the keywords IF-ELSE which make us infer that ?? operand can be replaced by simple old-school if else conditionals.
Related
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 just started learning LINQ and then just stuck on an example statement provided in the tutorial (Please not that I am working with C# .Net Framework). The statement
arr?.Count(w => w != null) > 0
returns True only if their is at-least one none-null element in the arr (array or list). But what is the ? operator doing there? Is this another form or ternary operator or something else? Please share your precious knowledge on this point. I shall be glad and thankful to read good answers from you.
Note: I tried removing the ? operator in the statement but could not find any difference.
It's the Null conditional operator
It's basically checking for null and executing the condition if not null. In the case arr was null, this code would not throw an exception. If written without the Null Conditional operator you would get a NullReferenceException.
// this would throw an exception
int?[] arr;
arr.Count(w => w != null) > 0;
// this will check if arr is null and not proceed to call the .Count method
int?[] arr;
arr?.Count(w => w != null) > 0;
This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.
Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to check if my Listentry = null but I don't know how.
Note:
ID is a Integer
Here is my Code
if(MeineGaeste[i].ID !=null)
{
i++;
}
else
{
MeinGast.ID = i++;
test = false;
}
As stated, an int cannot be null. If a value is not set to it, then the default value is zero. You can do a check for 0 if that is what you think it is being set to...
Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.
String.IsNullOrEmpty(ID.ToString());
Which returns a boolean so you can stick it into an if statement.
An int cannot be null. On the other hand a nullable int, int?, can be. That being said the check that you want to do is meaningless.
Changing the type of ID from int to int?, the check you do has a meaning and the compiler wouldn't complain about it.
you can use this ID.HasValue it return true when there is a value and false if null
As a normal int type is a value type, it can not be null.
You can use the nullable type int? for the property ID which is basically a struct, that allows null or any int value. See MSDN or this question or this question for additional documentation.
If it is not possible to change the type of the ID property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.
This question already has answers here:
Method Call using Ternary Operator
(8 answers)
Closed 8 years ago.
I want to convert this if, else-if, can someone help me out please?
if (condition1)
response.Redirect(" some link");
else if (condition2)
response.Redirect("link 2");
I want convert above statement,but showing error at the end, required ":". Any other way i can use this?
LinkPurchase.PostBackUrl =((Condition)?string.Format("some link"):
(condition2)?string.Format("link 2));
You cannot rewrite that to the ?: operator.
You have an if-else if, not just an if-else.
Besides, you do not pick up the return values from the Redirect calls.
The usual case where you do want to rewrite to the ?: operator is:
if (condition)
something = Abc();
else
something = Xyz();
where it is natural to use instead:
something = condition ? Abc() : Xyz();
You cannot do this.
There are other answers here that inform you that the ?: operator needs the "else" part, so yes, the first problem is that you're missing that.
However, Response.Redirect doesn't return anything, so you cannot do this even with the else part.
?: is an expression, you cannot write statements (easily) with this.
Stick with the if-statements.
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