Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# lambdas

//Lambdas are just short handed anonymous method or functions usally 
//for use in passing to other methods or functions to call such as a sort.
//Example:
//The Lambda used here after the =s
Action ExampleContainerForDelagate = ()=>
	{/*Do something such as this example*/
    	Console.WriteLine("Hello world");
	};
//has been shorted from this anonymous delegate or method.
Action ExampleContainerForDelagate = delegate () { Console.WriteLine("Hello world"); };
//And this full (named) method signature method passing
Action ExampleContainerForDelagate = ExampleMethod;
void ExampleMethod()
{ Console.WriteLine("Hello world"); }
//usage example
List<int> ExampleList = new List(){1,3,0};
ExampleList.Sort((x,y)=>{return x - y; });
//The Sort will sort based on the return values from the Lambda passed.
//a negative will be seen as a smaller than the next number
//a 0 will be seen as equal
//and a positive will be seen as bigger than the next number
//by switching x and y postion in the 'return x - y;' you will effectively 
//change the sort to descending order. This is there for useful for sorting 
//based on needs or where it is not obvious how to sort.
Comment

C# program lambda Func

using System;

class Program
{
    static void Main()
    {
        // Part 1: use implicitly-typed lambda expression.
        // ... Assign it to a Func instance.
        Func<int, int> func1 = x => x + 1;
        Console.WriteLine("FUNC1: {0}", func1.Invoke(200));
        
        // Part 2: use lambda expression with statement body.
        Func<int, int> func2 = x => { return x + 1; };
        Console.WriteLine("FUNC2: {0}", func2.Invoke(200));
        
        // Part 3: use formal parameters with expression body.
        Func<int, int> func3 = (int x) => x + 1;
        Console.WriteLine("FUNC3: {0}", func3.Invoke(200));
        
        // Part 4: use parameters with a statement body.
        Func<int, int> func4 = (int x) => { return x + 1; };
        Console.WriteLine("FUNC4: {0}", func4.Invoke(200));
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: C# xamaring form change text on label 
Csharp :: c# destroy function...unity 
Csharp :: convert list string to list enum c# 
Csharp :: remove scenedelegate 
Csharp :: .net 4.5 use tls 1.2 
Csharp :: remove duplicates in the list using linq 
Csharp :: Printing pattern in c# 
Csharp :: loop for specific time c# 
Csharp :: how to populate list in c# 
Csharp :: c# get smallest of 3 numbers 
Csharp :: c# check characters in string 
Csharp :: setting the parent of a transform which resides in a prefab 
Csharp :: how to change text in richtextbox wpf 
Csharp :: c# find comma in text and remove 
Csharp :: unitry raycast 
Csharp :: allow scroll with wheel mouse datagridview c# 
Csharp :: unity draw waypoins path 
Csharp :: unity DOScale 
Csharp :: how to create a blazor client-side application in a command-line interface 
Csharp :: unity color alpha not working 
Csharp :: demand a Security action c# 
Csharp :: c# if string in licbox 
Csharp :: unity2d switch camera 
Csharp :: c# mock ref parameter 
Csharp :: query parameters sending to controller action asp.net core 
Csharp :: print text c# unity 
Csharp :: serilog asp.net 5 
Csharp :: how create two database conction in laravel 
Csharp :: unity camera.main.screentoworldpoint(input.mouseposition) not working 
Csharp :: How to execute script in C# 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =