[Go]. NET the difference between several methods of First,firstordefault,single,singleordefault

Source: Internet
Author: User

The. NET System.Linq.Enumerable class provides us with many Linq methods, and today we share some of the differences between the First,firstordefault,single,singleordefault methods. Examples and usage scenarios, the first is the definition of these methods:
First: Returns one element in the sequence.
FirstOrDefault: Returns the first element in a sequence, or a default value if the sequence contains no elements.
Single: Returns the unique element of the sequence, or throws an exception if the sequence does not contain exactly one element.
Singleordefault: Returns the unique element in a sequence, returns a default value if the sequence is empty, or throws an exception if the sequence contains more than one element.
The definition is very simple, I believe everyone seems to have no difficulty, below the C # example to demonstrate the specific usage:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6  7 namespaceSingleordefaultdemo8 {9   class ProgramTen   { One     Static voidMain (string[] args) A     { -       varLST =Newlist<int> {Ten, -, -, -, -, -, the }; -   the       //output Result: Ten -Console.WriteLine ("first:{0}", LST. First ()); -       //output Result: Ten -Console.WriteLine ("firstordefault:{0}", LST. FirstOrDefault ()); +   -       //Output: 100 (note: Because first here is the index of the list<int> to fetch one element that meets the criteria, the result is 100, not in order from small to large) +Console.WriteLine ("first:{0}"Lst. First (x = x > -)); A       //output: 100 (ibid. first) atConsole.WriteLine ("firstordefault:{0}"Lst. FirstOrDefault (x = x > -)); -   -       //throw an exception: The sequence contains more than one element -       //Console.WriteLine ("Single:{0}", LST. single ()); -       //throw an exception: The sequence contains more than one element -       //Console.WriteLine ("Singleordefault:{0}", LST. Singleordefault ()); in   -       //Throw exception: sequence does not contain any matching elements to       //Console.WriteLine ("Single:{0}", LST. Single (x = x >)); +       //output results: 0 -Console.WriteLine ("singleordefault:{0}"Lst. Singleordefault (x = x > -)); the   *Console.WriteLine ("Press any key to quit ..."); $ Console.readkey ();Panax Notoginseng     } -   } the}

Run-time Exceptions:

Run the result diagram:

So when do we use First,fristordefault,single,singleordefault?
1. When the collection contains multiple elements and you want to throw an exception, you can use single or Singleordefault
2. When the collection does not contain any elements but you need to return the default values, you can use the Singleordefault
3. When we want to return a record regardless of whether there are elements in the collection, you can use first or firstordefault
4. When the collection does not contain any elements but you need to return the default values, you can use the FirstOrDefault

Mnemonic: A method with Ordefault returns a value (if there is no element that meets the criteria, the default value is returned), and a method without Ordefault throws an exception

Performance comparison of FirstOrDefault and Singleordefault:
FirstOrDefault typically behaves better than singleordefault in performance, because FirstOrDefault is returned from the start of the collection to the first matching element, and Singleordefault iterates over all the elements in the collection.

[Go]. NET the difference between several methods of First,firstordefault,single,singleordefault

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.