[Reserved] List in C # and collection SingleOrDefault any different method FirstOrDefault

During operation of the C # List collection class, sometimes we use to SingleOrDefault method and other methods List FirstOrDefault collection, these two methods are System.Linq.Enumerable Linq class provides methods for us. That's SingleOrDefault FirstOrDefault method and other methods so what difference is it. In fact, a similar method () and Single () method and the like in addition to the above two methods, there First.

The following are different from each method, and the specific meanings:

(1) First method: Returns the List collection of the first sequence of qualifying elements, if not found, then throw runtime exception.

(2) FirstOrDefault Method: Returns a collection of the first sequence List qualifying element, if not found, a corresponding default value is returned as a reference type, then return null object.

(. 3) Single Method: return List sequence set only recording, or if no return multiple records, an exception is thrown.

(. 4) the SingleOrDefault Method: Returns the unique sequence of records List collection, if the sequence is empty, a default value is returned; if the sequence comprising a plurality of elements, an exception is thrown.

 

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link in C # List SingleOrDefault collection methods and FirstOrDefault What is the difference _IT technology small fun house .

Guess you like

Origin www.cnblogs.com/xu-yi/p/11026444.html