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. I want to use the func delegate in the class employees to be able to use the following lambda expression in the main class. 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".

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;
        }
    }

这是我的解决方案,在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);
            }
        }
    }

Wijzig is a delegate of type Func<Employee, int> . Ie it's a method which accepts employee object and returns an integer. So lijst.Wijzig returns this delegate. 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. Correct invocation:

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

That will invoke a int Change(Employee b) method. But seems like you don't want to invokde Change method (which does nothing, except returning zero). 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:

 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.
  • it's not clear why your Change method returns an integer. 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.

        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.


    Your below code line to invoke func

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

    should just be below, passing an Employee object

    int result = lijst.Wijzig(lijst[0]);
    
    链接地址: http://www.djcxy.com/p/31598.html

    上一篇: 浮点Div / Mul>比Add / Sub慢30倍?

    下一篇: C#Func委托与lambda表达式组合不正确