コード例 #1
0
ファイル: TwoDPoint.cs プロジェクト: Borowik1/Dot_net_lessons
        public bool Equals(TwoDPoint p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((x == p.x) && (y == p.y));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Borowik1/Dot_net_lessons
        public static void Main(string[] args)
        {
            List <TwoDPoint> points = new List <TwoDPoint>(100000);

            int[]  hashOfPoints = new int[points.Capacity];
            Random rn           = new Random();

            for (int i = 0; i < points.Capacity; i++)
            {
                TwoDPoint point = new TwoDPoint(0, 0);

                do
                {
                    point.x = rn.Next(-100000, 100000);
                    point.y = rn.Next(-100000, 100000);
                }while (points.Contains(point));
                points.Add(point);
                hashOfPoints[i] = point.GetHashCode();
            }

            for (int i = 0; i < hashOfPoints.Length - 1; i++)
            {
                for (int j = i + 1; j < hashOfPoints.Length; j++)
                {
                    if (hashOfPoints[i] == hashOfPoints[j])
                    {
                        for (int k = j + 1; k < hashOfPoints.Length; k++)
                        {
                            hashOfPoints[k - 1] = hashOfPoints[k];
                        }
                        Array.Resize(ref hashOfPoints, hashOfPoints.Length - 1);
                        j--;
                    }
                }
            }

            TwoDPoint point1 = new TwoDPoint(10, 10);
            TwoDPoint point2 = new TwoDPoint(10, 10);

            if (point1.GetHashCode() == point2.GetHashCode())
            {
                Console.WriteLine("У одинаковых точек хеш совпадает");
            }
            else
            {
                Console.WriteLine("!!!У одинаковых точек хеш не совпадает");
            }

            double accuracy = 100 * Convert.ToDouble(hashOfPoints.Length) / Convert.ToDouble(points.Capacity);

            Console.WriteLine($"Уникальность хеш-функции {accuracy}%");
        }
コード例 #3
0
        public static void Demo(string[] args)
        {
            TwoDPoint point1 = new TwoDPoint(1, 10);
            TwoDPoint point2 = new TwoDPoint(1, 10);

            object object1 = (object)point1;
            object object2 = (object)point2;

            Console.WriteLine(point1.Equals(point2));
            Console.WriteLine(point1 == point2);

            Console.WriteLine(object1.Equals(object2));
            Console.WriteLine(object1 == object2);             // false !!!! почему?

            // в чем разница определений методов Equals и == ?
        }
コード例 #4
0
        public override bool Equals(Object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            TwoDPoint p = obj as TwoDPoint;

            if ((Object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((x == p.x) && (y == p.y));
        }
コード例 #5
0
ファイル: TwoDPoint.cs プロジェクト: Borowik1/Dot_net_lessons
        public override bool Equals(Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            TwoDPoint p = obj as TwoDPoint;

            if ((Object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((x == p.x) && (y == p.y));
        }
コード例 #6
0
ファイル: 06_HashCode.cs プロジェクト: AngelikaUshakova/epam
        public static void Demo(string[] args)
        {
            TwoDPoint point1 = new TwoDPoint(1, 10);
            TwoDPoint point2 = new TwoDPoint(1, 10);

            Console.WriteLine("Hash for point1: {0}\tHash for point2: {1}", point1.GetHashCode(), point2.GetHashCode());

            TwoDPointWithHash newPoint1 = new TwoDPointWithHash(1, 10);
            TwoDPointWithHash newPoint2 = new TwoDPointWithHash(1, 10);

            Console.WriteLine("Hash for point1: {0}\tHash for point2: {1}", newPoint1.GetHashCode(), newPoint2.GetHashCode());

            // уникальных точек будет 2, хотя координаты их одинаковы
            Console.WriteLine("TwoDPointWithHash:");

            var twoDPointList = new List <TwoDPoint> {
                point1, point2
            };
            var distinctTwoDPointList = twoDPointList.Distinct();

            foreach (var point in distinctTwoDPointList)
            {
                Console.WriteLine("Distinct point: {0}", point);
            }

            // одна уникальная точка
            Console.WriteLine("TwoDPointWithHash:");

            var twoDPointWithHashList = new List <TwoDPointWithHash> {
                newPoint1, newPoint2
            };
            var distinctTwoDPointWithHashList = twoDPointWithHashList.Distinct();

            foreach (var point in distinctTwoDPointWithHashList)
            {
                Console.WriteLine("Distinct point: {0}", point);
            }
        }