900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > c# 2维数组 取一维_C#| 不同类型的一维数组声明

c# 2维数组 取一维_C#| 不同类型的一维数组声明

时间:2020-07-08 20:49:56

相关推荐

c# 2维数组 取一维_C#| 不同类型的一维数组声明

c# 2维数组 取一维

In the below example, we aredeclaring an integer array (one dimensional)with following styles:

在下面的示例中,我们声明具有以下样式的整数数组(一维):

1) One dimensional Array declaration with initialization (without array size)

1)具有初始化的一维数组声明(无数组大小)

Example:

例:

int[] arr1 = { 12, 45, 56, 78, 89 };

In this style, arr1 will automatic occupy the size of the array based on given elements.

在这种样式中, arr1将根据给定的元素自动占据数组的大小。

2) Dynamic One dimensional Array declaration with fixed size

2)具有固定大小的动态一维数组声明

Example:

例:

int[] arr2 = new int[5];

In this style, arr2 will occupy the size for 5 integers dynamically.

在这种样式中, arr2将动态占用5个整数的大小。

3) Dynamic One dimensional Array declaration with variable size

3)具有可变大小的动态一维数组声明

Example:

例:

int[] arr3 = new int[size];

In this style, we will read the size of the arr3 first and then declare the array dynamically.

在这种样式中,我们将先读取arr3的大小,然后动态声明数组。

Example:In the below given example – we are using these 3 styles to declare an array.

示例:在下面给出的示例中–我们使用这3种样式声明一个数组。

Program:

程序:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Arrays{class Program{static void Main(string[] args){//Single DimensionConsole.WriteLine("Type 1 : Declaration");int[] arr1 = {12, 45, 56, 78, 89 };foreach(int item in arr1){Console.Write("{0}\t", item);}Console.WriteLine("\n\nType 2 : Declaration");int[] arr2 = new int[5];Console.WriteLine("Enter 5 Values:");for(int i=0;i<arr2.Length;i++){arr2[i] = Convert.ToInt32(Console.ReadLine());}foreach (int item in arr2){Console.Write("{0}\t", item);}Console.WriteLine("\n\nType 3 : Declaration");Console.Write("Enter Size:");int size = Convert.ToInt32(Console.ReadLine());int[] arr3 = new int[size];Console.WriteLine("Enter {0} Values:",size);for (int i = 0; i < arr3.Length; i++){arr3[i] = Convert.ToInt32(Console.ReadLine());}foreach (int item in arr3){Console.Write("{0}\t", item);}Console.ReadKey();}}}

Output

输出量

Type 1 : Declaration1245567889Type 2 : DeclarationEnter 5 Values:10203040501020304050Type 3 : DeclarationEnter Size:3Enter 3 Values:1000010000

翻译自: /dot-net/different-types-of-one-dimensional-array-declarations.aspx

c# 2维数组 取一维

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。