C#中類和結構的一個區別...

iDotNetSpace發表於2009-02-12

最近在努力學習C#語法...今晚在左一個二叉樹的迭代遍歷時發生了點錯誤...

程式碼如下:

 

 

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

namespace EnumeratorTestForPair
{
    class BinaryTree : IEnumerable where T : IComparable
    {

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

        private T _Value;

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

        private Pair> _SubItems;

        public Pair> 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
            {
                BinaryTree 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 IEnumerator GetEnumerator()
        {
            yield return Value;
            foreach (BinaryTree 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

    }
}

 

 

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

namespace EnumeratorTestForPair
{
    class Pair : IPair,
        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 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 IEnumerator 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.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 = new BinaryTree("John Fizgerald Kennedy");

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

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

            jfkFamilyTree.SubItems.Second.SubItems = new Pair>(
                new BinaryTree("John Francis Fizgerald"),
                new BinaryTree("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...終於搞懂了...

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-548884/,如需轉載,請註明出處,否則將追究法律責任。

相關文章