? Operator used in LINQ [duplicate] - c#

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;

Related

what is the use of ?? in c# [duplicate]

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.

.Net Syntax - what does "=>" do when filtering text within a collection? [duplicate]

This question already has answers here:
What does '=>' do in C#? [duplicate]
(5 answers)
Closed 8 years ago.
Say I have a collection that I want to filter on...
var users = groupInner.GetMembers(true).Where(user => user.Name.ToUpper().StartsWith("D0") == false &&
user.Name.ToUpper().StartsWith("D1") == false &&
user.Name.ToUpper().StartsWith("D2") == false &&
user.Name.ToUpper().StartsWith("D3") == false &&
user.Name.ToUpper().StartsWith("D4") == false).ToList();
When filtering with the where clause on text, I can only get the statement to work with =>. == doesn't seem to work. I've tested it, the above code gives me my correct answer, but just for understanding and to find out if I should using something more appropriate than what I already have...what does => mean when comparing text and is there something I should be using instead?
That's the syntax for indicating a lambda expression. It's called the "lambda operator". See:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side.

How are "anded" 'if' conditions evaluated? [duplicate]

This question already has answers here:
Difference between | and || or & and && for comparison [duplicate]
(7 answers)
Closed 8 years ago.
In C#, if we have the following code:
if (condition1 && condition2)
and condition1 turns out to be false, is condition2 still checked or does the execution simply continue after the if statement?
Firstly it evaluates condition1, then if it is true then it will evaluates condition2. It will not evaluate condition2 if condition1 is false. This is called short-circuit evaluation.
No the && operator is short circuiting. The & operator is however not and all of the expressions will be evaluated if you use that.
Yes C# does short circuit evaluation of boolean expressions. Therefore
if ( X && Y() )
1) X will be executed first
2) Y will only be executed if and only if X returns true
This applies to all boolean expressions and not just those in an IF statement...Check this in the C# Specification available online at MSDN. section 14.11.1
you can use also the & and in this case it won't be a short circuite evaluation because
& is the "and" operator used for bit manipulation.
&& is the "and" operator used to evaluate logically expressions.

AND/OR (&&/||) logic for multiple condition statements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If you have an if-statement in C# that checks multiple conditions:
if (a == 5 && b == 9) { ... }
Does b == 9 still get checked if a == 5 condition is false, or does it automatically exit since there's no way this could pass anymore?
Similarly, for an OR if-statement:
if (a == 5 || b == 9) { ... }
Will b == 9 still get checked if a == 5 is true?
Both && and || is "short-circuiting" operators, which means that if the answer is known from the left operand, the right operand is not evaluated.
This means that:
a && b
b will not be evaluated if a is false, since the final answer is already known.
Likewise:
a || b
b will not be evaluated if a is true, since the final answer is already known.
If you want both operands to be evaluated, use the & and | operators instead.
The bonus of this is that you can write expressions that would fail if all operands was evaluated. Here's a typical if-statement:
if (a != null && a.SomeProperty != null && a.SomeProperty.Inner != null)
... use a.SomeProperty.Inner
If a was null, and the expression would go on to evaluate a.SomeProperty, it would throw a NullReferenceException, but since && short-circuits, if a is null, the expression will not evaluate the rest and thus not throw the exception.
Obviously, if you replace && with &, it will throw that exception if either a or a.SomeProperty is null.
Conceptually, && and || short-circuit.
But since you don't have any side-effects there, the JIT compiler is free to remove the short-circuiting. I don't know whether it actually does so or not.
For : if (a == 5 && b == 9) { ... }
Does b == 9 still get checked if a == 5 condition is false, or does it
automatically exit since there's no way this could pass anymore?
If a == 5 is false no any other control will be executed on that line.
For: if (a == 5 || b == 9) { ... }
Will b == 9 still get checked if a == 5 is true?
Pass inside immediately, as first condition already satisfies requirements.
Using the AND operator, according to the boolean logic, all the conditions must be evaluated to TRUE. If only one of them isn't satisfied, the result of the conditions will be FALSE.
Does b == 9 still get checked if a == 5 condition is false, or does it
automatically exit since there's no way this could pass anymore?
It does automatically exit, because the first condition is false and the result is already known.
For the OR operator, you need that, at least, one of the conditions is TRUE and your logic will be executed. If the first one is not satisfied, the application will check the other conditions.
Will b == 9 still get checked if a == 5 is true?
No, it won't be checked, because the first condition is TRUE and then it's not necessary to check another condition.
Short-cirtuiting is defined by standard. Otherwise it would be impossible to say what is the outcome of expression such as:
if (a != null && a.IsValid) { ... }
In some C# compilers it would work fine, in some others it would cause an exception. That's why the standard is there to define common behavior.
EDIT: clarified last statement.
in AND check
a==5
if is true then
will go to b==9 else
will not go to b==9.
in OR:
it will check a==5 and b==9.

What is the VB.NET equivalent of the C# ? operator? [duplicate]

This question already has answers here:
Is there a conditional ternary operator in VB.NET?
(5 answers)
Closed 9 years ago.
What is the VB.NET equivalent of the C# ? operator?
For example, how would the following code be written in VB.NET?
hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP
Historically, IIf was commonly used for that - but that does not use short-circuiting so is not quite the same. However, there is now a 3-part If:
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
that does use short-circuiting, and thus is identical to the conditional operator in C#.
You can use the If operator
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
Try using the If function like so:
x = If(condition, trueValue, falseValue)
This question is a duplicate of a question that has already been asked and answered:
Is there a conditional ternary operator in VB.NET?
here:
Dim foo as String = If(bar = buz, cat, dog)

Categories

Resources