C# Single() Method


Get only a single element of a sequence using the Single() method.

Let’s say we have a string array with only one element.

string[] str = { "one" };

Now, get the element.

str.AsQueryable().Single();

Here is our code.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      string[] str = { "one" };
      string res = str.AsQueryable().Single();
      Console.WriteLine(res);
   }
}

Output

one

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements