C# is as 用法
is 判断变量是否为指定的类型 如果是则返回true 否则返回false
1.
string str = "test"; object obj = str; bool isString = obj is string;
2.
续上面的代码
if(obj is string) { Console.WriteLine("obj类型为string"); }
3.
续2加条件
if(obj is string str2 && str2 == "test") { Console.WriteLine("obj类型为string且值=test"); }
as 直接转换类型 如果类型 = 指定的类型则返回对象的值 否则返回null
需要注意的是 指定的类型必须可以为空 引用类型或者是加了?的类型
string str = "test"; object obj = str; string str2 = obj as string; //str2 = test

浙公网安备 33010602011771号