Essential C# Keywords  Suggest an edit

where

where can serve two purposes in C#: type constraining in a generic argument, and filtering LINQ queries.

In a generic class, let’s consider

public class Cup<T>
{
    // ...
}

T is called a type parameter. The class definition can impose constraints on the actual types that can be supplied for T.

The following kinds of constraints can be applied:

value type

In this case only structs (this includes ‘primitive’ data types such as int, boolean etc) can be supplied

public class Cup<T> where T : struct
{
    // ...
}

reference type

In this case only class types can be supplied

public class Cup<T> where T : class
{
    // ...
}

hybrid value/reference type

Occasionally it is desired to restrict type arguments to those available in a database, and these will usually map to value types and strings. As all type restrictions must be met, it is not possible to specify where T : struct or string (this is not valid syntax). A workaround is to restrict type arguments to IConvertible which has built in types of “… Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char, and String.” It is possible other objects will implement IConvertible, though this is rare in practice.

public class Cup<T> where T : IConvertible
{
    // ...
}

default constructor

Only types that contain a default constructor will be allowed. This includes value types and classes that contain a default (parameterless) constructor

public class Cup<T> where T : new
{
    // ...
}

inheritance and implementation

Only types that inherit from a certain base class or implement a certain interface can be supplied.

public class Cup<T> where T : Beverage
{
    // ...
}

public class Cup<T> where T : IBeer
{
    // ...
}

The constraint can even reference another type parameter:

public class Cup<T, U> where U : T
{
    // ...
}

Multiple constraints can be specified for a type argument:

public class Cup<T> where T : class, new()
{
    // ...
}

The previous examples show generic constraints on a class definition, but constraints can be used anywhere a type argument is supplied: classes, structs, interfaces, methods, etc.

where can also be a LINQ clause. In this case it is analogous to WHERE in SQL:

int[] nums = { 5, 2, 1, 3, 9, 8, 6, 7, 2, 0 };

var query =
    from num in nums 
    where num < 5
    select num;

foreach (var n in query)
{
    Console.Write(n + " ");
}
// prints 2 1 3 2 0

Table Of Contents
22 Enum
25 GUID
52 Keywords
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close