2

I have a generic function that I want to know how to write.

List<Something> something;

public int countItems<T>(List<T> Items)
{
    // Here I would like to compare the type of "Items" with the type of "something" to see if they are compatible. How do I do it?
    return 0;
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
prx
  • 21
  • 2
  • 1
    It really depends on your comparing method. When is something the same as Items? – RoflcoptrException Apr 18 '11 at 13:08
  • `var tAsSomething = Items[0] as Something; if (tAsSomething != null) ...` is my best-guess, if I understand correctly. – Brad Christie Apr 18 '11 at 13:10
  • Shades of the XY Problem. The first comment appears to take the original question title way too literally, so I've changed the title to what was actually meant. – Jim Balter Oct 22 '17 at 00:08
  • That said, the question is ambiguous because it isn't clear what "compatible" means here, and it isn't clear why `countItems` is generic if it can only count a list of `Something`s. If the problem is that you can't treat `List` as `List` where `D` is derived from `Something`, then use `IEnumerable` instead. – Jim Balter Oct 22 '17 at 00:24

4 Answers4

9

do you mean:

if(typeof(T) == typeof(Something)) {...}

?

Note that having generics depend hugely on the T (and act differently) may mean what you are trying to do isn't actually very generic...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The OP may want to know whether T is derived from Something, or v.v. This isn't entirely unreasonable, because you can't convert a `List` to `List` ...but then `IEnumerable` should be used instead. – Jim Balter Oct 22 '17 at 00:25
0

The question is poorly stated and more than a little vague, but this might do it:

using System.Linq;

public int countItems<T>(List<T> Items)
{
    return Items.OfType<Something>().Where(thing => thisOneCounts(thing)).Count();
}
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0
if (something.GetType() == items.GetType()) ...

This will compare the types of the actual objects.

Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
-1

I think what you want is to use the IComparable interface.

Another tool you may want to use is operator overloading, so that you can define how under what circumstances two objects are equal.

Cos Callis
  • 5,051
  • 3
  • 30
  • 57
  • The question is about comparing types, not values. – Jim Balter Oct 22 '17 at 00:27
  • You've edited a 6 year old question (that was poorly written) then proclaimed with certainty what it "Really" meant. FWIW I stand by my answer. Had OP come back then and clarified what they were looking for I might well have offered a more well developed answer about how to use IComparable and Operator overloads to accomplish the task at hand. Using an overload of = you can check for 'compatibility' by whatever standards one wishes to. I disagree that it is _about types_, when working with a List all items are of type and therefore must be 'compatible' at the type level. – Cos Callis Oct 22 '17 at 11:33
  • The age of the question is irrelevant (but gotta love the hypocrisy of extensive defense), my certainty is well warranted as it comes from the comment in the OP's example that you apparently never read, and so you're standing by something that is surely wrong. Even if I weren't wrong about the meaning, this is a poor answer, because it requires modifying classes that may not be under the OP's control. The bottom line is that my simple one line comment is factually correct and wasn't worth all the effort you put into your pointless response. I won't waste any more of my time on this. – Jim Balter Oct 22 '17 at 22:18