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

Chaining methods

suggest change

Many LINQ functions both operate on an IEnumerable<TSource> and also return an IEnumerable<TResult>. The type parameters TSource and TResult may or may not refer to the same type, depending on the method in question and any functions passed to it.

A few examples of this are

public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TResult> selector
)

public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, int, bool> predicate
)

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

While some method chaining may require an entire set to be worked prior to moving on, LINQ takes advantage of deferred execution by using yield return MSDN which creates an Enumerable and an Enumerator behind the scenes. The process of chaining in LINQ is essentially building an enumerable (iterator) for the original set – which is deferred – until materialized by enumerating the enumerable.

This allows these functions to be fluently chained wiki, where one function can act directly on the result of another. This style of code can be used to perform many sequence based operations in a single statement.

For example, it’s possible to combine Select, Where and OrderBy to transform, filter and sort a sequence in a single statement.

var someNumbers = { 4, 3, 2, 1 };

var processed = someNumbers
        .Select(n => n * 2)   // Multiply each number by 2
        .Where(n => n != 6)   // Keep all the results, except for 6
        .OrderBy(n => n);     // Sort in ascending order

Output:

2 4 8

Live Demo on .NET Fiddle

Any functions that both extend and return the generic IEnumerable<T> type can be used as chained clauses in a single statement. This style of fluent programming is powerful, and should be considered when creating your own extension methods.

Feedback about page:

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



Table Of Contents