This question already has answers here:
Shortest way to check for null and assign another value if not
(12 answers)
Closed 2 years ago.
Is it possible to execute a function within a short statement if the statement is true?
Something like that:
myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
yes:
if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);
At just 1 single character more than your original.
Related
This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.
This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
What does the ? operator mean in C# after a type declaration?
(7 answers)
Closed 3 years ago.
I am reading this article and it has this example
int? length = people?.Length; // null if people is null
My question is if the first question mark after the int a typo ? If not what is the purpose of introducing a ? after an int. I understand why its introduced after people. The reason for that is if people is null then it will return a null. Can someone explain the purpose of the ?
This question already has answers here:
What is a method group in C#?
(5 answers)
Closed 4 years ago.
I am using the below code to check if two lists are equal.
var a = ints1.All(ints2.Contains) && ints1.Count == ints2.Count;
The only thing I do not understand is how does ints2.Contains work. As far as I know, Contains() is a method and takes a parameter. As we can see here, Contains is not taking any parameter.
Note - ints1 and ints2 are two different lists.
That's called a method group. It's basically a shorcut for this:
ints1.All(x => ints2.Contains(x))
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've encountered this operator in C# when dealing with custom events: MyEvent?.Invoke(this, new EventArgs());. What is the purpose of the ?. portion of this statement?
It's making sure it's not null. They have these in swift and their called optionals. If the variable is null then it returns null
This question already has answers here:
How can I evaluate a math expression represented by a string?
(6 answers)
Closed 9 years ago.
Is there any method in C# in which you will pass a mathematical statement, and that method will give you the result for example you will pass this string
1+2-3/4*5
and it will give you in return
0
Use NCalc
Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());