Evolution from Comparison/Converter to Func

Source: Internet
Author: User

This article introduces two methods of delegation that have already appeared in. net2.0 and are very "old.

In general, we use these functions in the Array and List <T> classes. Understanding the usage and functions can help you use them freely.

However, you can also useFuncFamily form-new features introduced after. NET 2.0.

By understanding this basic information, we can see a part of the. net evolution process. I call this content "history ".

 

I hope you can talk more about it.

Comparison <T>

It appears to execute type-safe sorting for Array and List <T>.

Note that OrderBy () and ThenBy () of the Linq function do not support comparison <T> delegation.

See the following example:

using System;
using System.Collections.Generic;
using System.Linq;


public class Hero
{
public string Name { get; set; }
public int Age { get; set; }
public double Power { get; set; }
}

class Program
{
static void Main(string[] args)
{
var heroes = new List<Hero>
{
new Hero {Name = "Goku", Age = 60, Power = 1000.00},
new Hero {Name = "Vegeta", Age = 65, Power = 800.00},
new Hero {Name = "Ikki", Age = 22,Power = 345.30},
new Hero {Name = "Dr. Slump", Age = 40, Power = 1.23},
};

Console.WriteLine("***** sort by age ****");
heroes.Sort((lhs, rhs) => Comparer<int>.Default.Compare(lhs.Age, rhs.Age));
Print(heroes);
Console.WriteLine("***** sort by name ****");
heroes.Sort((lhs, rhs) => string.Compare(lhs.Name, rhs.Name));
Print(heroes);
Console.WriteLine("***** sort by power ****");
heroes.Sort((lhs, rhs) => Comparer<double>.Default.Compare(rhs.Power, lhs.Power));
Print(heroes);

Console.Read();
}

private static void Print(IEnumerable<Hero> heroes)
{
foreach (var hero in heroes)
{
Console.WriteLine(hero.Name + "(" + hero.Age + ")");
}
}
It is easy to understand the preceding sorting methods. When using the new Func, the implementation is as follows:

 

Console. WriteLine ("****** sort by name (Func )****");
Var heroesOrderByName = heroes. OrderBy (t => t. Name );
Print (heroesOrderByName );

In this case, Comparison <T> is rarely used because of the heavy workload of the code and complicated implementation.

Converter <TInput, TOutput>

Converter is used to operate a certain type of list or array and convert it to another type of list or array.

Continue to use the above Code and insert:

Console.WriteLine("***** TestFunc ****");
TestFunc(heroes);

Console.WriteLine("***** TestConverter ****");
TestConverter(heroes);

Console.WriteLine("***** TestLinqSelect ****");
TestLinqSelect(heroes);

private static void TestFunc(List<Hero> heroes)
{
Func<Hero, string> selector = p => p.Name;

IEnumerable<string> names = heroes.Select(selector);

print(names);
}

private static void TestLinqSelect(List<Hero> heroes)
{
var names = heroes.Select(p => p.Name);

print(names);
}

private static void TestConverter(List<Hero> heroes)
{
var names = heroes.ConvertAll(p => p.Name);

print(names);
}


private static void print(IEnumerable<string> names)
{
foreach (string name in names)
Console.WriteLine(name);
}
In fact, the above TestLinqSelect and TestFunc are the same thing. I just want to provide you with different code solutions. generally, TestFunc is not used. note that using the Select statement of Linq is the same as using some other Linq functions, which means that they will not be converted until the values are used. this is actually a double-edged sword. Sometimes it can get better efficiency, but if you want the results immediately, it will lead to additional background work overhead-the delayed execution function itself needs to use yield return/yield break to maintain the current state of the information iterator. summary

In essence, Comparison/Converter is not much different from the corresponding Func, but why. net to make them exist at the same time, or in other words, why should we introduce the latter "redundant" after the implementation scheme?

This is because the introduced commissioned Func <> is a multi-functional weapon, but the effect is slightly different when executing a specific task.

In new projects, we are more inclined to use new methods.

References: C #/. NET Little Wonders: The Predicate, Comparison, and Converter Generic Delegates

This article is from ASP. NET (Alex Song) of joy. Please indicate the source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.