Where. This is a C# keyword used inside queries. It is used in generic type constraints. And Where() is a method that filters elements from a collection.
We often use where in queries (with "from where"). As a type constraint, "where" is a less-common feature. The extension method Where() can filter an array with a lambda expression.
using System;
using System.Linq;
//// Example array that contains unwanted null and empty strings.//
string[] array = { "dot", "", "net", null, null, "perls", null };
//// Use Where method to remove null strings.//
var result1 = array.Where(item => item != null);
foreach (string value in result1)
{
Console.WriteLine(value);
}
//// Use Where method to remove null and empty strings.//
var result2 = array.Where(item => !string.IsNullOrEmpty(item));
foreach (string value in result2)
{
Console.WriteLine(value);
}dot
net
perls
dot
net
perls
Where, query. Where is not just a method. It is a contextual keyword in the C# language. This is the lowercase "where." It is part of a query expression (supported by System.Linq).
using System;
using System.Linq;
int[] numbers = { 10, 20, 30, 40 };
// ... Filter numbers with where.
var result = from number in numbers
where number >= 30
select number;
// ... Display results.
foreach (int value in result)
{
Console.WriteLine(value);
}30
40
Where, constraint. Consider a generic type. We can use a constraint keyword to ensure that the type can only be used with certain types of elements. We use "where" to constrain a type.
Tip Here we create a generic type called PageSet that can act on any element that implements the IPage interface.
Tip 2 We can use PageSet for any class that implements IPage—only one generic implementation is needed (this is a key advantage).
Also More information is available about type constraints and generic types, and information about closed and open types.
using System;
using System.Collections.Generic;
/// <summary>/// PageSet (could be set of anything that implements IPage)./// </summary>
class PageSet<T> where T : IPage
{
List<T> _pages = new List<T>();
public void Add(T page)
{
_pages.Add(page);
}
public void Render()
{
foreach (T page in _pages)
{
page.Render();
}
}
}
/// <summary>/// IPage./// </summary>
interface IPage
{
void Render();
}
/// <summary>/// ImagePage (implements IPage)./// </summary>
class ImagePage : IPage
{
public void Render()
{
Console.WriteLine("Render ImagePage");
}
}
class Program
{
static void Main()
{
// ... Create a generic type with constraint.
PageSet<ImagePage> pages = new PageSet<ImagePage>();
// ... Add an instance to the generic type.
ImagePage image = new ImagePage();
pages.Add(image);
// ... Use the generic type.
pages.Render();
}
}Render ImagePage
A key advantage. For type constraints the key advantage is the ability to reuse code when possible. We can share a single implementation, even if the type requires a certain interface.
A summary. We saw the Where() method. This method filters the elements in an IEnumerable collection with a lambda expression. And we used where in queries and generics.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 29, 2023 (edit link).