Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Types

Tags:

c#

.net

I've got a class looks quite like this:

object value;
Type type;

When I create the object I set the type to the type of the Object.

How can I compare this type with another type?

If for example the type is String:

type.Equals(String)

and

type == String 

does not work.

like image 312
mabstrei Avatar asked Feb 02 '11 14:02

mabstrei


People also ask

How do you compare two classes?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

How do I compare two data types in Python?

Use type() to compare types Call type(obj) to return the type of obj . Use == to compare this type with another.

How can I compare two data types in C#?

Equals(Object) Method. This method is used to check whether the underlying system type of the current defined Type object is exactly same as the underlying system type of the specified Object. Syntax: public override bool Equals (object obj);

How do you compare two variables in Java?

Method 1: using == operator Double equals operator is used to compare two or more than two objects, If they are referring to the same object then return true, otherwise return false. String is immutable in java. When two or more objects are created without new keyword, then both object refer same value.


1 Answers

In this context, you compare your Type instance with the result of typeof(T), where T is the type you want to compare.

bool objectIsString = myType == typeof(string);
like image 136
Anthony Pegram Avatar answered Sep 20 '22 06:09

Anthony Pegram