简体   繁体   中英

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."

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. 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.

So in your case it should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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