如何在C#中声明一个数组?

use*_*307 0 c# arrays collections list

如何在不设置初始元素集的情况下声明数组?或者是否必须输入初始元素集?这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        string ans, uname, choice, input;
        int temp;

        Console.WriteLine("Hi! We're Adrianne and Marco, today is " + DateTime.Now + ", what's yours?");
        uname = Console.ReadLine();

        do
        {
            Console.WriteLine("Hello, " + uname + "! Please select a function:");
            Console.WriteLine("1: Palindrome");
            Console.WriteLine("2: Prime or Not Prime");
            Console.WriteLine("3: Bubble Sort");
            Console.WriteLine("4: Fibonacci");
            choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.WriteLine("Enter any word or string:");
                input = Console.ReadLine();
                temp = Palindrome(input);

                if (temp == 0)
                    Console.WriteLine(input + " is not a palindrome...");
                else
                    Console.WriteLine(input + " is a palindrome!");
            }

            else if (choice == "2")
            {
                Console.WriteLine("Enter a number:");
                input = Console.ReadLine();
                temp = Prime(input);

                if (temp == 0)
                    Console.WriteLine(input + " is prime");
                else if (temp == 1)
                    Console.WriteLine(input + " is not prime");
                else
                    Console.WriteLine(input + " is neither prime nor composite");
            }

            else if (choice == "3")
            {
                int h;
                string tempo;
                double[] inputs = { 0, 0, 0, 0, 0, 0, 0, 0 };

                for (int i = 0; i < 8; i++)
                {
                    h = i + 1;
                    if (i == 0)
                    {
                        Console.WriteLine("Enter 1st number:");
                        tempo = Console.ReadLine();
                    }

                    else if (i == 1)
                    {
                        Console.WriteLine("Enter 2nd number:");
                        tempo = Console.ReadLine();
                    }

                    else if (i == 2)
                    {
                        Console.WriteLine("Enter 3rd number:");
                        tempo = Console.ReadLine();
                    }

                    else
                    {
                        Console.WriteLine("Enter " + h + "th number:");
                        tempo = Console.ReadLine();
                    }

                    inputs[i] = Convert.ToDouble(tempo);
                }

                bubbleSort(inputs);
                Console.WriteLine(inputs[0] + " " + inputs[1] + " " + inputs[2] + " " + inputs[3] + " " + inputs[4] + " " + inputs[5] + " " + inputs[6] + " " + inputs[7]);
            }

            else if (choice == "4")
            {
                Console.WriteLine("Enter a whole number:");
                input = Console.ReadLine();

                int temp3 = Convert.ToInt16(input);

                int[] fibNums = {0, 0, 0, 0, 0};

                //for (int i = 0; i < fibNums.Length; i++)
                //{
                //    Console.WriteLine(fibNums[i]);
                //}
                int temp4 = 0;
                do
                {
                    temp = fibSequence(temp4);
                    fibNums[temp4] = temp;
                    temp4++;
                } while (temp <= temp3);

                for (int i = 0; i < fibNums.Length; i++)
                {
                    Console.WriteLine(fibNums[i]);
                }
            }

            Console.WriteLine("There, all finished! Try again? (Y/N)");
            ans = Console.ReadLine();

        } while (ans == "Y");

        Console.WriteLine("Thank you, come again!");
        Console.Read();

    }
Run Code Online (Sandbox Code Playgroud)

错误在这部分"int [] fibNums = {0,0,0,0,0};".在我的代码中,数组实际上可以包含无限数量的元素,具体取决于输入.但是因为,我不知道如何声明一个数组,元素的数量仅限于我初始化它的元素数量.

Den*_*aub 6

我认为一个List<int>更适合你的目的.它会根据需要自动扩展.

List<int> fibNums = new List<int> {0, 0, 0, 0, 0};
Run Code Online (Sandbox Code Playgroud)