Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array.Contains

Array.Contains

Scheduled Pinned Locked Moved C#
csharpquestiondata-structures
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hello all, I've read that in dotnet framework 3.5 and above one can use the keyword Contains to check if an array has a particular value in one of it's elements. How do I check if an array contains a particular value in earlier versions of dotnet frameworks using C#? Thanks in advance.

    N L K B 4 Replies Last reply
    0
    • A ASPnoob

      Hello all, I've read that in dotnet framework 3.5 and above one can use the keyword Contains to check if an array has a particular value in one of it's elements. How do I check if an array contains a particular value in earlier versions of dotnet frameworks using C#? Thanks in advance.

      N Offline
      N Offline
      n podbielski
      wrote on last edited by
      #2

      linq method:

      list.Contains(value);

      http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx[^]

      No more Mister Nice Guy... >: |

      1 Reply Last reply
      0
      • A ASPnoob

        Hello all, I've read that in dotnet framework 3.5 and above one can use the keyword Contains to check if an array has a particular value in one of it's elements. How do I check if an array contains a particular value in earlier versions of dotnet frameworks using C#? Thanks in advance.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Iterate through the array and do a match yourself:

        for(int i=0; i < myArray.Length; i++) {
        if (myArray[i] == valueToCheck) {
        //Take action
        break;
        }
        }

        A 1 Reply Last reply
        0
        • L Lost User

          Iterate through the array and do a match yourself:

          for(int i=0; i < myArray.Length; i++) {
          if (myArray[i] == valueToCheck) {
          //Take action
          break;
          }
          }

          A Offline
          A Offline
          ASPnoob
          wrote on last edited by
          #4

          Hi, thanks for replying. That is exactly what I'm looking for.

          L 1 Reply Last reply
          0
          • A ASPnoob

            Hi, thanks for replying. That is exactly what I'm looking for.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Glad to be of help. Mark it as answer if that helped.

            1 Reply Last reply
            0
            • A ASPnoob

              Hello all, I've read that in dotnet framework 3.5 and above one can use the keyword Contains to check if an array has a particular value in one of it's elements. How do I check if an array contains a particular value in earlier versions of dotnet frameworks using C#? Thanks in advance.

              K Offline
              K Offline
              KiranKumar Roy
              wrote on last edited by
              #6

              Dear, You can use below function to check if Value exist in array or not..

              Function CheckIfValueExistInArray(ByVal strString As String) As Boolean
                  Dim animals() As String = {"lion", "turtle", "ostrich"}
                  If Array.IndexOf(animals, strString) <> -1 Then
                      'if exist will then retrun True
                      Return True
                  Else
                      'if does't exist then will retrun False
                      Return False
                  End If
              End Function
              

              Thanks

              KiranKumar Roy

              D 1 Reply Last reply
              0
              • K KiranKumar Roy

                Dear, You can use below function to check if Value exist in array or not..

                Function CheckIfValueExistInArray(ByVal strString As String) As Boolean
                    Dim animals() As String = {"lion", "turtle", "ostrich"}
                    If Array.IndexOf(animals, strString) <> -1 Then
                        'if exist will then retrun True
                        Return True
                    Else
                        'if does't exist then will retrun False
                        Return False
                    End If
                End Function
                

                Thanks

                KiranKumar Roy

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                KiranKumar Roy wrote:

                You can use below function to check if Value exist in array or not

                Not really. This is the C# forum, you have posted VB code. You are checking for a string, the OP could be searching for any type of data. You are searching an array that is created inside the method, typically he would be searching a class level variable or passing an array to a static method. Array.IndexOf or Array.IndexOf<T> are a good call though, but no need to wrap in another method. If you really want a separate method then extensions such as these would be better IMO:

                public static class Extensions
                {
                public static bool Contains(this object[] array, object value)
                {
                bool result = false;
                if (array != null)
                result = Array.IndexOf(array, value) > -1;
                return result;
                }
                public static bool Contains(this T[] array, T value)
                {
                bool result = false;
                if (array != null)
                result = Array.IndexOf(array, value) > -1;
                return result;
                }
                }

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                K 1 Reply Last reply
                0
                • D DaveyM69

                  KiranKumar Roy wrote:

                  You can use below function to check if Value exist in array or not

                  Not really. This is the C# forum, you have posted VB code. You are checking for a string, the OP could be searching for any type of data. You are searching an array that is created inside the method, typically he would be searching a class level variable or passing an array to a static method. Array.IndexOf or Array.IndexOf<T> are a good call though, but no need to wrap in another method. If you really want a separate method then extensions such as these would be better IMO:

                  public static class Extensions
                  {
                  public static bool Contains(this object[] array, object value)
                  {
                  bool result = false;
                  if (array != null)
                  result = Array.IndexOf(array, value) > -1;
                  return result;
                  }
                  public static bool Contains(this T[] array, T value)
                  {
                  bool result = false;
                  if (array != null)
                  result = Array.IndexOf(array, value) > -1;
                  return result;
                  }
                  }

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  K Offline
                  K Offline
                  KiranKumar Roy
                  wrote on last edited by
                  #8

                  I am really very sorry for posting VB code in C# forum.. I just want to give idea to ASPnoob, So he can apply according logic in his code. I will keep in mind that i will not post VB Code in C# form. Thanks.

                  KiranKumar Roy

                  1 Reply Last reply
                  0
                  • A ASPnoob

                    Hello all, I've read that in dotnet framework 3.5 and above one can use the keyword Contains to check if an array has a particular value in one of it's elements. How do I check if an array contains a particular value in earlier versions of dotnet frameworks using C#? Thanks in advance.

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #9

                    if(0 <= Array.IndexOf(array, element)){ ... }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups