TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
where clause in C# is a contextual keyword utilized in two distinct language constructs: enforcing compile-time type constraints on generic parameters, and applying boolean predicate filters within Language Integrated Query (LINQ) expressions.
Generic Type Constraints
In generic programming, thewhere clause restricts the types that can be substituted for a type parameter. This allows the compiler to assume specific structural capabilities (such as methods, constructors, or base types) about the type argument, ensuring type safety without requiring boxing or runtime type checking.
Syntax:
- Value/Reference Constraints:
struct: The type argument must be a non-nullable value type.class: The type argument must be a reference type.class?: The type argument must be a nullable or non-nullable reference type.notnull: The type argument must be a non-nullable value or reference type.unmanaged: The type argument must be a non-nullable, non-reference type that contains no reference type fields at any level of nesting.default: The type argument is not constrained toclassorstruct. This is used when overriding methods or explicitly implementing interfaces to resolve ambiguity when the base method lacks aclassorstructconstraint.
- Inheritance Constraints:
BaseClassName: The type argument must be or derive from the specified base class.InterfaceName: The type argument must implement the specified interface.U: The type argument supplied forTmust be or derive from the argument supplied forU(Naked Type Constraint).
- Constructor Constraints:
new(): The type argument must have a public parameterless constructor.
- Anti-Constraints:
allows ref struct: The type argument can be aref struct. This lifts the implicit restriction that generic type parameters cannot beref structtypes (introduced in C# 13).
- Primary constraint (
class,class?,struct,unmanaged,notnull,default, or a specific base class). - Anti-constraint (
allows ref struct). In C# 13, this cannot be combined withclass,class?,unmanaged,default, a specific base class constraint, or thenew()constructor constraint. Among primary constraints, it can only be combined withstructornotnull, and must immediately follow the primary constraint. It can also be followed by secondary constraints (like interfaces). - Secondary constraints (interfaces or naked type constraints).
- Constructor constraint (
new()), which must always appear last.
LINQ Query Expressions
In query syntax, thewhere clause acts as a filtering operator. It evaluates a sequence of elements against a boolean expression (predicate) and yields only the elements that satisfy the condition.
Syntax:
where clause into a method invocation named Where. It will bind to any accessible method (instance or extension) that matches the signature, converting the predicate to whatever delegate or expression type that specific method expects. While this commonly resolves to Enumerable.Where (expecting Func<TSource, bool>) or Queryable.Where (expecting Expression<Func<TSource, bool>>), it is not strictly limited to those types.
- Deferred Execution: The
whereclause does not execute immediately. The predicate is evaluated lazily; elements are processed one by one only when the resulting sequence is actively enumerated (e.g., via aforeachloop or a terminal operation likeToList()). - Short-Circuiting: While
whereitself evaluates the predicate for elements as they are yielded, combining it with terminal operators likeFirst()orAny()will short-circuit the enumeration pipeline once the condition is met. - Multiple Clauses: Multiple
whereclauses in a single query expression are translated into chained.Where()method calls, which logically act as a booleanANDoperation.
Master C# with Deep Grasping Methodology!Learn More





