Filters a sequence of values based on a predicate.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Where(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Where : seq<'Source> * Func<'Source, bool> -> seq<'Source>
<Extension()>
Public Function Where(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As IEnumerable(Of TSource)
The type of the elements of source
.
source
An IEnumerable<T> to filter.
predicate
A function to test each element for a condition.
An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.
source
or predicate
is null
.
The following code example demonstrates how to use Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to filter a sequence.
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
/*
This code produces the following output:
apple
mango
grape
*/
' Create a list of strings.
Dim fruits As New List(Of String)(New String() _
{"apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry"})
' Restrict the results to those strings whose
' length is less than six.
Dim query As IEnumerable(Of String) =
fruits.Where(Function(fruit) fruit.Length < 6)
' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' apple
' mango
' grape
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator
method directly or by using foreach
in C# or For Each
in Visual Basic.
In query expression syntax, a where
(C#) or Where
(Visual Basic) clause translates to an invocation of Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>).
C# supports two types of class methods: static and nonstatic. Any normal method is a nonstatic method.
A static method in C# is a method that keeps only one copy of the method at the Type level, not the object level. The last updated value of the method is shared among all objects of that Type. That means all class instances share the exact copy of the method and its data.
Static methods are called by using the class name, not the instance of the class.
The Console class and its Read and Write methods exemplify static methods. The following code example calls Console.WriteLine and Console.ReadKey methods without creating an instance of the Console class.
class Program
{
public static void withoutObj()
{
Console.WriteLine("Hello");
}
static void Main()
{
Program. withoutObj();
Console.ReadKey();
}
}
Usually, we define a set of data members for a class, and then every object of that class will have a separate copy of each data member. Let's give an example.
class Program
{
public int myVar; //a non-static field
static void Main()
{
Program p1 = new Program(); //a object of class
p1.myVar = 10;
Console.WriteLine(p1.myVar);
Console.ReadKey();
}
}
In the above example, myVar is a nonstatic field so to use this field; we first need to create the object of that class. On the other hand, static data is shared among all the objects of that class. That is, for all the objects of a class, there will be only one copy of static data. Let's give an example.
class Program
{
public static int myVar; //a static field
static void Main()
{
//Program p1 = new Program(); //a object of class
myVar = 10;
Console.WriteLine(myVar);
Console.ReadKey();
}
}
In the above, we don't have an object of the class to use that field since the field is static.
If you create your own class and think only one copy of the data (method) is needed among all instances of the class, you can create your own static method. Learn more here: Static Class and Static Class Members In C#
Notable Points here are
This article taught us Static Method In C#. C# language also has several other types of methods. Read more Types of Methods in C#.
Thanks for reading.
HAPPY CODING!!
187
0
0
All Comments (0)
If you are interested in sending in a Guest Blogger Submission,welcome to write for us!
Comments