Array.Contains
-
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.
-
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.
linq method:
list.Contains(value);
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx[^]
No more Mister Nice Guy... >: |
-
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.
-
Iterate through the array and do a match yourself:
for(int i=0; i < myArray.Length; i++) {
if (myArray[i] == valueToCheck) {
//Take action
break;
}
} -
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.
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
-
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
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
orArray.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) -
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
orArray.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)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
-
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.