前提・実現したいこと
MicroSoft Docsのジェネリックの概要
https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/generics/introduction-to-generics
に記載されているサンプルコードを理解したい
発生している問題・エラーメッセージ
該当ページのサンプルコードのNodeクラスの下にある
private Node head;
が何を指し示しているのかがわかりません。
変数宣言だと思うのですが、なぜ入れ子のNodeが出てくるのでしょうか?
C#は初めて1年未満で、ここ最近ジェネリックを勉強し始めたレベルです。
該当のソースコード
public class GenericList<T> { private class Node { public Node(T t) { next = null; data = t; } private Node next; public Node Next { get { return next; } set { next = value; } } private T data; public T Data { get { return data; } set { data = value; } } } private Node head; public GenericList() { head = null; } public void AddHead(T t) { Node n = new Node(t); n.Next = head; head = n; } public IEnumerator<T> GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } } }
試したこと
GenericListのコンストラクタで
head=1と書き換えたところ、先程のprivate Node head;
がprivate int head; になることは確認しました。
1=intでそこが書き換わるのはわかるのですが、nullだとNodeで宣言できるのはどうしてだろう?と悩んでいます。
補足情報(FW/ツールのバージョンなど)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/07 07:22
2019/02/07 07:29
2019/02/07 07:36