简体   繁体   English

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

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

I'm trying to translate 我正在尝试翻译

"A method that returns true if the value 3 is contained in the array and false otherwise." “如果数组中包含值3,则返回true,否则返回false。

into code, but so far the only thing I could come up with is 到代码中,但到目前为止,我唯一能想到的是

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

Not really sure where do go from here, any feedback would be much appreciated. 不太确定从何而来,我们将不胜感激。

An C#-Array implements the IEnumerable<TSource> interface. C#数组实现IEnumerable<TSource>接口。 One of the defined methods on this interface is the Contains(TSource element) method which checks if the element is in the collection of elements. 这个接口上定义的方法之一是Contains(TSource element)方法,该方法检查该元素是否在元素集合中。

So in your case it should be: 因此,在您的情况下应该是:

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

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

相关问题 c#List.Exists返回true并带有“ false &amp;&amp; true” - c# List.Exists returns true with “false && true” 如果regularexpressionvalidator在C#中返回错误的启动方法 - If regularexpressionvalidator returns false launch method in c# C# 检查正则表达式是否匹配,返回 True 而不是 False - C# Check if Regex is a match, returns True instead of False C#:如何检查布尔变量数组中的所有元素是否具有相同的值(全部为true或全部为false)? - C#: How to check if all the elements in an array of boolean variables are of the same value (All true or All false)? C# 解析“(真和真)或(真或假)” - C# resolve “(true and true) or (true or false)” C#中是否有模式或方法来检查(int 1,2,4,8,...)选项是真还是假 - Is there a pattern or a method in C# to check if an (int 1,2,4,8,…) option is true or false 为什么方法不间断返回true却不间断返回false? C# - Why is method returning true without break but false with break? C# 在 C# 中随机填充一个带有 true 和 false 布尔值的数组(和其他问题) - Filling an array with true and false booleans randomly in c# (and other questions) 如何在C#中选择一个值为true或false? - how to select one value if true or false in C#? C#中的真错误测验 - true false quiz in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM