예제 #1
0
        private static void dynamicArrayExample()
        {
            //requirements: type, size, values, iteration...
            Console.WriteLine("What Type of Array U want to create\nPS: Enter the CTS typename");
            string input    = Console.ReadLine();  //ReadLine is the only method to take inputs from the console.
            Type   dataType = Type.GetType(input); //Similar to ClassName for...
            int    size     = MyConsole.GetNumber("Enter the size of the array");
            Array  array    = Array.CreateInstance(dataType, size);

            for (int i = 0; i < size; i++)
            {
                Console.WriteLine("Enter the value for the location {0} of the data type {1}", i, dataType.Name);
                input = Console.ReadLine();
                object inputValue = Convert.ChangeType(input, dataType);
                array.SetValue(inputValue, i);
            }
            Console.WriteLine("All the values are set\nReading the values");
            foreach (object value in array)
            {
                Console.WriteLine(value);
            }
        }
예제 #2
0
        private static void twoDArrayExample()
        {
            //Length vs. GetLength vs. Rank of the Array class.
            //GetLength method is used to get the length of the specified dimension in a multi dimensional array. dimension index starts with 0...
            int[,] rooms = new int[3, 2];
            Console.WriteLine("The no of dimensions:" + rooms.Rank);
            for (int i = 0; i < rooms.GetLength(0); i++)
            {
                for (int j = 0; j < rooms.GetLength(1); j++)
                {
                    rooms[i, j] = MyConsole.GetNumber($"Enter the value for {i},{j} location");
                }
            }

            for (int i = 0; i < rooms.GetLength(0); i++)
            {
                for (int j = 0; j < rooms.GetLength(1); j++)
                {
                    Console.Write(rooms[i, j] + "\t");//Difference b/w Write vs WriteLine
                }
                Console.WriteLine();
            }
        }