Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型节点声明_C#_Generics_Nested Generics - Fatal编程技术网

C# 泛型节点声明

C# 泛型节点声明,c#,generics,nested-generics,C#,Generics,Nested Generics,微软将此作为学习泛型的冒泡排序示例。在我到达第76行和第77行之前,这是有意义的。这些声明如何可能?节点是一个类。你不需要用new来实例化它吗? 您将如何优化排序?哪个零件是通用零件,哪个零件是非通用零件 1 public class GenericList<T> : System.Collections.Generic.IEnumerable<T> 2 { 3 protected Node head; 4 prot

微软将此作为学习泛型的冒泡排序示例。在我到达第76行和第77行之前,这是有意义的。这些声明如何可能?节点是一个类。你不需要用new来实例化它吗? 您将如何优化排序?哪个零件是通用零件,哪个零件是非通用零件

1   public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
2       {
3           protected Node head;
4           protected Node current = null;
5   
6           // Nested class is also generic on T
7           protected class Node
8           {
9               public Node next;
10              private T data;  //T as private member datatype
11   
12              public Node(T t)  //T used in non-generic constructor
13              {
14                  next = null;
15                  data = t;
16              }
17  
18              public Node Next
19              {
20                  get { return next; }
21                  set { next = value; }
22              }
23  
24              public T Data  //T as return type of property
25              {
26                  get { return data; }
27                  set { data = value; }
28              }
29          }
30  
31          public GenericList()  //constructor
32          {
33              head = null;
34          }
35  
36          public void AddHead(T t)  //T as method parameter type
37          {
38              Node n = new Node(t);
39              n.Next = head;
40              head = n;
41          }
42  
43          // Implementation of the iterator
44          public System.Collections.Generic.IEnumerator<T> GetEnumerator()
45          {
46              Node current = head;
47              while (current != null)
48              {
49                  yield return current.Data;
50                  current = current.Next;
51                  
52              }
53          }
54  
55          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
56          {
57              return GetEnumerator();
58          }
59      }
60  
61      public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
62      {
63          // A simple, unoptimized sort algorithm that 
64          // orders list elements from lowest to highest:
65  
66          public void BubbleSort()
67          {
68              if (null == head || null == head.Next)
69              {
70                  return;
71              }
72              bool swapped;
73  
74              do
75              {
76                  Node previous = null;
77                  Node current = head;
78  
79                  
80                  //Console.WriteLine(previous.GetType());
81                  //Console.ReadLine();
82  
83                  swapped = false;
84                  
85                   
86                  //Debug.WriteLine(p);
87                  //Debug.WriteLine("Testing " + current.ToString());
88  
89                  while (current.next != null)
90                  {
91                      //  Because we need to call this method, the SortedList
92                      //  class is constrained on IEnumerable<T>
93                      if (current.Data.CompareTo(current.next.Data) > 0)
94                      {
95                          Node tmp = current.next;
96                          current.next = current.next.next;
97                          tmp.next = current;
98  
99                          if (previous == null)
100                         {
101                             head = tmp;
102                         }
103                         else
104                         {
105                             previous.next = tmp;
106                         }
107                         previous = tmp;
108                         swapped = true;
109                     }
110                     else
111                     {
112                         previous = current;
113                         current = current.next;
114                     }
115                 }
116             } while (swapped);
117         }
118     }
1公共类GenericList:System.Collections.Generic.IEnumerable
2       {
3受保护的节点头;
4受保护节点电流=零;
5.
6//嵌套类在T上也是泛型的
7受保护类节点
8           {
9下一个公共节点;
10私有T数据;//T作为私有成员数据类型
11
12公共节点(T)//T在非泛型构造函数中使用
13              {
14 next=null;
15数据=t;
16              }
17
18下一个公共节点
19              {
20获取{返回下一步;}
21设置{next=value;}
22              }
23
24公共T数据//T作为属性的返回类型
25              {
26获取{返回数据;}
27设置{data=value;}
28              }
29          }
30
31 public GenericList()//构造函数
32          {
33头=空;
34          }
35
36 public void AddHead(T)//T作为方法参数类型
37          {
38节点n=新节点(t);
39 n.下一个=头部;
40头=n;
41          }
42
43//迭代器的实现
44 public System.Collections.Generic.IEnumerator GetEnumerator()
45          {
46节点电流=头;
47 while(当前!=null)
48              {
49收益回报率数据;
50电流=电流。下一步;
51
52              }
53          }
54
55 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
56          {
57返回GetEnumerator();
58          }
59      }
60
61公共类分类列表:通用列表,其中T:System.i可比较
62      {
63//一种简单、未优化的排序算法
64//顺序从最低到最高列出元素:
65
66公共空间泡泡港()
67          {
68如果(null==head | | null==head.Next)
69              {
70人返回;
71              }
72个布尔交换;
73
74做
75              {
76节点前一个=空;
77节点电流=磁头;
78
79
80//Console.WriteLine(previous.GetType());
81//Console.ReadLine();
82
83交换=假;
84
85
86//Debug.WriteLine(p);
87//Debug.WriteLine(“测试”+current.ToString());
88
89 while(current.next!=null)
90                  {
91//因为我们需要调用这个方法,SortedList
92//类被约束在IEnumerable上
93如果(current.Data.CompareTo(current.next.Data)>0)
94                      {
95节点tmp=current.next;
96 current.next=current.next.next;
97 tmp.next=电流;
98
99如果(上一个==null)
100                         {
101头=tmp;
102                         }
103其他
104                         {
105上一个。下一个=tmp;
106                         }
107先前=tmp;
108交换=真;
109                     }
110其他
111                     {
112先前=当前;
113当前=当前。下一步;
114                     }
115                 }
116}时(交换);
117         }
118     }

您分别将
上一个
下一个
分配给
null
。这是一个赋值操作,就像其他操作一样。除非实际创建对象的新实例,否则不需要使用new


通用部件是指
T
的任何部件。它是类实例化时提供的泛型类型。一个很好的例子是
List
,它创建了
T
类型的对象列表。您可以将其作为字符串列表的
List
,整数列表的
List
,等等。

C中的
类型可以使用
null
或与声明类型兼容的值初始化。第76行和第77行看起来是这样的

Node previous = null;
Node current = head;

这里的值
null
是合法的。它本质上说“我没有价值”。对
头部
的分配也是合法的,因为
头部
也是
节点
类型。结果是两个引用
head
current
引用相同的
节点
对象值。通过一个引用修改
节点
实例将对另一个可见

这些是引用,可以指向类的实例,也可以是
null

您可以将引用指定给其他引用:

Foo f1 = new Foo();
Foo f2 = f1;
Foo f3 = null;

在该代码的第76行和第77行没有实例化任何对象。正如您所说,
节点
是一个类。.NET中的类始终是引用类型,这意味着:

  • 引用类型变量的值是对对象的引用(或不引用)
  • 引用类型的变量可以指定值
    null
    ,这意味着变量不引用任何内容
  • 分配一个引用类型的变量