I've seen a lot of people use the following code:
Type t = typeof(obj1); if (t == typeof(int)) // Some code here
But I know you can do the same:
if (obj1.GetType() == typeof(int)) // Some code here
Or this:
if (obj1 is int) // Some code here
Personally, I think the last one is the cleanest, but what do I lack? Which is the best way to use it or personal preference?
#1 building
I have a type property to compare. I can't use is (for example, my "type is" basetypeolookfor), but I can use the following:
base_type.IsInstanceOfType(derived_object); base_type.IsAssignableFrom(derived_type); derived_type.IsSubClassOf(base_type);
Note that when IsInstanceOfType and IsAssignableFrom return true to compare the same type, IsSubclassOf will return false. IsSubclassOf is not applicable to interfaces on the other two interfaces. (see also This question and answer . )
public class Animal {} public interface ITrainable {} public class Dog : Animal, ITrainable{} Animal dog = new Dog(); typeof(Animal).IsInstanceOfType(dog); // true typeof(Dog).IsInstanceOfType(dog); // true typeof(ITrainable).IsInstanceOfType(dog); // true typeof(Animal).IsAssignableFrom(dog.GetType()); // true typeof(Dog).IsAssignableFrom(dog.GetType()); // true typeof(ITrainable).IsAssignableFrom(dog.GetType()); // true dog.GetType().IsSubclassOf(typeof(Animal)); // true dog.GetType().IsSubclassOf(typeof(Dog)); // false dog.GetType().IsSubclassOf(typeof(ITrainable)); // false
#2 building
You can use the "typeof()" operator in C, but you need to use System.IO to call the namespace. If you want to check the type, you must use the 'is' keyword.
#3 building
Performance test typeof() vs GetType():
using System; namespace ConsoleApplication1 { class Program { enum TestEnum { E1, E2, E3 } static void Main(string[] args) { { var start = DateTime.UtcNow; for (var i = 0; i < 1000000000; i++) Test1(TestEnum.E2); Console.WriteLine(DateTime.UtcNow - start); } { var start = DateTime.UtcNow; for (var i = 0; i < 1000000000; i++) Test2(TestEnum.E2); Console.WriteLine(DateTime.UtcNow - start); } Console.ReadLine(); } static Type Test1<T>(T value) => typeof(T); static Type Test2(object value) => value.GetType(); } }
Results in debug mode:
00:00:08.4096636 00:00:10.8570657
Results in publish mode:
00:00:02.3799048 00:00:07.1797128
#4 building
The System.Type object used to get the type. The typeof expression takes the following form:
System.Type type = typeof(int); Example: public class ExampleClass { public int sampleMember; public void SampleMethod() {} static void Main() { Type t = typeof(ExampleClass); // Alternatively, you could use // ExampleClass obj = new ExampleClass(); // Type t = obj.GetType(); Console.WriteLine("Methods:"); System.Reflection.MethodInfo[] methodInfo = t.GetMethods(); foreach (System.Reflection.MethodInfo mInfo in methodInfo) Console.WriteLine(mInfo.ToString()); Console.WriteLine("Members:"); System.Reflection.MemberInfo[] memberInfo = t.GetMembers(); foreach (System.Reflection.MemberInfo mInfo in memberInfo) Console.WriteLine(mInfo.ToString()); } } /* Output: Methods: Void SampleMethod() System.String ToString() Boolean Equals(System.Object) Int32 GetHashCode() System.Type GetType() Members: Void SampleMethod() System.String ToString() Boolean Equals(System.Object) Int32 GetHashCode() System.Type GetType() Void .ctor() Int32 sampleMember */
This example uses the GetType method to determine the type used to contain the numerical calculation results. This depends on the storage requirements for the result number.
class GetTypeTest { static void Main() { int radius = 3; Console.WriteLine("Area = {0}", radius * radius * Math.PI); Console.WriteLine("The type is {0}", (radius * radius * Math.PI).GetType() ); } } /* Output: Area = 28.2743338823081 The type is System.Double */
#5 building
if (c is UserControl) c.Enabled = enable;