C# - Why can't I use ternary operation inside string? [duplicate] - c#

This question already has an answer here:
How to use the ternary operator inside an interpolated string?
(1 answer)
Closed 5 years ago.
I'm trying to write
$"This is { awesomeEnough ? "awesome" : "not awesome"}"
but I'm getting compiler error
CS1003 Syntax error, ':' expected
is it expecting behavior or bug?

Operator priority. Parenthesis should solve the problem
$"This is { (awesomeEnough ? "awesome" : "not awesome") }"

Related

why you put a ? in Console.ReadLine() and ToLower() [duplicate]

This question already has answers here:
What does question mark and dot operator ?. mean in C# 6.0?
(3 answers)
Closed 7 months ago.
So I just done it as a habit now but I want to know how it really works. Console.ReadLine()?.ToLower() ?? ""; So why do you put a ? in between .ToLower() and Console.ReadLine(). I know that after the .ToLower() you put ?? ""; to make a null input into a string. But what about the other question mark?
Single question mark (?) means null check.
Basically, if Console.ReadLine is null, the other part will not be implemented.
In other words, Console.ReadLine()?.ToLower() is equal to this
var dummy = Console.ReadLine();
if(dummy != null)
{
dummy.ToLower()....
}

C# Conditional operator [duplicate]

This question already has answers here:
Using ternary operator on Console.WriteLine
(4 answers)
Closed 2 years ago.
age < 18 ? Console.WriteLine("Too Young") : Console.WriteLine("Old enough");
I am trying to get this line of code to run but keep getting CS0201 error on visual studio. Whats wrong?
The conditional operator requires an expression. Console.WriteLine is not an expression.
But a string is an expression, so you can say:
Console.WriteLine(age< 18 ? "Too Young" : "Old enough")
Or use an if-statement.

C# tertiary operator solve [duplicate]

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

handling null and string.empty using ?? operator in C# [duplicate]

This question already has answers here:
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 4 years ago.
I want to handle "" and null while assigning value to the property of the class.
So how can i handle the same. Below is my example which works for null. But also want to handle empty string
Id = characater.Id ?? System.Guid.NewGuid().ToString(),
Use string.IsNullOrEmpty along with the ?: Operator.
Id = string.IsNullOrEmpty(characater.Id)
? System.Guid.NewGuid().ToString()
: characater.Id;
If you you also want to check for white space characters line spaces, line breaks, tabs, you can use String.IsNullOrWhiteSpace instead.

Is it okay to check for null with 'is' keyword? [duplicate]

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?

Categories