C#: What's the "=> . <=" Operator? [duplicate] - c#

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;
}

Related

Why am I getting a result even though int value exceeds the limit? [duplicate]

This question already has answers here:
What is an integer overflow error?
(10 answers)
What is the C# "checked" keyword for?
(1 answer)
Closed last month.
This is just a basic sum method.
public int Sum(int a, int b)
{
return a + b;
}
If I call the function as below
int result = Sum(int.MaxValue, int.MaxValue);
Why am I gettin ‘-2’ in the result variable even though there is an overflow. Shouldn't I get a stackoverflow exception?

C# null or null-conditional operator in boolean expression [duplicate]

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.

Calling functions in C# ternary operator [duplicate]

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.

How can i declare variables inside LINQ method [duplicate]

This question already has answers here:
C# Declare variable in lambda expression
(3 answers)
Closed 7 years ago.
How can i declare variables in below method
For example:
double price = double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text);
double total = GvProducts.Rows
.Cast<GridViewRow>()
.Where(r => ((CheckBox)r.FindControl("chkSel")).Checked)
.Sum(r =>
double.Parse(((TextBox)r.FindControl("txtQuantity")).Text) *
double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text));
When any method expects a Func you have two possibilites (or to be exact three).
Add a single-line-expression (as already done)
Add the name of a delegate:
Sum(x => MyMethod(x))
Where MyMethod is a method within your class returning an int and expecting a T).
Add a multi-line expression embraced in curly brackets:
Sum(x => { /* any statements */ })

Creating a custom operator - '!?' (negation of '??'). Is it possible? [duplicate]

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.

Categories

Resources