c#固定数组大小_在C#中声明一个固定大小的数组

c#固定数组大小

In C#, an array can be declared with a fixed size length and a variable length. Sometimes it's required that we need to have an array of fixed length. In this tutorial, we will learn how to declare an array of fixed size length, how to declare an array with a variable length i.e. input array length at run time, how to print array length (using Array.Length property), how to input array elements at run time and how to print array elements?

在C#中,可以声明具有固定大小长度和可变长度的数组。 有时,我们需要具有固定长度的数组。 在本教程中,我们将学习如何声明一个固定长度的数组如何声明一个可变长度的数组,即在运行时输入数组的长度, 如何打印数组的长度 (使用Array.Length属性 ),如何输入数组元素在运行时如何打印数组元素?

Syntax to declare a fixed size array:

声明固定大小数组的语法:

    <data_type>[] array_name = new <data_type>[size];

Example:

例:

Here is the declaration statement for an integer type 10 elements array,

这是整数类型10元素数组的声明语句,

    int[] arr = new int[10];

When an integer array is declared it initialized with default values (0), if we print array elements, it will print zeros.

声明整数数组时,它将使用默认值(0)进行初始化,如果我们打印数组元素,它将打印零。

1)C#示例声明一个固定大小的数组,输入和打印数组元素 (1) C# example to declare a fixed size array, input and print array elements)

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring a 10 elements integer array
            int[] arr = new int[10];

            //printing size
            Console.WriteLine("Array size is: " + arr.Length);

            //printing array 
            Console.WriteLine("Array elements...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();            

            //input and assigning elements to the array
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("Enter " + i + " element: ");
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            //printing array 
            Console.WriteLine("Array elements...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Array size is: 10
Array elements...
0 0 0 0 0 0 0 0 0 0
Enter 0 element: 10
Enter 1 element: 20
Enter 2 element: 30
Enter 3 element: 40
Enter 4 element: 50
Enter 5 element: 60
Enter 6 element: 60
Enter 7 element: 80
Enter 8 element: 90
Enter 9 element: 55
Array elements...
10 20 30 40 50 60 60 80 90 55

Input array size at run time and declare an array

在运行时输入数组大小并声明一个数组

Here, we will input array length and will declare an array of variable length (according to input value).

在这里,我们将输入数组长度,并声明一个可变长度的数组(根据输入值)。

Syntax:

句法:

    int[] arr = new int[len];

Here, len is a variable, that will be read from the user.

在这里, len是一个变量,将从用户那里读取。

2)C#示例输入长度并在运行时声明一个数组,输入并打印数组元素 (2) C# example to Input length and declare an array at run time, input and print array elements)

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
			//variable to store length
			int len = 0;
			
			//input array length
            Console.Write("Enter array length: ");
            len = Convert.ToInt32(Console.ReadLine());

            //declaring array of len size at run time
            int[] arr = new int[len];

            //printing size
            Console.WriteLine("Array size is: " + arr.Length);

            //printing array 
            Console.WriteLine("Array elements...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();            

            //input and assigning elements to the array
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("Enter " + i + " element: ");
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            //printing array 
            Console.WriteLine("Array elements...");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Enter array length: 5
Array size is: 5
Array elements...
0 0 0 0 0
Enter 0 element: 100
Enter 1 element: 200
Enter 2 element: 300
Enter 3 element: 400
Enter 4 element: 500
Array elements...
100 200 300 400 500


翻译自: https://www.includehelp.com/dot-net/declaring-a-fixed-size-array-in-c-sharp.aspx

c#固定数组大小

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值