欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#中的数组

程序员文章站 2022-07-16 19:47:29
...

数组:
//数组
//给数组添加10个元素,分别是1-10;

        int[] c = new int[10];
        for(int d = 0; d < 10; d++)
        {
            c[d] = d + 1;
        }
        for(int e = 0; e < c.Length; e++)
        {
            Console.WriteLine(c[e]);
        }

第二种办法
int[] n = new int[10];
for (int d = 0; d < 10; d++)
{
n[d] = d + 1;
}

        foreach (int m in n)
        {
            Console.WriteLine(m);
        }

C#中的数组
//判断数组当中有没有5这个元素
//如果有,那么求他的索引

bool a = false;
int e = 0;
int[] b = new int[5] { 1, 2, 3, 4, 5 };
for (int c = 0; c < b.Length; c++)
{
if (b[c] == 5)
{
a = true;
e = c;
break;
}
}
Console.WriteLine(a);
Console.WriteLine(e);
}
方法二
bool a = false;
int e = 0;
int[] b = new int[5] { 1, 2, 3, 4, 5 };
foreach(int n in b)
{
if (n == 5)
{
a = true;
break;
}
e++;
}
Console.WriteLine(a);
Console.WriteLine(e);
C#中的数组