zoukankan      html  css  js  c++  java
  • 匿名方法和匿名对象

    // Declare a delegate.
    delegate void Printer(string s);

    class TestClass
    {
        static void Main()
        {
            // Instatiate the delegate type using an anonymous method.
            Printer p = delegate(string j)
            {
                System.Console.WriteLine(j);
            };

            // Results from the anonymous delegate call.
            p("The delegate using the anonymous method is called.");

            // The delegate instantiation using a named method "DoWork".
            p = new Printer(TestClass.DoWork);

            // Results from the old style delegate call.
            p("The delegate using the named method is called.");
        }

        // The method associated with the named delegate.
        static void DoWork(string k)
        {
            System.Console.WriteLine(k);
        }
    }
    /* Output:
        The delegate using the anonymous method is called.
        The delegate using the named method is called.
    */


    匿名对象

    而在C# 3.0中,我们无须为这些无关紧要的类型浪费时间。通过使用“匿名类型”,只要在需要一个这样的对象时使用没有类型名字的new表达式。var b1 = new { Name = "The First Sample Book"Price = 88.0f };  

    1. var b2 = new { Price = 25.0f, Name = "The Second Sample Book" };  
    2. var b3 = new { Name = "The Third Sample Book"Price = 35.00f };  
    3.  
    4. Console.WriteLine(b1.GetType());  
    5. Console.WriteLine(b2.GetType());  
    6. Console.WriteLine(b3.GetType()); 


     

  • 相关阅读:
    设计模式之开篇(C#语法) 爱拼才会赢
    C#语法糖之第一篇:自动属性&隐式类型 爱拼才会赢
    C#语法糖之第四篇: 扩展方法 爱拼才会赢
    C#中this在扩展方法的应用
    笔记20120215_转义字符_运算符
    Linux基本操作
    SQL创表
    软件测试的概念
    java 线程池
    Redis广播
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/2106806.html
Copyright © 2011-2022 走看看