C sharp(#) Concise method of array definition

Here is an example of a one-dimensional integer array

using System;

namespace debug
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
        	// 方法一
            int[] numbers = {
    
     2, 12, 5, 15 };
            Console.WriteLine(numbers.GetType());
            foreach(int i in numbers)
            {
    
    
                Console.WriteLine($"{i}");
            }
            // 方法二
            int[] numbers1 = new int[] {
    
     2, 12, 5, 15 };
            Console.WriteLine(numbers1.GetType());
            foreach (int i in numbers)
            {
    
    
                Console.WriteLine($"{i}");
            }
        }        
    }
}
//Result:
//System.Int32[]
//2
//12
//5
//15
//System.Int32[]
//2
//12
//5
//15

We can see that when defining the array, we can omit the second half new int[]and directly enter the value of the array we want to define.

If you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/111187462