473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

generic method that tests if array contains a value

Is it possible to write a generic method which returns true or false,
depending on if the array contains a value?

Is there already a framework method that provides this functionality?

the following code does not compile. Error is: Operator == cannot be
applied to operands of type T and T.

public static bool Contains<T>(T[] InValues, T InPattern)
{
bool doesContain = false;
foreach (T vlu in InValues)
{
if (vlu == InPattern)
{
doesContain = true;
break;
}
}
return doesContain;
}

May 10 '07 #1
2 5037
"Steve Richter" <St************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
the following code does not compile. Error is: Operator == cannot be
applied to operands of type T and T.
You can get it to compile if you use Equals instead of == :

if (vlu.Equals(InPattern))
{
doesContain = true;
break;
}

If the class <Tdoes an adequate override of Object.Equals, then you
will get a meaningful behaviour. Otherwise, Equals defaults to calling
ReferenceEquals, which will consider the two elements you are comparing to
be equal if they contain the same reference, and will consider that they are
different if the reference is different, regardless of wether the contents
are equal or not. This may or may not be adequate for your needs, depending
on what <Tis and what you want to do with it.
May 10 '07 #2
Well, you could use Array.IndexOf<T>(array, instance) >= 0...

However, ICollection<Thas a Contains(T) method, and T[] :
ICollection<T>, hence (and more re-usable, if trivial):

static void Main() {
int[] values = { 1, 2, 3, 4, 5 };
Console.WriteLine(Contains(values, 3));
Console.WriteLine(Contains(values, 6));
}
static bool Contains<T>(ICollection<Tdata, T value) {
if (data == null) throw new ArgumentNullException("data");
return data.Contains(value);
}

Marc
May 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
2
by: Stephen Costanzo | last post by:
I have: Public Class test Private displayValue As String Private dbType As Integer Property Display() As String Get Return displayValue End Get
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.