简体   繁体   English

这个语法有什么作用? if(obj是SomeType obj2)

[英]What does this syntax do? if(obj is SomeType obj2)

I'm starting to see these statements and I am trying to wrap my head around these kind of statements. 我开始看到这些陈述,我试图围绕这些陈述。

if (obj is SomeAuto car)
{
   //Do stuff
}

If I understand correctly we basically are casting obj into the variable car which would be a type "SomeAuto"? 如果我理解正确,我们基本上将obj转换为变量汽车,这将是一个类型“SomeAuto”?

1) What is the official terminology of this statement? 1)本声明的官方术语是什么?

2) What would happen if I wanted to change the if statement to conditionally execute for a particular reason? 2)如果我想将if语句更改为因特定原因有条件执行会发生什么?

For example say SomeAuto is a base class and I only wanted a certain type of auto, or say I want all of the SomeAuto except maybe one particular kind. 例如,说SomeAuto是一个基类,我只想要某种类型的汽车,或者说我想要所有的SomeAuto,除了可能是一种特殊类型。

This if statement is using the is expression added in C# 7.0 under pattern matching . 此if语句使用在模式匹配下C#7.0中添加的is表达式。 Docs specify that: 文档指定:

The is pattern expression extends the familiar is operator to query an object beyond its type. is模式表达式扩展了熟悉的is运算符,以查询超出其类型的对象。

It enables you to check if obj is of a specific type and also assigns the casted result into a variable. 它使您能够检查obj是否属于特定类型,并将转换结果分配给变量。


Before these feature you'd probably write: 在这些功能之前你可能会写:

var car = obj as SomeAuto;
if(car != null)
{
    //Do Stuff
}

As pointed out by @BurnBA a difference when using the as than the original is is that Note that the as operator performs only reference conversions, nullable conversions, and boxing conversion and therefore cannot be used to check non-nullable value types. 作为使用时所指出的@BurnBA的差as比原来的is的是, 请注意,作为操作者只执行引用转换,可为空的转换,和装箱转换 ,因此不能被用于检查非空值类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 obj1.Equals(obj2)和c#中的静态Object.Equals(obj1,obj2)有什么区别? - What's the difference between obj1.Equals(obj2) and static Object.Equals(obj1, obj2) in c#? Object.Equals(obj1,obj2)vs obj1.Equals(obj2)? - Object.Equals(obj1, obj2) vs obj1.Equals(obj2)? 你如何创建一个类似数组的C#构造函数,它将允许“new MyClass(){obj1,obj2,obj3};” - How do you create an array-like C# constructor that will allow “new MyClass() { obj1, obj2, obj3 };” 参考平等性能差异? ((object)obj1 ==(object)obj2)vs。object.ReferenceEquals(obj1,obj2) - Reference equality performance difference? ((object)obj1 == (object)obj2) vs. object.ReferenceEquals( obj1, obj2 ) 转换清单 <OBj1> 列出 <Obj2> 在Windows Phone 8中 - Convert List<OBj1> to List<Obj2> in windows phone 8 我怎么知道对象完全像obj1 = obj2一样被改变了? - How can I know that object is changed totally like obj1 = obj2? 使用(object obj = new Object())是什么意思? - What does using(object obj = new Object()) mean? 用什么? 我用什么来制作一个使用 c# 打开 .obj 和 .dae 的程序? - Using what? What do I use to make a program that opens .obj and .dae using c#? 什么是 <class> 在load_obj.AddComponent中 <Class> ();? - what is <class> in load_obj.AddComponent<Class>();? c#对象obj的值为{}。 什么是 ”{}”? - c# Object obj's value is {}. What is “{}”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM