Get the first and last item of an array of strings

后端 未结 5 1073
既然无缘
既然无缘 2021-02-05 06:35

If I have the following array of strings:

string[] stringArray = {\"one\", \"two\", \"three\", \"four\"};

Is there a way to get the first and l

5条回答
  •  醉梦人生
    2021-02-05 07:10

    First

        Console.WriteLine(stringArray.First());
        Console.WriteLine(stringArray.ElementAt(0));
        Console.WriteLine(stringArray[0]);
        var stringEnum = stringArray.GetEnumerator();
        if (stringEnum.MoveNext())
            Console.WriteLine(stringEnum.Current);
    

    Last

        Console.WriteLine(stringArray.Last());
        if (stringArray.Any())
            Console.WriteLine(stringArray.ElementAt(stringArray.Count()-1));
        Console.WriteLine(stringArray[stringArray.Length -1]);
        var stringEnum = stringArray.GetEnumerator();
        string lastValue = null;
        while (stringEnum.MoveNext())
            lastValue = (string)stringEnum.Current;
        Console.WriteLine(lastValue);
    

提交回复
热议问题