is 关键字用于检查对象是否可以转换为特定类型。操作的返回类型为boolean。
示例using system;namespace demoapplication{ class program{ static void main(){ employee emp = new permanentemployee{ id = 1, name = "martin" }; // returns true as the derived type can be converted to base type. if (emp is employee){ console.writeline(emp.name + " is employee"); } else{ console.writeline(emp.name + " is not employee"); } //returns true, as the actual object is of type permanentemployee. if (emp is permanentemployee){ console.writeline(emp.name + " is permanentemployee"); } else{ console.writeline(emp.name + " is not permanentemployee"); } //returns false, as permanentemployee object cannot be converted to //contractemployee. if (emp is contractemployee){ console.writeline(emp.name + " is contractemployee"); } else{ console.writeline(emp.name + " is not contractemployee"); } } } class employee{ public int id { get; set; } public string name { get; set; } } class permanentemployee : employee{ public int annualsalary { get; set; } } class contractemployee : employee{ public int hourlysalary { get; set; } }}
以上就是c# 中“is”关键字有什么用?的详细内容。