---
title: "C#中类和结构的一个区别..."
author: "lysuzz"
date: "2009-02-11T23:03:00Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2010-12-18T20:55:27Z"
id: 845481
url: "https://lysu.iteye.com/blog/845481"
---

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

```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 

    } 
} 
```



```C#
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; 
        } 


    } 
} 
```


而调试代码是：... 



```C#
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...终于搞懂了... 


哈哈 说了这么多 我今晚被这个也搞了好长时间。。。所以在这说下，其实就是值类型和引用的区别但是用的时候就忘了..留在这提醒下自己，也希望对大家有用...第一篇博客哈！