SingleorDefault() Method in C#

SingleorDefault() Method in C#

using System;
using System.Linq;
using System.Collections.Generic;
public class Example {
   public static void Main() {
      string[] str1 = { "Rajive" };
      string[] str2 = { };
      string res = str1.AsQueryable().Single();
      Console.WriteLine("String found: "+res);
      string res1 = str2.AsQueryable().SingleOrDefault();
      Console.WriteLine(String.IsNullOrEmpty(res1) ? "String not found" : res1);
   }
}

Output

 

String found: one
String not found

2020-03-01