LinkedList<T>.Contains(T) method is used to check whether a value is in the LinkedList<T> or not.
Syntax:
public bool Contains (T value);
Here, value is the value to locate in the LinkedList<T>. The value can be null for reference types.
Return Value: This method returns True if value is found in the LinkedList, otherwise, False.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to check if a // value is in LinkedList using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Strings LinkedList<String> myList = new LinkedList<String>(); // Adding nodes in LinkedList myList.AddLast("A"); myList.AddLast("B"); myList.AddLast("C"); myList.AddLast("D"); myList.AddLast("E"); // To check if a value is in LinkedList Console.WriteLine(myList.Contains("B")); } } |
Output:
True
Example 2:
// C# code to check if a // value is in LinkedList using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Integers LinkedList<int> myList = new LinkedList<int>(); // Adding nodes in LinkedList myList.AddLast(1); myList.AddLast(2); myList.AddLast(3); myList.AddLast(4); myList.AddLast(5); // To check if a value is in LinkedList Console.WriteLine(myList.Contains(8)); } } |
Output:
False
Note: This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Check if two String objects have the same value | Set-1
- C# | Check if the Hashtable contains a specific Value
- C# | Check if a SortedList object contains a specific value
- C# | Check if the StringDictionary contains a specific value
- C# | Get the maximum value in the SortedSet
- C# | Get the minimum value in the SortedSet
- C# | Count the number of key/value pairs in the Hashtable
- C# | Get or Set the value associated with specified key in Hashtable
- C# | How to add key/value pairs in SortedList
- C# | Set all bits in the BitArray to the specified value
- C# | Set the bit at a specific position in the BitArray to the specified value
- C# | Get value of the bit at a specific position in BitArray
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Add key and value into StringDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Add the specified key and value into the ListDictionary
- C# | Get or set the value associated with specified key in ListDictionary
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Adding the specified key and value into HybridDictionary
- C# | Count the number of key/value pairs in HybridDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

