This question already has an answer here:
C# syntax shorthand for multiple condition testing [duplicate]
(1 answer)
Closed 6 years ago.
I have the following statement in C#
if(user.member.RegistrationDate.Value.Month == 10 || user.member.RegistrationDate.Value.Month == 11)
My question is, is there a way to write this in shorthand eg;
if(user.member.RegistrationDate.Value.Month == 10 || 11)
How about
if (new []{10,11}.Contains( user.member.RegistrationDate.Value.Month))
Related
This question already has answers here:
How to elegantly check if a number is within a range?
(33 answers)
Is there a "between" function in C#?
(18 answers)
C# Variable Name "_" (underscore) only
(5 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Is there a simpler or alternate way to code the following C# sentence?
if (iValue >= 4 && iValue <= 10) {... other code ...}
Do I need to repeat the iValue twice?
I have seen a structure similar to _ = ...some code... but I am not familiar with that leading underscore?
With C#9 you can use:
if(iValue is >= 4 and <= 10)
{
Console.WriteLine("in range");
}
This question already has answers here:
question mark inside C# syntax [duplicate]
(7 answers)
How Ternary operator works in C# [duplicate]
(3 answers)
Closed 3 years ago.
Can any one tell me the meaning of the below C# code
true? para1:para2;
For example,
char x = 'A';
Console.WriteLine(true? x : 0);
The console prints 65
I do not understand how it works.
Its a ternary operator
condition? 'execute this part if condition satisfies':'execute this part if condition not satisfies'
In your example Console.WriteLine(true? x : 0);
if something is true,it writes A(as the value of x is A) else 0
This question already has answers here:
What is the difference between "x is null" and "x == null"?
(3 answers)
What is a difference between "foo is null" and "foo == null"
(2 answers)
Closed 4 years ago.
In a code review that I have made today to one of my team mates, I have seen him checking properties for null with the is keyword, like:
if(myVariable is null)
{
//do something
}
I was tempted to disagree of this type of checking, but I don't feel so confident on my knowledge about it. Is it really okay to check for null like this?
This question already has answers here:
Test for multiple values in an if statement in C# [duplicate]
(5 answers)
Closed 6 years ago.
What syntax exists in C# to help with multiple condition testing?
I often have to test for multiple conditions in the following manner:
if (a == 3 || a == 4)
Perhaps I'm being subjective, but that isn't very pretty.
If there is a larger set of conditions, I could do:
if (new int[]{3, 4, 5, 6, 7}.Contains(a))
But if there are just a few conditions to test, I'm not saving any keystrokes.
Is there a syntax shortcut in the C# language that would allow me to accomplish something like the following, without many keystrokes and without extension methods, etc?
// doesn't work
if (a == 3 || 4)
This is different from this SO thread, because it is dealing with short conditions (2 or 3), all with an unchanging "a" value in an "a compare to b" comparison.
No there is not. Sorry, but this is your answer.
This question already has answers here:
C# equivalent to InStrRev
(3 answers)
Closed 7 years ago.
I need to rewrite
Microsoft.VisualBasic.Strings.Mid(pReturn, (InstrRev(pReturn, ".") + 1), (pReturn).Length)).Length > 4
to C# but can't find a concise solution for InstrRev.
How can I convert this line?
String.LastIndexOf would be the equivalent.