This question already has answers here:
C# Conditional Operator Not a Statement?
(9 answers)
Closed 6 years ago.
Why is this code not valid? Pretty sure it's legit in C /C++
Pseudocode:
String s = Console.ReadLine();
int x = 0;
Int32.TryParse(s, out x) ? Console.WriteLine("Foo") : Console.WriteLine("bar");
The ternary operator is used to return values and those values must be assigned.
If you want to invoke void methods in a ternary operator, you can use delegates like this:
String s = Console.ReadLine();
int x = 0;
(Int32.TryParse(s, out x) ? new Action(() => Console.WriteLine("Foo")) : () => Console.WriteLine("bar"))();
console.writeline return void.. The conditional operator (?:) returns one of two values depending on the value of a Boolean expression
MSDN
As discussed here, in C#, not every expression can be used as a statement.
Related
This question already has answers here:
What does "=>" mean?
(7 answers)
Closed 2 years ago.
In C#, What does the "=> x <=" operator mean?
Where the arrows point towards the variable x in the middle.
The code where I found it in:
public static bool Check(this int x) => x <= 2;
This is two separate operators that look similar, but are quite different. The first part indicates an expression-bodied member (=>), which in this case is being used as shorthand to declare the method implementation. It's equivalent to this:
public static bool Check(this int x)
{
return x <= 2;
}
This question already has answers here:
Why does >= return false when == returns true for null values?
(8 answers)
How to compare nullable types?
(8 answers)
nullable bool in if() statement - checks required?
(5 answers)
Closed 2 years ago.
Can someone explain to me why this works?
List<int> foo = null;
bool bar = foo?.Count > 7;
When foo is null, it becomes equivalent to the following:
bool bar = null > 7;
The IntelliSense is saying both null and 7 are of type Nullable<int>, but how did the compiler decide that?!
Additionally, Nullable<T> doesn't define a greater than operator. How could it? There is no guarantee that T defines a greater than operator.
As Rofus and Jeroin correctly pointed out in comments the conversion between T to T? is always implicit when you do the comparison with "Null" 7 becomes a "Nullable<int>"
bool bar = foo?.Count > 7;
is similar to -
int? count = null;
int? k = 7; // Implicit conversion here!
var bar = count > k;
For your second question on the > operator. Yes, while the Nullable<T> struct does not define operators such as <, >, or even ==, still the following code compiles and executes correctly which is similar to your case -
int? x = 3;
int? y = 10;
bool b = x > y;
This is because compiler lifts the greater than operator from underlying value type like -
bool b = (x.HasValue && y.HasValue) ? (x.Value > y.Value) : false;
Operator Lifting or Lifted Operators means you can implicitly use T's operators on T?. Compiler has different set of rules on different set of operators on handling them on Nullable<T> types.
This question already has answers here:
C# operator overload for `+=`?
(10 answers)
Closed 3 years ago.
I'm from C++ and using C# as newbie, just tried this out:
class Class1
{
int mI = 0;
string mS = "ab";
public static Class1 operator + (Class1 obj1, Class1 obj2)
{
return new Class1()
{
mI = obj1.mI + obj2.mI,
mS = obj1.mS + obj2.mS
};
}
public static void operator += (Class1 obj1)
{
mI += obj1.mI;
mS += obj1.mS;
}
}
I found that operator+= function doesn't compile, saying:
error CS1019: Overloadable unary operator expected.
So C# doesn't do this kind of operator overloading at all?
You can overload +, but not +=, as per the documentation:
Assignment operators cannot be explicitly overloaded. However, when you overload a binary operator, the corresponding assignment operator, if any, is also implicitly overloaded. For example, += is evaluated using +, which can be overloaded.
So, as you can read, += is considered x = x + y. That's why it is not allowed to overload the += operator.
This question already has answers here:
Conditional operator and Comparison Delegate
(2 answers)
Conditional statement, generic delegate unnecessary cast
(4 answers)
Why doesn't the C# ternary operator work with delegates?
(2 answers)
How can I assign a Func<> conditionally between lambdas using the conditional ternary operator?
(4 answers)
Closed 4 years ago.
I have a choice between two statically defined converters, and I want to use a ternary expression to make the choice:
Func<Input, Output> converter = useConverter1
? MyConverter.Converter1
: MyConverter.Converter2;
The compiler will not accept this expression. It fails with a message saying "There is no implicit conversion between 'method group' and 'method group'".
The structure of MyConverteris
public static class MyConverter
{
public static Output Converter1(Input input)
{
....
}
public static Output Converter1(Input input)
{
....
}
}
Can I work around this without an if-else statement?
This question already has answers here:
Is it possible to create a new operator in c#?
(7 answers)
Closed 8 years ago.
Is it possible to create a custom operator like '!?' (negation of '??') instead of writing long expression:
int? value = 1;
var newValue = value != null ? 5 : (int?)null;
I want to have:
var newValue = value !? 5;
Thanks!
No.
You cannot create your own operators in C#. You can only override (some of the) ones that already exist in the language.
You cannot define new operator in C# - the only way is to redefine existing ones, but you can not redefine ?: and ?? operators.