ジェネリック コードの default キーワード (C# プログラミング ガイド)

ジェネリック クラスとジェネリック メソッドでは、あらかじめ以下の情報を把握していない場合に、パラメーター化された型 T に既定値を割り当てる方法が 1 つの問題となります。

  • T が参照型か値型か

  • T が値型の場合、数値か構造体か

パラメーター化された型 T の変数がある場合、ステートメント t = null は、T が参照型のときにのみ有効です。また、t = 0 は、数値では機能しますが、構造体では機能しません。 この問題を解決するには、default キーワードを使用します。このキーワードは、参照型の場合には null を返し、数値の値型にはゼロを返します。 構造体の場合、ゼロまたは null (値型か参照型かによって変わります) に初期化された構造体の各メンバーを返します。 null 許容値型の場合、default は System.Nullable<T> を返します。これは、他の構造体と同様に初期化されます。

GenericList<T> クラスで default キーワードを使用する方法の例を次に示します。 詳細については、「ジェネリックの概要 (C# プログラミング ガイド)」を参照してください。

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

参照

参照

ジェネリック (C# プログラミング ガイド)

ジェネリック メソッド (C# プログラミング ガイド)

System.Collections.Generic

概念

C# プログラミング ガイド

その他の技術情報

.NET Framework におけるジェネリック