Rajanikant Hawaldar
What is the opposite of 'Contains' in C#?

What is the opposite of ‘Contains’ in C#?

By Rajanikant Hawaldar in C# on Oct 27 2020
  • Anu Viswan
    Oct, 2020 27

    You haven’t quite mentioned the context of your question, for making a guess here.

    Easiest way to check if “not” contains would be to use negation operator !. For example,

    To check to ensure does not contain a particular substring
    if(!str.Contains(substring))
    To ensure a particular item is not part of collection

    1. if(!list.Contains(item))

    • 3
  • Liviu Sosu
    Dec, 2020 13

    I find the question pretty confusing if not silly.

    1. In plain C# there is no such thing as “the opposite of ‘Contains’ in C#”
    2. In LINQ you have the Method “Exclude”. LINQ is related to C#, as it is in the BCL starting with version 3.5

    • 2
  • Yogeshkumar Hadiya
    Nov, 2020 22

    you can use not operator for check enter value is not in list .

    1. if (!myList.Contains("name"))
    2. {
    3. myList.Add("name");
    4. }

    or you can use any method to perform same.

    1. if (!myList.Any(s => s == "name"))
    2. {
    3. myList.Add("name");
    4. }

    • 1
  • Ishoo Anyal
    Nov, 2020 8

    you can use Except in case of list types

    • 1
  • Debraj Chakraborty
    Dec, 2020 20

    Use ! operator

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS