Join Us

Enumerable.Where Method (System.Linq)

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)

Type Parameters

TSource

The type of the elements of source.

Parameters

source

IEnumerable<TSource>

An IEnumerable<T> to filter.

predicate

Func<TSource,Boolean>

A function to test each element for a condition.

Returns

An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.

Exceptions

ArgumentNullException

source or predicate is null.

Examples

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

Remarks

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>).

See also

Applies to

Introduction

C# supports two types of class methods: static and nonstatic. Any normal method is a nonstatic method. 

Static Method in C#

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();  
    }  
}  

Using Static Method

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

  1. A static method can be invoked directly from the class level
  2. A static method does not require any class object
  3. Any main() method is shared through the entire class scope, so it always appears with a static keyword.

Conclusion

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!!

Enumerable.Where Method (System.Linq)

Static Method In C#

187

0

Comments

0/2000

All Comments (0)

Guest Posts

If you are interested in sending in a Guest Blogger Submission,welcome to write for us!

Your Name: (required)

Your Email: (required)

Subject:

Your Message: (required)