C# Conditional operator [duplicate] - c#

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.

Related

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

What is the negation of the test "is"? [duplicate]

This question already has answers here:
C# : 'is' keyword and checking for Not
(13 answers)
Closed 4 years ago.
To test if an object is of a certain type then we write if ( myObject is SomeClassName )
But how do we write if I want to test that the object is not of the mentionned type ?
if (!(myObject is SomeClassName))
Is probably the cleanest way of checking not is. Just checks if it is the type of object then flips it.
There isn't one, you have to wrap it in a ! operator:
if (!(myObject is SomeClassName))

Evaluate the string as math expression [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance

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

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") }"

C# how to detect operators in string to calculate? [duplicate]

This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
Closed 9 years ago.
Say a program receives an input string "8*10+6/2" and should output 83, in this case. how to handle the operator?
I can chop the string into individual strings, then detect whether it is a number or operator. If it is an operator I can convert it to int. But I have no idea how to handle the operator so that the calculation works.
You could use the DataTable.Compute-"trick":
double result = (double)new DataTable().Compute("8*10+6/2", null);
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations in: DataColumn.Expression at Expression Syntax.

Categories