This question already has answers here:
What does x?.y?.z mean?
(3 answers)
Closed 7 years ago.
I have been in a conference and speaker's example has '?.' operator.
What is it?
Similar code:
var result = man?.Name;
It's c# 6.0 syntax, Null propagation Operator. It means :
var p = man;
if(p != null)
{
var result = man.Name;
}
else
{
var result = null;
}
More info here: https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
It is called Null-propagating operator in C#-6.0 version.
var result = man?.Name;
is equal to
var temp = man;
var result = (temp != null) ? man.Name : null;
The New and Improved C# 6.0
Null-propagating operator ?.
Null-propagating operator details
Related
This question already has answers here:
Elegant way to avoid NullReferenceException in C#
(4 answers)
Closed 3 years ago.
Suppose I have a fairly deep class, and I need to get some field embedded deep within
int? result = this.child.button.data;
return result;
And at any point, it could be pointing to a null, in which case I want to return null as well. A common approach would be
if (this.child != null && this.child.button!= null) {
return this.child.button.data;
} else {
return null;
}
But if the field is REALLY deep nested, then the only solution I can think of is this:
int? result = null;
try {
result = this.child.button.handler.controller.renderer.data;
} catch (NullReferenceException ex) {
// Do nothing here
}
Is this the correct approach, or is there a better solution?
you may check the null conditional operator in C# - ?.
Like:
result = this?.child?.button?.handler?.controller?.renderer?.data;
This question already has answers here:
?: ?? Operators Instead Of IF|ELSE
(9 answers)
Closed 4 years ago.
if (excellInfoUpdateL3.canSendLetter == "N/A") {
return ClosureInfo.canClose ="N";
}
else {
return ClosureInfo.canClose = excellInfoUpdateL3.canSendLetter;
}
can above code be written in a single line using if condition or something like that?
You can put it in one line like this:
return excellInfoUpdateL3.canSendLetter == "N/A" ? "N" : excellInfoUpdateL3.canSendLetter;
Yes You can by using ternary operator
return (excellInfoUpdateL3.canSendLetter == "N/A")?"N":excellInfoUpdateL3.canSendLetter;
Here is how ternary operator works
(Condition)?if contion is true : if condition is false
This question already has answers here:
Equivalent of Visual Basic's And and Or in C#?
(6 answers)
Closed 8 years ago.
I have the following code:
if (bl != closeButtonLabel)
{
if (bl != minimiseButtonLabel)
{
optionPanel.Controls.Remove(bl);
}
}
Is there a way to do this in 1 if but check the 2 conditions?
At VB it's easy, you place 'Or' and not 'OrElse' but in c# there is only '||'.
Can anyone help me?
You could try the logical AND operator, '&&'.
if (bl != closeButtonLabel && bl != minimiseButtonLabel)
optionPanel.Controls.Remove(bl);
You don't need the logical Or operator here, ||.
Let's say out loud, what you want: bl should be differnt fom closeButtonLabel AND it shoud be different from minimisButtonlabel
if (bl != closeButtonLabel && bl != minimiseButtonLabel) {...}
or if you really want to use OR
if (!(bl == closeButtonLabel || bl == mimimizeButtonLabel)) {...}
(DeMorgan's Laws)
if(bl != closeButtonLabel && bl != minimiseButtonLabel)
{
//do work
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Explaining post-increment in C#
Consider the following C# code:-
int i = 2;
i = i++;
Console.WriteLine(i);
I am getting the output as 2. Why there is no effect of i = i++?
Depending on where you put the +-operators, the value assigned is incremented before or after:
i = ++i;
This way i is counted up before assigned.
i = i++;
This way i is counted up after assigned.
Because the = operator takes precedence first.
MSDN: Operator precedence and associativity.
Try this one:
int i = 2;
i = ++i; // or write just ++i;
Console.WriteLine(i);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Shortcut for “null if object is null, or object.member if object is not null”
I wish there were a way to make this type of test simpler in C#:
if (foo != null &&
foo.bar != null &&
foo.bar.baz != null &&
foo.bar.baz.bat != null)
{
var bat = foo.bar.baz.bat;
bat.cat = 5;
bat.dog = 6;
}
One possible feature that would support this is if statements that supported new variable declarations, e.g.
if (foo != null &&
(var bar = foo.bar) != null &&
(var baz = bar.baz) != null &&
(var bat = baz.bat) != null)
{
bat.cat = 5;
bat.dog = 6;
}
The gains may not be obvious here, since the proposed solution is actually longer in this example. But with longer names it would really pay off.
Is there any better way to do this? Any chance of a suggestion like this actually making it into a future version of the language (calling Eric Lippert, come in)?
This is a fairly frequently requested feature, and as a result this question has now been asked several times on StackOverflow. See
Shortcut for "null if object is null, or object.member if object is not null"
for some thoughts on it.
Good question, but bad intentions. Your code is going to ignore the Law of Demeter
I usually just move the expression in the if-statement to its own method at the bottom of the file:
if (CheckBat(foo))
{
var bat = foo.bar.baz.bat;
bat.cat = 5;
bat.dog = 6;
}
... snip ...
private bool CheckBat(MyFoo foo)
{
return foo != null &&
foo.bar != null &&
foo.bar.baz != null &&
foo.bar.baz.bat != null;
}