C# method declarations using the lambda operator [duplicate] - c#

This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
While researching C# operator overloading, I stumbled across this block of code on the MSDN web site:
public static Complex operator +(Complex c1, Complex c2) =>
new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
// Override ToString() to display a complex number
// in the traditional format:
public override string ToString() => $"{this.real} + {this.imaginary}";
This is a really useful way of defining simple methods in certain circumstances, but I don't recall ever seeing it described anywhere. I tried searching the C# 5.0 language specification for a description of this method declaration syntax, but could find nothing. I also found nothing in my web searches.
Two questions:
In which version of C# did this method declaration syntax become available?
Where in the language specification is this syntax described?

It was added in C# 6, you can read about it on the official Github of the new compiler here.

Related

What exactly means this type of generic usage (dot between function and generic): "var mf: MeshFilter = GetComponent.<MeshFilter>();"? [duplicate]

This question already has an answer here:
UnityScript "generic functions" in Unity 5
(1 answer)
Closed 4 years ago.
I am newbie in C#. I am switching languages from Java to C# and I found different usage of generics. Can someone explain what this dot between function and generic means? What is a difference between function with generic that has this dot and that one without it?
I was searching in google what it may be but I did not find nothing like this. I was searching for it here: https://www.geeksforgeeks.org/c-sharp-generics-introduction/ but without any result.
Here is the code that I do not understand:
function Start() {
var mf: MeshFilter = GetComponent.<MeshFilter>();
...
source: https://docs.unity3d.com/Manual/Example-CreatingaBillboardPlane.html
this isn't c# its something called unityscript, you can easily tell because when declaring a variable you can't have a ':' at the end of the variable name, and you cant specify a generic type after the '.' you have to specify it before, or in somecases the compiler will auto detect it based on the output, like how Task.Run(()=>{return int.MaxValue; }) will be auto compleated by the compiler to
Task.Run<int>(()=>{return int.MaxValue;}).

What is the use of an empty statement [duplicate]

This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.

C# lambda operator for assignment [duplicate]

This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
I came across this in our code-base today, and it took me awhile to see what the effect of it was, but what in the world does this actually mean??
public virtual SomeClass InstanceVariable => new SomeClass("arg1", "arg2");
I played around with this in Visual Studio's C# interactive terminal and discovered that it appears to be equivalent to:
public virtual SomeClass InstanceVariable { get { new SomeClass("arg1", "arg2"); } }
However, I couldn't find any documentation on this being any form of 'syntactic sugar' for a read-only property.
Someone want to shed some light on the scenario?
It is from new C# 6.0. You can instantiate your class by default. Check "Expression Bodied Functions and Properties" of https://msdn.microsoft.com/en-us/magazine/dn802602.aspx article

Is it possbile to implement the index operator '[]' with generic capability [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.

Is there C# version of Microsoft.VisualBasic.CompilerServices.Operators? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
CompilerServices.Operators equivalent on C#
I was looking for Microsoft.CSharp.CompilerServices.Operators but couldn't find it.
No there is no real equivalent to this in the C# runtime assembly.
However many these methods are essentially implementing the late bound operations for VB.Net in a declarative method (and indeed there are cases where the late binder simply just defers to these methods for operations). So these could be replicated in C# by defining methods which just explicitly defer to the C# dynamic binder.
For example, the rough equivalent of DivideObject in C# would be the following
public static dynamic DivideObject(dynamic left, dynamic right)
{
return left / right;
}

Categories