C# : Lambda

[".0s4*0|collections;datetime-format;lambda",["F@eTU^","LCKAECBEBEMA","OBOBCCSTUUUUTTUUUUYGDGDGDGHDGDFOCOBCOBBBBWSTTUUUUTTUUUU","ehwtt.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","Comparison."," The Comparison type enables custom sorting. We implement Comparison(T) using its constructor. The target is a method that receives two parameters of the appropriate type and returns an int. It determines sorting order of the parameters. ","Generic Class ","generic","Example."," This program introduces two static methods. In the Main entry point control flow begins. And the CompareLength method receives two strings and returns an int. In the Main entry point, we create an array of strings and then display it. ","String Array ","array","Then: ","We create a new Comparison<string> using the CompareLength method as the target.","Finally: ","We sort the array again using this Comparison delegate and display it.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses Comparison delegate","\n\nusing System;\n\nclass Program\n{\n static int CompareLength(string a, string b)\n {","\n // Return result of CompareTo with lengths of both strings.\n ","return a.Length.CompareTo(b.Length);\n }\n\n static void Main()\n {","\n // Source array.\n ","string[] array =\n {\n \"bird\",\n \"elephant\",\n \"dog\",\n \"cat\",\n \"mouse\"\n };","\n\n // Display array.\n ","foreach (string element in array)\n {\n Console.WriteLine(element);\n }\n Console.WriteLine();","\n\n // Create Comparison instance and use it.\n ","Comparison","<string> comparison = new Comparison<string>(CompareLength);\n Array.Sort(array, comparison);","\n\n // Display array.\n ","foreach (string element in array)\n {\n Console.WriteLine(element);\n }\n }\n}\n\n","Output","\n\nbird\nelephant\ndog\ncat\nmouse\n\ncat\ndog\nbird\nmouse\nelephant","Requirements."," The Comparison(T) type requires that you specify a method that receives two parameters of type T and then returns an int. You pass the method name to the constructor. ","Tip: ","You can often implicitly use the Comparison delegate by just passing the target method name.","How do you implement"," the code inside the target method (CompareLength)? You typically use the CompareTo method on a property, field, or other information derived from each type. Here, we use CompareTo with the lengths of both strings. ","CompareTo Int Method ","compareto","Tip: ","To reverse the sort, just reverse the order of the CompareTo expression variables.","Discussion."," There are many other examples of using Comparison. You can use Comparison without specifying the constructor explicitly. It is compatible with the lambda expression syntax. Also it can be used with the List.Sort or Array.Sort methods. ","Sort KeyValuePair List ","sort-keyvaluepair","Sort Tuple ","tuple","Sort List Method ","sort-list","Array.Sort Examples ","array-sort","Summary."," We looked at the Comparison type. We provided an example of the Comparison(T) constructor, but also pointed out that it is not necessary to provide in all cases. We implemented the target method by using the CompareTo method. ","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAB2ANIDASIAAhEBAxEB/8QAHAABAAIDAQEBAAAAAAAAAAAAAAMEAQIFBgcI/8QAORAAAgIBAgQEAggDCQEAAAAAAAECAwQFEQYSITETQVFhB5EUIiMyQlJxgUNysQgVFiQzNFOCktH/xAAaAQEAAwEBAQAAAAAAAAAAAAAAAQIDBAUG/8QAJREBAAICAQUAAQUBAAAAAAAAAAECAxESBBMhMVEVIkFCYZGx/9oADAMBAAIRAxEAPwD9UgAAAAAMcy9UY54fmj8wabA0dta/iQ+Zjxqv+SPzCeM/EgIvpFX54mHlUr8a+Q0njb4zzr1CsKPjr1MeP7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bKYqCqL9a2Q5C3NkQhki62epIPFjHgDjB6hCiIlGJjAMhqK4HCRAJJhLGgbABwUhUIscgzHwBBCJRieyHVCbKItIIzoQUQHYhU0ND2LQXUgla/ATEmRCMRCdiQygB4DLI0hyLrmUPhIFxGQSj8OqRCSEAXAnBSDARG8LzKBCsIsIDG1LSSJBHc26byMOG0DwOBLOARGxIJ3QRIaJrIKHBQYcWhNGQuf4KL4BgJI0p2hBSH51dC2l8HpwWRPD06yHTqjf9EZLcAJDQgYQdEIh8BEjsQOIdIc4rh3e3hOhIJgOiLyNDQEI/ETv2ZrwFyV+ELAqy7IHk/jchhQme+CLE+W4i9gURJMTM1PlSkb4ImTYTsW9R7Al/DRJXXDQgULs9+hqkIQLSgOTxIBiJ3wmp40ECQMJOiKPhIBhJ3AspMhrExXbOgNiSlFoIfw+CMyAg6QWf2VUZHaLGGRWZCvz8E5APrVKW9Pt/WmCkLAKQc/YPKd9C4FftKeoAAAAASUVORK5CYII=)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAAB4BAMAAADS7QcCAAAAGFBMVEX////0qHKhmbLDvs74yKT4xaH4yqr2tYUVUWgbAAAAfUlEQVR42u3XsQmAMBgF4V9xAAOCfVxAdAEdQtwg1il0fsHS8IpIsLob4OvPnKy1V7WXWb+J9pQZZpG3zkRNyowmmmBgYGBgYGBgYGBgYGA+Mn0QHTl3V+o1iYieKifLYq4gilnMYqIVBgYGBgYGBgYGBgYG5gfm3ESx0GvePshxrr29cIIAAAAASUVORK5CYII=)"]

["url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","D","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANM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