简体   繁体   English

C#Func委托与Lambda表达式组合使用不正确

[英]C# Func delegate in combination with lambda expression not correct

I have a class Employee, with some basic params, and a class employees that has a list of employee objects. 我有一个Employee类,其中包含一些基本参数,还有一个class雇员,其中有一个employee对象列表。 I want to use the func delegate in the class employees to be able to use the following lambda expression in the main class. 我想在班级员工中使用func委托,以便能够在主类中使用以下lambda表达式。 I think I have some syntax mistakes here... I'am getting the following error message: "cannot convert lambda expression to type 'Employee' because it is not a delegate type". 我想我在这里有一些语法错误...我收到以下错误消息:“无法将lambda表达式转换为类型'Employee',因为它不是委托类型”。

Someone knows what I'am doing wrong? 有人知道我做错了吗?

Many thanks guys !! 非常感谢你们!

static void Main(string[] args)
        {
            Employees lijst = new Employees();
            lijst.Add(new Employee("B1", 200));
            lijst.Add(new Employee("B2", 100));
            lijst.Add(new Employee("B3", 300));

            lijst.Wijzig((Employee b) => b.Salary += 100);

            Console.ReadKey();
        }


class Employee
    {
        public String Name { get; set; }
        public int Salary { get; set; }



        public Employee(String Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;

        }
    }


class Employees
    {
        public Func<Employee, int> Wijzig = new Func<Employee, int>(Change);
        private ArrayList _lijst = new ArrayList();

        public void Add(Employee e)
        {
            _lijst.Add(e);
        }

        static int Change(Employee b)
        {

            return 0;
        }
    }

Wijzig is a delegate of type Func<Employee, int> . WijzigFunc<Employee, int>类型的委托。 Ie it's a method which accepts employee object and returns an integer. 也就是说,这是一种接受员工对象并返回整数的方法。 So lijst.Wijzig returns this delegate. 因此, lijst.Wijzig返回了此委托。 If you want to invoke this delegate, you should pass employee object (according to signature Func<Employee, int> ), but you are passing lambda delegate instead of an employee. 如果要调用此委托,则应传递employee对象(根据签名Func<Employee, int> ),但是要传递lambda委托而不是雇员。 Correct invocation: 正确的调用:

  lijst.Wijzig(someEmployee);  // calls Employees.Change

That will invoke a int Change(Employee b) method. 这将调用一个int Change(Employee b)方法。 But seems like you don't want to invokde Change method (which does nothing, except returning zero). 但是似乎您不想调用Change方法(除了返回零外,它什么也不做)。 You want to invoke some real change - increment of salary. 您想调用一些实际的更改-工资增加。 So, the first step you should do is change the delegate wich is assigned to Wijzig , and then invoke this delegate with your employee object: 因此,您应该做的第一步是更改分配给Wijzig的委托,然后用employee对象调用此委托:

 lijst.Wijzig = (Employee b) => { b.Salary += 100; return 0; }
 lijst.Wijzig(someEmployee); // calls lambda which we assigned

Notes: 笔记:

  • there is no sense to use ArrayList in .NET 2.0+ you should use generic collection like List<Employee> instead. 在.NET 2.0+中使用ArrayList是没有意义的,您应该改用List<Employee>类的通用集合。
  • it's not clear why your Change method returns an integer. 尚不清楚您的Change方法为何返回整数。 Consider making it void. 考虑使其无效。
  • you are re-implementing functionality LINQ - there is a ForEach extension method which accepts an action to be performed on each item of the collection. 您正在重新实现LINQ功能-有一个ForEach扩展方法,该方法接受要对集合的每个项目执行的操作。

      var list = new List<Employee> { new Employee("B1", 200), new Employee("B2", 100), new Employee("B3", 300) }; list.ForEach(e => e.Salary += 100); 

That's it. 而已。 All your code without custom classes. 您的所有代码都没有自定义类。

Here is my solution that has no change in the Main function: 这是我的解决方案,在Main函数中没有任何变化:

    static void Main(string[] args)
    {
        Employees lijst = new Employees();
        lijst.Add(new Employee("B1", 200));
        lijst.Add(new Employee("B2", 100));
        lijst.Add(new Employee("B3", 300));

        lijst.Wijzig((Employee b) => b.Salary += 100);

        lijst.Print();

        Console.ReadKey();
    }


    class Employee
    {
        public String Name { get; set; }
        public int Salary { get; set; }

        public Employee(String Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
    }


    class Employees
    {
        // [Delete]
        // public Func<Employee, int> Wijzig = new Func<Employee, int>(Change);
        private ArrayList _lijst = new ArrayList();

        public void Add(Employee e)
        {
            _lijst.Add(e);
        }

        // [Delete]
        //static int Change(Employee b)
        //{
        //    return 0;
        //}

        // [New]
        public void Wijzig(Func<Employee, int> func) 
        {
            foreach (Employee e in _lijst)
            {
                func(e);
            }
        }

        public void Print() 
        {
            foreach (Employee e in _lijst)
            {
                Console.WriteLine(e.Name + "\t" + e.Salary);
            }
        }
    }

Your below code line to invoke func 您的以下代码行调用func

lijst.Wijzig((Employee b) => b.Salary += 100);

should just be below, passing an Employee object 应该在下面,并传递Employee对象

int result = lijst.Wijzig(lijst[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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