0

Possible Duplicate:
C# how do I compare two objects if they are of same type?

I have a generic function,

class Something {
    public int found;
    public Something() {
        this.found = true;
    }
}
List<Something> something;

public int countFound<T>(List<T> Organisms)
{
    if (typeof(T) == typeof(Something))
    {
        foreach (T organism in Organisms)
        {
            // here i want to check something like organism.found == true how do i do it?
        }
    }
    return 0;
}

Thanks in advance for all the help!

Community
  • 1
  • 1

4 Answers4

2

You probably want something like this:

class Organism
{
    public bool Found
    {
        get;
        set;
    }
}

class Something : Organism
{
    public Something()
    {
        this.Found = true;
    }
}

public class Program
{
    public int countFound<T>(List<T> Organisms)
        where T : Organism
    {
        foreach (T organism in Organisms)
        {
            if (organism.Found)
            {
                // Do something with the organism
            }
        }

        return 0;
    }
}

The key points here are:

  • You have a common base class called Organism that defines the Found property
  • The Something class derives from Organism, setting Found to true when it gets constructed
  • The CountFound method has a generic constraint (the where clause) on T specifying that it must derive from Organism (The Something meets this criteria). This then allows you to use any method or property that Organism provides in the method - in this case Organism.Found.
Mike Goatly
  • 7,380
  • 2
  • 32
  • 33
  • 1
    Or, like ZoolWay suggests, you could make an interface on all the classes. However, given that the Found property may be provided on all Organism derivations then it would probably make most sense to use a base class like this. – Mike Goatly Apr 18 '11 at 13:47
1

You must limit your generic to one (or more) interface which dictates to implement to properties which are needed by your generic!

Let's say interface IFound implements the property you want to check:

public int countFound<T>(List<T> Organisms) where T : IFound
{     
    if (typeof(T) == typeof(Something))     
    {         
         foreach (T organism in Organisms)         
         {
              if(organism.found)) // done because IFound tells T has a property with this name
         }
    }     
    return 0; 
} 

IFound is an interface you must implement by yourself. For example:

interface IFound
{
    bool Found { get; }
}

Your class Something must implement IFound:

class Something : IFound
{
    public bool Found
    {
        get { return true; } // implement your condition in a method called here
    }
}

Then you can call your method like you wanted:

int a = countFound<Something>(List<Something> parameter);
ZoolWay
  • 5,411
  • 6
  • 42
  • 76
  • doesn't work for me what is an IFound? –  Apr 18 '11 at 13:37
  • See my edit. You can choose whatever name you think makes sense for it. Also every class you want to use with your countFound()-method must implement this - meaning your! - inteface. – ZoolWay Apr 18 '11 at 13:41
  • 1
    You can't have fields on interfaces. But more importantly @softplaza should make it a property. Don't expose public fields. – R. Martinho Fernandes Apr 18 '11 at 13:42
  • now how do i use countFound function ? like how do i call it? countFound(Something) doen't work for me –  Apr 18 '11 at 13:53
  • @Martinho Fernandes: Yeah, I meant an interface but did not remember the syntax correctly ;) @softplaza I updated the example further more. Please read into the topic of interface. – ZoolWay Apr 18 '11 at 14:43
1

There are two options here, depending on what you want the function to do:

If the countFound function must take all types T, but you want a special case when T is (or inherits from) Something, then you can use this:

public int countFound<T>(List<T> Organisms)
{
    if (typeof(T) == typeof(Something) || typeof(T).IsSubclassOf(typeof(Something)))
    {
        foreach (T organism in Organisms)
        {
            Something s = (Something)(object)organism;

            // do whatever you like with s
        }
    }
    return 0;
}

If you only want the function to take type T when T is (or inherits from) Something, then that's simpler:

public int countFound<T>(List<T> Organisms) where T : Something
{
    foreach (T organism in Organisms)
    {
        // here organism will have all of the properties of Something
    }

    return 0;
}
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

In your scenario it seems you wouldn't want to try to implement an equality function because equality would always be defined within the context of the types you are comparing(specific code per type to do the comparisons). This would work for you if all your T 's were of a common type(base class) and the equality condition could be expressed in terms of the base class' common properties etc...

Achilles
  • 11,165
  • 9
  • 62
  • 113