Getting Started With C#
Literals
Operators
Conditional Statements
Equality Operator
Equals and GetHashCode
Null-Coalescing Operator
Null-Conditional Operators
nameof Operator
Verbatim String
Common String Operations
String Format
String Concatenate
String Manipulation
String Interpolation
String Escape Sequences
StringBuilder
Regex
DateTime
Arrays
On Algorithm for circular rotation of an array
Enum
Tuples
Overview of C# collections
GUID
BigInteger
Collection Initializers
Looping
Iterators
IEnumerable
Value type vs Reference type
Built-in Types
Aliases of built-in types
Anonymous types
Dynamic type
Type Conversion
Casting
Nullable types
Constructors and Finalizers
Access Modifiers
Interfaces
Static Classes
Singleton Implementation
Dependency Injection
Partial class and methods
Object Initializers
Methods
Extension Methods
Named Arguments
Named And Optional Arguments
Data Annotation
Keywords
Recursion
Naming Conventions
XML Documentation Comments
Comments and regions
Inheritance
Generics
Using Statement
Using Directive
IDisposable interface
Reflection
IQueryable interface
LINQ to Objects
LINQ Queries
LINQ to XML
Parallel LINQ PLINQ
XmlDocument
XDocument
C# 7.0 Features
C# 6.0 Features
C# 5.0 Features
C# 4.0 Features
C# 3.0 Features
Exception Handling
NullReferenceException
Handling FormatException
Read and Understand Stack trackes
Diagnostics
Overflow
JSON handling
Using json.net
Lambda Expressions
Lambda expressions
Generic Lambda Query Builder
Properties
Initializing Properties
INotifyPropertyChanged interface
Events
Expression Trees
Overload Resolution
Preprocessor directives
Structs
Attributes
Delegates
File and Stream IO
Networking
Performing HTTP requests
Reading and writing .zip files
FileSystemWatcher
Asynchronous Socket
Action Filters
Polymorphism
Immutability
Indexer
Checked and Unchecked
Stream
Timers
Stopwatches
Threading
Async/Await, BackgroundWorker, Task and Thread examples
Async Await
Synchronization Context in Async/Await
BackgroundWorker
Task Parallel Library
Making a variable thread safe
Lock Statement
Yield Keyword
Task Parallel Library TPL Dataflow Constructs
Functional Programming
Func delegates
Function with multiple return values
Binary Serialization
ICloneable
IComparable
Accessing Databases
Using SQLite
Caching
Code Contracts
Code Contracts and Assertions
Structural Design Patterns
Creational Design Patterns
Implementing Decorating Design Pattern
Implementing Flyweight Design Pattern
System.Management.Automation
Pointers
Pointers and Unsafe Code
Simulating C Unions with C# Structs
Reactive Extensions Rx
AsseblyInfo.cs examples
Creating Console Application
CLSCompliantAttribute
ObservableCollection<T>
Hash Functions
Generating Random Numbers
Cryptography
Unsafe Code in .NET
C# Script
Runtime Compile
Interoperability
.NET Compiler Platform Roslyn
Creating Own MessageBox in Windows Form Application
Including Font Resources
Garbage Collector
Windows Communication Foundation
Contributors

First FirstOrDefault Last LastOrDefault Single and SingleOrDefault

suggest change

All six methods return a single value of the sequence type, and can be called with or without a predicate.

Depending on the number of elements that match the predicate or, if no predicate is supplied, the number of elements in the source sequence, they behave as follows:

First()

Example

// Returns "a":
new[] { "a" }.First();

// Returns "a":
new[] { "a", "b" }.First();

// Returns "b":
new[] { "a", "b" }.First(x => x.Equals("b"));

// Returns "ba":
new[] { "ba", "be" }.First(x => x.Contains("b"));

// Throws InvalidOperationException:
new[] { "ca", "ce" }.First(x => x.Contains("b"));

// Throws InvalidOperationException:
new string[0].First();

Live Demo on .NET Fiddle

FirstOrDefault()

Example

// Returns "a":
new[] { "a" }.FirstOrDefault();

// Returns "a":
new[] { "a", "b" }.FirstOrDefault();

// Returns "b":
new[] { "a", "b" }.FirstOrDefault(x => x.Equals("b"));

// Returns "ba":
new[] { "ba", "be" }.FirstOrDefault(x => x.Contains("b"));

// Returns null:
new[] { "ca", "ce" }.FirstOrDefault(x => x.Contains("b"));

// Returns null:
new string[0].FirstOrDefault();

Live Demo on .NET Fiddle

Last()

Example

// Returns "a":
new[] { "a" }.Last();

// Returns "b":
new[] { "a", "b" }.Last();

// Returns "a":
new[] { "a", "b" }.Last(x => x.Equals("a"));

// Returns "be":
new[] { "ba", "be" }.Last(x => x.Contains("b"));

// Throws InvalidOperationException:
new[] { "ca", "ce" }.Last(x => x.Contains("b"));

// Throws InvalidOperationException:
new string[0].Last();

LastOrDefault()

Example

// Returns "a":
new[] { "a" }.LastOrDefault();

// Returns "b":
new[] { "a", "b" }.LastOrDefault();

// Returns "a":
new[] { "a", "b" }.LastOrDefault(x => x.Equals("a"));

 // Returns "be":
new[] { "ba", "be" }.LastOrDefault(x => x.Contains("b"));

// Returns null:
new[] { "ca", "ce" }.LastOrDefault(x => x.Contains("b")); 

// Returns null:
new string[0].LastOrDefault();

Single()

Example

// Returns "a":
new[] { "a" }.Single();

// Throws InvalidOperationException because sequence contains more than one element:
new[] { "a", "b" }.Single();

// Returns "b":
new[] { "a", "b" }.Single(x => x.Equals("b"));

// Throws InvalidOperationException:
new[] { "a", "b" }.Single(x => x.Equals("c"));

// Throws InvalidOperationException:
new string[0].Single(); 

// Throws InvalidOperationException because sequence contains more than one element:
new[] { "a", "a" }.Single();

SingleOrDefault()

Example

// Returns "a":
new[] { "a" }.SingleOrDefault();

// returns "a"
new[] { "a", "b" }.SingleOrDefault(x => x == "a"); 

// Returns null:
new[] { "a", "b" }.SingleOrDefault(x => x == "c");

// Throws InvalidOperationException:
new[] { "a", "a" }.SingleOrDefault(x => x == "a");

// Throws InvalidOperationException:
new[] { "a", "b" }.SingleOrDefault();

// Returns null:
new string[0].SingleOrDefault();

Recommendations

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents