コード例 #1
0
        public static void Main(string[] args)
        {
            TwoDPointWithHash[] arr = new TwoDPointWithHash[1000];

            int minValue = -500;
            int maxValue = 500;

            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = minValue; j < maxValue; j++)
                {
                    arr[i] = new TwoDPointWithHash(j, j);
                }
            }

            int[] arrayHashCode = new int[arr.Length];

            for (int i = 0; i < arr.Length; i++)
            {
                arrayHashCode[i] = arr[i].GetHashCode();
            }

            double procent;

            procent = 1 - (arrayHashCode.Distinct().Count() / arrayHashCode.Length) * 100;

            Console.WriteLine($"{procent}%");
        }
コード例 #2
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);
            }
        }