C#中类和结构的一个区别...

本文记录了作者在使用C#实现二叉树迭代遍历过程中遇到的问题及解决过程,详细分析了值类型和引用类型的区别,并给出了正确的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在努力学习C#语法...今晚在左一个二叉树的迭代遍历时发生了点错误...

代码如下:


using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
class BinaryTreeT> : IEnumerableT> where T : IComparable
{

public BinaryTree(T value)
{
Value = value;
}

private T _Value;

public T Value
{
get { return _Value; }
set { _Value = value; }
}

private PairBinaryTreeT>> _SubItems;

public PairBinaryTreeT>> SubItems
{
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
{
BinaryTreeT> currentNode = this;
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 Members

public IEnumeratorT> GetEnumerator()
{
yield return Value;
foreach (BinaryTreeT> tree in SubItems)
{
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

}
}


using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
class PairT> : IPairT>,
IEnumerableT>
{
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 Members

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 Members

public IEnumeratorT> GetEnumerator()
{
yield return First;
yield return Second;
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion


public System.Collections.Generic.IEnumerableT>
GetNotNullEnumerator()
{
if (First == null || Second == null)
yield break;
yield return Second;
yield return First;
}

public System.Collections.Generic.IEnumerableT>
GetReverseEnumerator()
{
yield return Second;
yield return First;
}


}
}


而调试代码是:...


using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
class Program
{
static void Main(string[] args)
{

BinaryTreestring> jfkFamilyTree = new BinaryTreestring>("John Fizgerald Kennedy");

jfkFamilyTree.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("Joseph Patrick Kennedy"),
new BinaryTreestring>("Rose Elizabeth Fizgrald"));

jfkFamilyTree.SubItems.First.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("Patrick Joseph Kennedy"),
new BinaryTreestring>("Mary Augusta Hickey"));

jfkFamilyTree.SubItems.Second.SubItems = new PairBinaryTreestring>>(
new BinaryTreestring>("John Francis Fizgerald"),
new BinaryTreestring>("Mary Hoseph Hannom"));

foreach (string name in jfkFamilyTree)
{
Console.WriteLine(name);
}


}

}


}

结果发生问题说 迭代到树尖时SubItems为null....后来在迭代中加了个判断是否为null就貌似解决了....但不对啊???为什么书没有啊?难道输错了...


最后 终于找到问题是录入错误把Pair弄成了class...而BinaryTree中持有一个Pair类型的SubItems的“引用”...不是值类型..这样会直接初始化为null所以错了


那怎么让他初始化为一个默认的Pair 加个构造函数可以 但书上是把class改成Struct后就是持有一个值类型了...这样初始化会调用一个结构的默认值(感觉像默认构造函数...),就是一个Pair...每个成员都是null...终于搞懂了...


哈哈 说了这么多 我今晚被这个也搞了好长时间。。。所以在这说下,其实就是值类型和引用的区别但是用的时候就忘了..留在这提醒下自己,也希望对大家有用...第一篇博客哈!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值