Applying the '==' operator to a common parameter

Possible duplicate:
Does the Cant == operator apply to generic types in C #?

I have a DatabaseLookup {} class, where the T parameter will be used by search methods in the class. Before searching, I want to see if T has already been raised with something like

if (T == previousLookupObject) ... 

This does not compile at all. What prevents me from making such a simple comparison?

+7
source share
4 answers

T is a type parameter. If your previousLookupObject is a Type object, you need to do typeof(T) == previousLookupObject .

If previousLookupObject is a variable of type T , you need to have the actual object T to compare it with.

If you want to know if the previousLookupObject type is of type T , you need to use the is : if (previousLookupObject is T) .

+16
source

T is a type, previousLookupObject is (I suppose) an instance of an object. So, you are comparing apples to oranges. Try the following:

 if (previousLookupObject is T) { ... } 
+8
source
+1
source

What type of previousLookupObject ? Typical type parameters are types and cannot be used as regular object references.

0
source

All Articles