O Owen Feb 24, 2004 #1 Hello: I want to know the class of object, to compare to some class, how can I do that? Owen.
T Tom Porterfield Feb 24, 2004 #2 Owen said: Hello: I want to know the class of object, to compare to some class, how can I do that? Click to expand... The is operator can do this quite simply, it gets the type and handles the comparison for you. Ex: MyClass myObject = new MyClass; if (myObject is MyClass) return true; That would return true. -- Tom Porterfield MS-MVP MCE http://support.telop.org Please post all follow-ups to the newsgroup only.
Owen said: Hello: I want to know the class of object, to compare to some class, how can I do that? Click to expand... The is operator can do this quite simply, it gets the type and handles the comparison for you. Ex: MyClass myObject = new MyClass; if (myObject is MyClass) return true; That would return true. -- Tom Porterfield MS-MVP MCE http://support.telop.org Please post all follow-ups to the newsgroup only.
J Jon Skeet [C# MVP] Feb 24, 2004 #3 Owen said: I want to know the class of object, to compare to some class, how can I do that? Click to expand... Well, there's: if (myObject is SomeType) or myObject.GetType() to get the type of the object or typeof(SomeType) to get the type of SomeType (which you know in advance)
Owen said: I want to know the class of object, to compare to some class, how can I do that? Click to expand... Well, there's: if (myObject is SomeType) or myObject.GetType() to get the type of the object or typeof(SomeType) to get the type of SomeType (which you know in advance)
S Stoitcho Goutsev \(100\) [C# MVP] Feb 24, 2004 #4 Hi Owen, If you want to check whether an object is instance of given class you can use *as* or *is* operators. If you want to check exactly the run-time class of an object you can do if(obj.GetType() == typeof(SomeType)) { //The run-time type of the object is SomeType .... }
Hi Owen, If you want to check whether an object is instance of given class you can use *as* or *is* operators. If you want to check exactly the run-time class of an object you can do if(obj.GetType() == typeof(SomeType)) { //The run-time type of the object is SomeType .... }