Delegate   Func  Action  Predicate default() 知识点

 看仓储模式,有代码写到这几个关键字,陌生,记录下来。

      定义一个类型,此类型抽象化了相似结构的某一类方法,因此我们能将此类型代表的方法作为参数进行传递。

     Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

  Func可以接受0个至16个传入参数,必须具有返回值 用于泛型

  Action可以接受0个至16个传入参数,无返回值        用于泛型

  Predicate只能接受一个传入参数,返回值为bool类型 用于泛型

 

 

     default(T)   针对泛型默认值的初始化处理。 当T不确认是值类型或引用类型时,他的默认值会给我们造成困扰。既可以是NULL又可能是0。当使用default(T)处理后T temp = default(T);

     temp的默认值会根据T的类型自动赋值。

  //下面代码来自msdn.microsoft.com

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Test with a non-empty list of integers.
            GenericList<int> gll = new GenericList<int>();
            gll.AddNode(5);
            gll.AddNode(4);
            gll.AddNode(3);
            int intVal = gll.GetLast();
            // The following line displays 5. 结果将返回5
            System.Console.WriteLine(intVal);

            // Test with an empty list of integers.用空值进行测试
            GenericList<int> gll2 = new GenericList<int>();
            intVal = gll2.GetLast();
            // The following line displays 0. 结果返回0   
            //此处根据T的类型返回了int型的默认值0
            System.Console.WriteLine(intVal);

            // Test with a non-empty list of strings.
            GenericList<string> gll3 = new GenericList<string>();
            gll3.AddNode("five");
            gll3.AddNode("four");
            string sVal = gll3.GetLast();
            // The following line displays five.
            System.Console.WriteLine(sVal);

            // Test with an empty list of strings.
            GenericList<string> gll4 = new GenericList<string>();
            sVal = gll4.GetLast();
            // The following line displays a blank line.
           //此处根据T的类型返回string的默认值“空白”
            System.Console.WriteLine(sVal);
        }
    }

    // T is the type of data stored in a particular instance of GenericList.
    public class GenericList<T>
    {
        private class Node
        {
            // Each node has a reference to the next node in the list.
            public Node Next;
            // Each node holds a value of type T.
            public T Data;
        }

        // The list is initially empty.
        private Node head = null;

        // Add a node at the beginning of the list with t as its data value.
        public void AddNode(T t)
        {
            Node newNode = new Node();
            newNode.Next = head;
            newNode.Data = t;
            head = newNode;
        }

        // The following method returns the data value stored in the last node in
        // the list. If the list is empty, the default value for type T is
        // returned.
        public T GetLast()
        {
            // The value of temp is returned as the value of the method. 
            // The following declaration initializes temp to the appropriate 
            // default value for type T. The default value is returned if the 
            // list is empty.
            T temp = default(T);

            Node current = head;
            while (current != null)
            {
                temp = current.Data;
                current = current.Next;
            }
            return temp;
        }
    }
}

 

posted on 2016-03-02 09:24  宝山空归  阅读(164)  评论(0编辑  收藏  举报