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
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:
How can I get a character in a string by index?
(2 answers)
Closed 5 years ago.
string qwe = "ABCD";
if(qwe.StartsWith("A") && qwe.EndsWith("D"))
{
MessageBox.Show("Message");
}
What I need is to make also a decision for B and C but not StartsWith and EndsWith, it's really hard for me to explain but just like this:
if(qwe.Second("B"))
{
//Do anything
}
and
if(qwe.Third("C"))
{
//Do anything
}
You know that you can access characters via index(zero based)?
if(qwe.Length >= 2 && qwe[1] == 'B')
{
//Do anything
}
if(qwe.Length >= 3 && qwe[2] == 'C')
{
//Do anything
}
This question already has answers here:
Comparing a variable to multiple values [duplicate]
(7 answers)
Closed 9 years ago.
Let's say I have an if that goes like this:
if(condition1 != asd && menu != menu1 && menu != menu2)
Can this be shortened to something like:
if(condition1 != asd && menu != (menu1 && menu2))
I tried putting parentheses around the second condition either around the two that it should not be, and around the whole thing but it does not pass the compiler.
Is it possible to shorten the second condition this way? Or is there a better way to do it?
PS. I had no idea what to call this, so feel free to edit the title.
Imagine if you were comparing numbers:
if (myNumber != 5 && myNumber != 10)
You could add the numbers being compared to a list first, then use a .Contains():
var forbiddenNumbers = new List<int> { 5, 10 };
if (!forbiddenNumbers.Contains(myNumber))
I don't see any reason you couldn't do the same with your menus, as you're simply comparing object references. (I'm not sure what the type is here, so I simply chose "Menu".)
var menusToCheck = new List<Menu> { menu1, menu2 };
if (!allowedMenus.Contains(menu))
You can use this extension-method:
public static class EqualsAnyExtension
{
public static bool EqualsAny<T>(this T value, params T[] items)
{
return items.Contains(value);
}
}
Use it like this:
if (condition1 != asd && !menu.EqualsAny(menu1, menu2))
No, you can't do this like the second statement. Because
menu != (menu1 && menu2)
Would mean "compare menu with boolean AND operation on menu1 and menu2".
While
menu != menu1 && menu != menu2
means "apply AND operation to results of comparison operation on menu and menu1 and comparison operation on menu and menu2".
The logical operators yield boolean results. Therefore:
(menu1 && menu2)
evaulates to true or false depending on what the values of the variables are.
If menu1 or menu2 are not boolean then you'll need to do a comparison to yield a boolean, as you did in your first go:
if(condition1 != asd && menu != menu1 && menu != menu2)
You have not specified the data type of menu and the answer could depend on that.
For example if menu,menu1 and menu2 are all boolean then this could make some logical sense
if(menu != (menu1 && menu2))
(amounts to if menu1, menu2 and menu are all the same boolean value)
However if they are strings (or in fact anything other than booleans), then it does not make sense. You could still shorten it, but I wouldn't recommend it for 2 items:
var menuItems = new string[]{menu1,menu2}
if(condition1 != asd && menuItems.Contains(menu))
{
...
}
(menu1 && menu2)
in
if(condition1 != asd && menu != (menu1 && menu2))
both menu1 and menu2 need to be of bool type otherwise it throws
CS0019: Operator '&&' cannot be applied to operands of type
I think it cannot be shortened anymore unless all of them are boolean values, when you can use XOR operator between menu1 and menu2.
var menusToCheck = new List<Menu> {"abc","def","eee","sdds" };
if (!menusToCheck .Contains(menu))
{
..spin..
}
Like this.
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;
}