how to apply if condition in C# [duplicate] - c#

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

Related

How can I shorten a test for a list being null or having no elements? [duplicate]

This question already has answers here:
How do I check a variable is not null before doing another check?
(3 answers)
Closed 3 years ago.
I have this code:
if (App.selectedPhrases == null || App.selectedPhrases.Count == 0)
I know that I can use App.selectedPhrases?.Count to return null if needed but how how can I shorten this test? I can't see a way to check for null or 0 without needing to use the || and have two tests.
How about setting output value to 0 if the list is null using the ? you tried:
if ((App.selectedPhrases?.Count ?? 0) == 0)
Try:
(App.selectedPhrases?.Count ?? 0) == 0

A safe way to get some deep value [duplicate]

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;

Compare multiple values in one condition [duplicate]

This question already has answers here:
Equality comparison between multiple variables
(14 answers)
Closed 7 years ago.
Int32 int1, int2, int3 = 123;
Given the above variables, how can I test that all of my variables have the value 123 without creating a collection to perform some Any or something on?
What I've Tried
if(int1 == int2 == int3 == 123)
{
// Fantastic code here
}
EDIT
I must apologise, I haven't been clear enough in my question. I'm fully aware of the && operator, I was asking with regard to 'elegance', i.e. how can I avoid repeating the value I want to compare against.
In the same way I have assigned all 3 integer variables the same value in one hit, I'd like to now make the comparison. It's looking as though there is no way of doing this, so far. I think I'm asking the impossible, I'll have to stick to the basic syntax and keep it simple.
You can create an usefull extension function:
public static bool EqualsAll<T>(this T val, params T[] values)
{
return values.All(v => v.Equals(val));
}
call it like:
bool res = 123.EqualsAll(int1,int2,int3);
You could try something like this, using the logical and operator:
if(int1 == 123 &&
int2 == 123 &&
int3 == 123)
{
}
if(int1 == 123 && int2 == 123 && int3 == 123) { // Code }
What your trying to achieve isn't possible the way you do it.
You have to separate it with &.
if(int1 == something && int2 == something && int3 == 123)
{
// Fantastic code here
}
This is how you should do it using && operator. You can check multiple conditions using this.
UPDATE :
As far as checking multiple values at one go is concerned, you can try making an array out those values and just fire a simple LINQ statement like this to check all of them for a particular value :
if (new[] { int1, int2, int3 }.All(x => x == 1))
Dont know if this fits into your requirement, just a suggestion though.

What is operator "?."? [duplicate]

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

equivalent of Or in C# [duplicate]

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
}

Categories

Resources