.Net(VB C#) LINQ
Enumerable.ElementAt メソッド、ElementAtOrDefaultメソッド

Enumerable.ElementAtメソッド
【C#】
シーケンス内の指定されたインデックス位置にある要素を返します。
public static TSource ElementAt<TSource>
    (this IEnumerable<TSource> source, int index)
【VB】
シーケンス内の指定されたインデックス位置にある要素を返します。
<ExtensionAttribute>
Public Shared Function ElementAt(Of TSource) 
    (source As IEnumerable(Of TSource), index As Integer) 
    As TSource

Enumerable.ElementAtOrDefaultメソッド
【C#】
シーケンス内の指定されたインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。
public static TSource ElementAtOrDefault<TSource>
   (this IEnumerable source, int index)
【VB】
シーケンス内の指定されたインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。
<ExtensionAttribute>
Public Shared Function ElementAtOrDefault(Of TSource) 
    (source As IEnumerable(Of TSource), index As Integer) 
    As TSource

シーケンス(配列やコレクションなど)からインデックスを指定して要素を取得します。
ElementAtメソッドは指定したインデックスの要素がなければ例外をスローします。
ElementAtOrDefaultメソッドは指定したインデックスの要素がなければデフォルト値(値型なら初期値、参照型ならnull)を返します。

このメソッドは即時実行されます。


テストデータです。
【C#】
private class Fruit
{
    public string Name { get; set; }
    public string Rank { get; set; }
    public decimal Price { get; set; }
}

var fruits = new List<Fruit>()
                {
                    new Fruit(){Name = "りんご", Rank = "A" , Price = 1000 },
                    new Fruit(){Name = "みかん", Rank = "A" , Price = 600 },
                    new Fruit(){Name = "ぶどう", Rank = "B" , Price = 1200 },
                    new Fruit(){Name = "りんご", Rank = "B" , Price = 800 },
                    new Fruit(){Name = "みかん", Rank = "A" , Price = 500 }
                };
【VB】
Private Class Fruit
    Public Property Name As String
    Public Property Rank As String
    Public Property Price As Decimal
End Class

Dim fruits = New List(Of Fruit)() From
            {
                New Fruit() With {.Name = "りんご", .Rank = "A", .Price = 1000},
                New Fruit() With {.Name = "みかん", .Rank = "A", .Price = 600},
                New Fruit() With {.Name = "ぶどう", .Rank = "B", .Price = 1200},
                New Fruit() With {.Name = "りんご", .Rank = "B", .Price = 800},
                New Fruit() With {.Name = "みかん", .Rank = "A", .Price = 500}
            }
まずはElementAtメソッド、ElementAtOrDefaultメソッドを使用してインデックス0番目の要素を取得します。
0番目の要素は存在するので、どちらも問題なく取得できます。
【C#】
//ElementAtメソッドを使用して、インデックス0番目の要素を取得
Fruit fruit1 = fruits.ElementAt(0);
//出力
Console.WriteLine(fruit1.Name);
//りんご


//ElementAtOrDefaultメソッドを使用して、インデックス0番目の要素を取得
Fruit fruit2 = fruits.ElementAtOrDefault(0);
//出力
Console.WriteLine(fruit2.Name);
//りんご
【VB】
'ElementAtメソッドを使用して、インデックス0番目の要素を取得
Dim fruit1 As Fruit = fruits.ElementAt(0)
'出力
Console.WriteLine(fruit1.Name)
'りんご

'ElementAtOrDefaultメソッドを使用して、インデックス0番目の要素を取得
Dim fruit2 As Fruit = fruits.ElementAtOrDefault(0)
'出力
Console.WriteLine(fruit2.Name)
'りんご
次に存在しないインデックス5番目の要素を取得してみます。
ElementAtメソッドはArgumentOutOfRangeException例外がスローされるますが、
ElementAtOrDefaultメソッドでは、参照型の規定値であるnullが返ります。
【C#】
//ElementAtメソッドを使用して、インデックス5番目の要素を取得
//ArgumentOutOfRangeException例外がスローされる
Fruit fruit1 = fruits.ElementAt(5);

//ElementAtメソッドを使用して、インデックス5番目の要素を取得
//参照型なのでnullが返る
Fruit fruit2 = fruits.ElementAtOrDefault(5);
//出力
Console.WriteLine(fruit2 == null ? "null" : fruit2.Name);
//null
【VB】
'ElementAtメソッドを使用して、インデックス5番目の要素を取得
'ArgumentOutOfRangeException例外がスローされる
Dim fruit1 As Fruit = fruits.ElementAt(5)

'ElementAtメソッドを使用して、インデックス5番目の要素を取得
'参照型なのでnullが返る
Dim fruit2 As Fruit = fruits.ElementAtOrDefault(5)
'出力
Console.WriteLine(If(fruit2 Is Nothing, "nothing", fruit2.Name))
'nothing
※VBの場合、コンパイルオプションで「Option Infer」をONにし、型推論を有効にしてください。

.Net(VB C#) LINQのメソッド一覧

0 件のコメント: