C# - 如何计算数组中的元素数量? [英] C# - how can I count the number of elements in an array?

查看:121
本文介绍了C# - 如何计算数组中的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如`

DynamicArray d = new DynamicArray(4,5,6,1,2);

当我将调用GetLength函数时,它将打印5 。

Console.WriteLine(d.GetLength());

它将打印元素数量为5



我尝试过:



我写了这个函数`



public int GetLength(params int [] x)

{

//我不知道写什么。

}

for example`
DynamicArray d = new DynamicArray(4, 5, 6, 1, 2);
when i will call GetLength function, it will print 5.
Console.WriteLine(d.GetLength());
It will print "Number of elements are 5"

What I have tried:

I wrote this function`

public int GetLength(params int[] x)
{
// I don't know what write here.
}

推荐答案

尝试:

Try:
public int GetLength(params int[] x)
    {
    return x.Length;
    }


我不知道 DynamicArray 是什么,但要完成代码您拥有的片段(获取整数数组的长度),您只需返回 x.Length

I don't know what DynamicArray is, but to complete the code snippet you have (getting the length of an integer array), you only have to return x.Length:
public int GetLength(params int[] x)
{
    return x.Length;
}


这篇关于C# - 如何计算数组中的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆