Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

usage and explanation of => syntax [duplicate]

Tags:

c#

I saw a lot of samples of C# code with the => syntax.

Can anyone explain about what the usage of this syntax?

select x => x.ID
like image 677
Yu Yenkan Avatar asked May 18 '15 08:05

Yu Yenkan


People also ask

What is the use of duplicate query?

A find duplicates query allows you to search for and identify duplicate records within a table or tables. A duplicate record is a record that refers to the same thing or person as another record. Not all records containing similar information are duplicates.

What does duplicated do in Python?

The duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not.

What is duplicate function?

The duplicated() function is used to indicate duplicate Series values. Duplicated values are indicated as True values in the resulting Series. Either all duplicates, all except the first or all except the last occurrence of duplicates can be indicated. Syntax: Series.duplicated(self, keep='first')

What is duplicate key in SQL?

The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.


2 Answers

can anyone explain about what the usage of => syntax?

The "fat arrow" syntax is used to form something called a Lambda Expression in C#. It is a mere syntactical sugar for a delegates creation.

The expression you provided doesn't make any sense, but you can see it used alot in LINQ:

var strings = new[] { "hello", "world" };
strings.Where(x => x.Contains("h"));

The syntax x => x.Contains("h") will be inferred by the compiler as a Func<string, bool> which will be generated during compile time.


Edit:

If you really want to see whats going on "behind the scenes", you can take a look at the decompiled code inside any .NET decompiler:

[CompilerGenerated]
private static Func<string, bool> CS$<>9__CachedAnonymousMethodDelegate1;

private static void Main(string[] args)
{
    string[] strings = new string[]
    {
        "hello",
        "world"
    };

    IEnumerable<string> arg_3A_0 = strings;
    if (Program.CS$<>9__CachedAnonymousMethodDelegate1 == null)
    {
        Program.CS$<>9__CachedAnonymousMethodDelegate1 = new Func<string, bool>
                                                             (Program.<Main>b__0);
    }
    arg_3A_0.Where(Program.CS$<>9__CachedAnonymousMethodDelegate1);
}

[CompilerGenerated]
private static bool <Main>b__0(string x)
{
    return x.Contains("h");
}

You can see here that the compiler created a cached delegate of type Func<string, bool> called Program.CS$<>9__CachedAnonymousMethodDelegate1 and a named method called <Main>b__0 which get passed to the Where clause.

like image 184
Yuval Itzchakov Avatar answered Oct 07 '22 17:10

Yuval Itzchakov


This syntax is used by lambda expressions - https://msdn.microsoft.com/en-us/library/bb397687.aspx

delegate int del(int i);
static void Main(string[] args)
{
    var myDelegateLambda = x => x * x;  // lambda expression
    Func<int, int> myDelegateMethod = noLambda; // same as mydelegate, but without lambda expression
    int j = myDelegateLambda(5); //j = 25
}

private int noLambda(int x)  
{
    return x * x;
}

As you can see, the lambda expression is very useful to convey simple delegates.

like image 33
Pawel Maga Avatar answered Oct 07 '22 18:10

Pawel Maga