What is an anonymous method?

Anonymous method is a method without a name. Introduced in C# 2.0,they provide us a way of creating delegate instances without having to write a separate method.

class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>() { 
                new Person{ID=101,Name="lin1"},
                new Person{ID=102,Name="lin2"},
                new Person{ID=103,Name="lin3"}
            };

            Person person = persons.Find(
                delegate(Person p)          //this is an anonymous method.
                {
                    return p.ID == 101;
                }
                );
            Console.WriteLine("person id={0},name={1}", person.ID, person.Name);

        }
    }
    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
View Code

相关文章:

  • 2021-12-07
  • 2021-07-13
  • 2022-02-12
  • 2021-05-23
  • 2022-12-23
  • 2021-12-26
  • 2021-12-18
  • 2021-11-16
猜你喜欢
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-06-10
  • 2021-06-12
  • 2021-12-08
  • 2021-08-10
相关资源
相似解决方案