简体   繁体   中英

Difference between these 2 function calls

Getting some practice in with delegates specifically using the Func keyword and noticed that I can make the same function call in 2 different ways.

public class TestClass
{
    public string Name { get; set; }
    public string Description { get; set; }

    /// <summary>
    ///     Creates a SelectListItem
    /// </summary>
    /// <param name="function">Function which retrieves the data to populate the list</param>
    /// <param name="key">The column name which contains the index/uniqueidentifier which is used to identify the object</param>
    /// <param name="displayText">Column name for the text that will appear on the dropdown list</param>
    /// <returns> A List of SelectListItem</returns>

    public static List<SelectListItem> CreateList<T>(Func<List<T>> function, string key, string displayText)
    {
        var list = new List<SelectListItem>();
        var model = function();
        foreach (var item in model)
        {
            var property = item.GetType().GetProperty(key);
            var text = item.GetType().GetProperty(displayText);
            if (property != null && text != null)
            {
                list.Add(new SelectListItem()
                {
                    Text = text.GetValue(item, null).ToString(),
                    Value = property.GetValue(item, null).ToString()
                });
            }
        }
        return list;
    }
    public  List<TestClass> GetList()
    {
        List<TestClass> test = new List<TestClass>();
        var t = new TestClass
        {
            Name = "T",
            Description = "P"
        };
        test.Add(t);
        return test;
    }

}

Called like:

var obj = new TestClass();

TestClass.CreateList(() => obj.GetList(), "Name", "Description");
TestClass.CreateList(obj.GetList, "Name", "Description");

Is there any difference between the way they are called? They both seem to run in the exact same way, are there efficiency differences between them? Explanation would be appreciated :-)

The argument calls for a function.

This passes the function.

TestClass.CreateList(obj.GetList, "Name", "Description");

This passes an anonymous function that calls the function.

TestClass.CreateList(() => obj.GetList(), "Name", "Description");

The additional syntax of the second one is unnecessary. Resharper will offer to replace the second one with the first one.

In the first case you are passing Lambda expression , while in the second one function to delegate . No there is no difference in efficience.

The => token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax Source MSDN

A delegate is a reference type that can be used to encapsulate a named or an anonymous method. Delegates are similar to function pointers in C++; however, delegates are type-safe and secure. For applications of delegates, see Delegates and Generic Delegates. Source MSDN

You can read about this differences here delegates-actions-funcs-lambdaskeeping-it-super-simple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM