<p>here is what a I'm doing:  </p>

<pre><code>object ReturnMatch(System.Type type)  
{  
    foreach(object obj in myObjects)  
    {
        if (obj == type)  
        {  
            return obj;  
        }  
    }  
}  
</code></pre>

<p>However, if obj is a subclass of <code>type</code>, it will not match. But I would like the function to return the same way as if I was using the operator <code>is</code>.</p>

<p>I tried the following, but it won't compile:</p>

<pre><code>if (obj is type) // won't compile in C# 2.0  
</code></pre>

<p>The best solution I came up with was:</p>

<pre><code>if (obj.GetType().Equals(type) || obj.GetType().IsSubclassOf(type))  
</code></pre>

<p>Isn't there a way to use operator <code>is</code> to make the code cleaner?</p>

[{'answer_id': 72384, 'author': 'Thunder3', 'author_id': 2832, 'author_profile': 'https://Stackoverflow.com/users/2832', 'pm_score': 2, 'selected': False, 'text': '<p>Perhaps </p>\n\n<pre><code>type.IsAssignableFrom(obj.GetType())\n</code></pre>\n'}, {'answer_id': 72407, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 0, 'selected': False, 'text': '<p>the is operator indicates whether or not it would be \'safe\' to cast one object as another obeject (often a super class).</p>\n\n<pre><code>if(obj is type)\n</code></pre>\n\n<p>if obj is of type \'type\' or a subclass thereof, then the if statement will succeede as it is \'safe\' to cast obj as (type)obj.</p>\n\n<p>see: <a href="http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx</a></p>\n'}, {'answer_id': 72503, 'author': 'icelava', 'author_id': 2663, 'author_profile': 'https://Stackoverflow.com/users/2663', 'pm_score': 0, 'selected': False, 'text': '<p>Is there a reason why you cannot use the "is" keyword itself?</p>\n\n<pre><code>foreach(object obj in myObjects)\n{\n  if (obj is type)\n  {\n    return obj;\n  }\n}\n</code></pre>\n\n<p>EDIT - I see what I was missing. Isak\'s suggestion is the correct one; I have tested and confirmed it.</p>\n\n<pre><code>  class Level1\n  {\n  }\n\n  class Level2A : Level1\n  {\n  }\n\n  class Level2B : Level1\n  {\n  }\n\n  class Level3A2A : Level2A\n  {\n  }\n\n\n  class Program\n  {\n    static void Main(string[] args)\n    {\n      object[] objects = new object[] {"testing", new Level1(), new Level2A(), new Level2B(), new Level3A2A(), new object() };\n\n\n      ReturnMatch(typeof(Level1), objects);\n      Console.ReadLine();\n    }\n\n\n    static void ReturnMatch(Type arbitraryType, object[] objects)\n    {\n      foreach (object obj in objects)\n      {\n        Type objType = obj.GetType();\n\n        Console.Write(arbitraryType.ToString() + " is ");\n\n        if (!arbitraryType.IsAssignableFrom(objType))\n          Console.Write("not ");\n\n        Console.WriteLine("assignable from " + objType.ToString());\n\n      }\n    }\n  }\n</code></pre>\n'}, {'answer_id': 72525, 'author': 'Paul van Brenk', 'author_id': 1837197, 'author_profile': 'https://Stackoverflow.com/users/1837197', 'pm_score': 2, 'selected': False, 'text': '<p>Not using the is operator, but the Type.IsInstanceOfType Method appears to be what you\'re looking for.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.type.isinstanceoftype.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.type.isinstanceoftype.aspx</a></p>\n'}, {'answer_id': 72550, 'author': 'Isak Savo', 'author_id': 8521, 'author_profile': 'https://Stackoverflow.com/users/8521', 'pm_score': 4, 'selected': True, 'text': "<p>I've used the IsAssignableFrom method when faced with this problem.</p>\n\n<pre><code>Type theTypeWeWant; // From argument or whatever\nforeach (object o in myCollection)\n{\n    if (theTypeWeWant.IsAssignableFrom(o.GetType))\n         return o;\n}\n</code></pre>\n\n<p>Another approach that may or may not work with your problem is to use a generic method:</p>\n\n<pre><code>private T FindObjectOfType&lt;T&gt;() where T: class\n{\n    foreach(object o in myCollection)\n    {\n        if (o is T)\n             return (T) o;\n    }\n    return null;\n}\n</code></pre>\n\n<p>(Code written from memory and is not tested)</p>\n"}]
['https://Stackoverflow.com/questions/72360', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10833/']