C#中類和結構的一個區別...
最近在努力學習C#語法...今晚在左一個二叉樹的迭代遍歷時發生了點錯誤...
程式碼如下:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class BinaryTree
{
public BinaryTree(T value)
{
Value = value;
}
private T _Value;
public T Value
{
get { return _Value; }
set { _Value = value; }
}
private Pair
public Pair
{
get
{
return _SubItems;
}
set
{
IComparable first;
first = value.First._Value;
if (first.CompareTo(value.Second._Value) < 0)
{
}
else
{
}
_SubItems = value;
}
}
//[System.Runtime.CompilerServices.IndexerName("Entry")]
public T this[PairItem[] branches]
{
get
{
BinaryTree
int totalLevel = (branches == null) ? 0 : branches.Length;
int currentLevel = 0;
while (currentLevel < totalLevel)
{
currentNode = currentNode.SubItems[branches[currentLevel]];
if (currentNode == null)
{
throw new IndexOutOfRangeException();
}
++currentLevel;
}
return currentNode.Value;
}
}
#region IEnumerable
public IEnumerator
{
yield return Value;
foreach (BinaryTree
{
if (tree != null)
{
foreach (T item in tree)
{
yield return item;
}
}
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class Pair
IEnumerable
{
public Pair(T first)
{
this._First = first;
this._Second = default(T);
}
public Pair(T first, T second)
{
this._First = first;
this._Second = second;
}
#region IPair
public T First
{
get
{
return _First;
}
private set
{
_First = value;
}
}
private T _First;
public T Second
{
get
{
return _Second;
}
private set
{
this._Second = value;
}
}
private T _Second;
public T this[PairItem index]
{
get
{
switch (index)
{
case PairItem.First:
return First;
case PairItem.Second:
return Second;
default:
throw new NotImplementedException(
string.Format("The enum {0} has not been implemented."));
}
}
set
{
switch (index)
{
case PairItem.First:
First = value;
break;
case PairItem.Second:
Second = value;
break;
default:
throw new NotImplementedException(
string.Format("The enum {0} has not been implemented."));
}
}
}
#endregion
#region IEnumerable
public IEnumerator
{
yield return First;
yield return Second;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
public System.Collections.Generic.IEnumerable
GetNotNullEnumerator()
{
if (First == null || Second == null)
yield break;
yield return Second;
yield return First;
}
public System.Collections.Generic.IEnumerable
GetReverseEnumerator()
{
yield return Second;
yield return First;
}
}
}
而除錯程式碼是:...
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumeratorTestForPair
{
class Program
{
static void Main(string[] args)
{
BinaryTree
jfkFamilyTree.SubItems = new Pair
new BinaryTree
new BinaryTree
jfkFamilyTree.SubItems.First.SubItems = new Pair
new BinaryTree
new BinaryTree
jfkFamilyTree.SubItems.Second.SubItems = new Pair
new BinaryTree
new BinaryTree
foreach (string name in jfkFamilyTree)
{
Console.WriteLine(name);
}
}
}
}
結果發生問題說 迭代到樹尖時SubItems為null....後來在迭代中加了個判斷是否為null就貌似解決了....但不對啊???為什麼書沒有啊?難道輸錯了...
最後 終於找到問題是錄入錯誤把Pair
那怎麼讓他初始化為一個預設的Pair 加個建構函式可以 但書上是把class改成Struct後就是持有一個值型別了...這樣初始化會呼叫一個結構的預設值(感覺像預設建構函式...),就是一個Pair...每個成員都是null...終於搞懂了...
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-548884/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C# 結構體與類的區別C#結構體
- c#中結構體和類的比較C#結構體
- C++中結構體與類的區別C++結構體
- C# 中 Struct 和 Class 的區別總結C#Struct
- 資料結構中樹和森林的區別資料結構
- c# 中的靜態類(satic class)和具體類的區別及用法C#
- C#中Hashtable和HashMap的區別C#HashMap
- C#中ref和out的區別C#
- 【轉】C#中virtual和abstract的區別C#
- C#中堆和堆疊的區別C#
- C#中誰最快:結構還是類?
- 偽元素和偽類的區別總結
- Mybatis中#{}和${}傳參的區別及#和$的區別小結MyBatis
- C# 中的 == 和 equals()有什麼區別?C#
- C#中的屬性和欄位的區別C#
- BLDC 電機和 PMSM 的結構區別
- js中==和===的區別以及總結JS
- C#中String和string區別C#
- c#中Debug和Release的區別實驗C#
- 什麼是CS和BS結構,兩種結構的區別
- C#資料結構篇(一連結串列類) (轉)C#資料結構
- Java中抽象類和介面的區別Java抽象
- python類中super()和__init__()的區別Python
- C++ protected許可權成員以及類和結構體區別C++結構體
- [C#]C#中ToString()和Convert.ToString()的區別C#
- Java中異常分類和Throw和Throws的區別Java
- Oracle中sys和system的區別小結Oracle
- OC中類的結構
- 【思考】URI和URL的區別?以及URL的結構
- 淺談C#中重寫和隱藏的區別C#
- 糾結了很久的一個問題, 內部類和靜態內部類在使用上有啥區別
- 類和結構
- 介面和抽象類的區別抽象
- C#中List與IList的區別C#
- C#中的資料結構C#資料結構
- C/S和B/S結構區別整理
- 判斷一個類是否為另一個類的例項 instanceof關鍵字和isAssignableFrom方法的區別
- 關於一個樹狀結構的通用類