Java Class.isInstance() kiểm tra object có gán được cho kiểu không.
Tương đương C#:
Type.IsAssignableFrom()- Toán tử
is:bool res = (ob is DemoClass); Type.IsInstanceOfType():ob.GetType().IsInstanceOfType(otherOb)
Ví dụ:
object obj = "hello"; bool b1 = obj is string; // true bool b2 = typeof(string).IsAssignableFrom(obj.GetType()); // true bool b3 = typeof(string).IsInstanceOfType(obj); // true
Khuyến nghị: Dùng is pattern matching (C# 7+): if (obj is string s) { ... } - vừa kiểm tra vừa cast.