0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#.NETで配列の一番最後の要素を取得する時はLinqのLast()を使うと便利

Last updated at Posted at 2024-02-07

やり方

サンプルコード
using System;
using System.Linq;

namespace Sampleple
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] hai_ = new string[3] {"たきな", "ちさと", "その他"};

            //System.Linqを使うと『要素数を取得して…』みたいなことせずにLast()だけで取得できるので便利。
            Console.Write(hai_.Last());
            
            Console.ReadLine();
        }
    }
}

実行結果。
image.png

.NET 8.0以上ならインデックス演算子

下記は.NET Framework4.8での画面です。
image.png

.NET 8.0以上ならもっと便利な『インデックス演算子』というものが使えるようです。

蛇足

やり方がすぐ思いつくロジックであっても『改めてググってみる』とか『ChatGPT君に聞いてみる』とかやると良いです。
自分が知らなかった『もっと便利なやり方』が見つかるかもです。

参考サイトさん

バージョン

Microsoft .NET Framework Version 4.8.09037

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?