繁体   English   中英

C#:如果数组中包含值3,则返回true,否则返回false

[英]C#: A method that returns true if the value 3 is contained in the array and false otherwise

我正在尝试翻译

“如果数组中包含值3,则返回true,否则返回false。

到代码中,但到目前为止,我唯一能想到的是

class Test
{
    static void Main(string[] args)
    {
        ValueThree();
    }
    static bool ValueThree()
    {
        int[] arr = { 1, 2, 3 };
        if (Array = 3)
    }
    return true;
}

不太确定从何而来,我们将不胜感激。

C#数组实现IEnumerable<TSource>接口。 这个接口上定义的方法之一是Contains(TSource element)方法,该方法检查该元素是否在元素集合中。

因此,在您的情况下应该是:

static bool ValueThree()
{
    int[] arr = { 1, 2, 3 };
    return arr.Contains(3);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM