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
Related
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.
This question already has answers here:
What are Range and Index types in C# 8?
(2 answers)
Closed 2 years ago.
int[] numbers = new int[10];
numbers[9] = 10;
Console.Write(numbers[^1] == numbers[numbers.Count()-1]); //true
How does index of ^1 returns the last item in an array?
What does ^1 mean in C# compiler?
Does it have performance benefit vs numbers[numbers.Count()-1]?
numbers[^1] is the same with numbers[length-1]
They are called ranged operators in C# 8. Check this link for more details.
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))
This question already has answers here:
Is there an exponent operator in C#?
(9 answers)
Closed 8 years ago.
I'm doing this equation
double1 * ((double2/double3) ^ 2.333)
getting error
operator '^' cannot be used on type double.
what variable should I use instead of double?
Use Math.Pow(value, exponent) instead.
Math.Pow(3,2) outputs 9
The operator you attempted to use (^) cannot be used for this purpose. It is a logical XOR and should be used for bitwise exclusive OR operations.
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.