C#yield 和结构比较 IEquatable IEqualityComparer

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

namespace HelloCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            #region
            //var game = new GameMoves();
            //IEnumerator enumerator = game.Cross();
            //while (enumerator.MoveNext())
            //{
            //    enumerator = enumerator.Current as IEnumerator;
            //}
            #endregion

            #region 结构比较
            var janet = new Person { FirstName = "Janet", LastName = "Jackson" };
            Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet };
            Person[] persons2 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet };
            if (persons1 != persons2)
            {
                Console.WriteLine("不相等");
            }
            if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer<Person>.Default))
            {
                Console.WriteLine("相等");
            }
            #endregion


            var t1 = Tuple.Create<int, string>(1, "Stephanie");
            var t2 = Tuple.Create<int, string>(1, "Stephanie");
            if (t1 != t2)
            {
                Console.WriteLine("不相等");
            }
            if (t1.Equals(t2))
            {
                Console.WriteLine("相等");
            }


            //if (t1.Equals(t2, new TupleComparer()))
            //{
            //    Console.WriteLine("相等");
            //}
            Console.ReadKey();
        }
    }
    public class HelloCollection
    {
        public IEnumerator<string> GetEnumerator()
        {
            yield return "Hello";
            yield return "World";
        }
    }

    public class GameMoves
    {
        private IEnumerator cross;
        private IEnumerator circle;
        public GameMoves()
        {
            cross = Cross();
            circle = Circle();
        }
        private int move = 0;
        const int MaxMoves = 9;
        public IEnumerator Cross()
        {
            while (true)
            {
                Console.WriteLine("Cross,move {0}", move);
                if (++move >= MaxMoves)
                {
                    yield break;
                }
                yield return circle;
            }
        }

        public IEnumerator Circle()
        {
            while (true)
            {
                Console.WriteLine("Circle, move {0}", move);
                if (++move >= MaxMoves)
                {
                    yield break;
                }
                yield return cross;
            }
        }
    }


    public class Person : IEquatable<Person>
    {
        public int Id { get; private set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public override string ToString()
        {
            return string.Format("{0},{1} {2}", Id, FirstName, LastName);
        }
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            return Equals(obj as Person);
        }
        public bool Equals(Person other)
        {
            if (other == null) throw new ArgumentNullException("other");
            return this.Id == other.Id && this.FirstName == other.FirstName && this.LastName == other.LastName;

        }
    }

    public class TupleComparer : IEqualityComparer
    {

        public new bool Equals(object x, object y)
        {
            return x.Equals(y);
        }

        public int GetHashCode(object obj)
        {
            return obj.GetHashCode();
        }
    }
}

 

posted on 2013-03-17 21:46  R.Ray  阅读(217)  评论(0)    收藏  举报

导航