Possible Duplicate: Direct casting vs 'as' operator? as doesn't throw an exception, but wouldn't it result in a NullPointerException later? doesn't it make it harder to debug? 在使用as之后,您应该始终检查null。
可能重复: 直接投射vs'as'运营商? 因为不会抛出异常,但不会导致后续的NullPointerException异常吗? 这难道不会让它更难调试吗? 在使用as之后,您应该始终检查null。
This question already has an answer here: Direct casting vs 'as' operator? 15 answers Casting vs using the 'as' keyword in the CLR 18 answers What is the difference between casting and using “as” in C#? 8 answers The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception. You can use the as
这个问题在这里已经有了答案: 直接投射vs'as'运营商? 15个答案 在CLR中使用“as”关键字进行投射 铸造和在C#中使用“as”有什么区别? 8个答案 as运算符就像一个演员操作。 但是,如果转换不可行,则返回null而不是引发异常。 您可以使用as运算符在兼容的引用类型或可为空的类型之间执行某些类型的转换。 考虑下面的例子: expression as type 该代码等同于下面的表达式,只是表达式变量只被计算一次。
This question already has an answer here: Direct casting vs 'as' operator? 15 answers The first one will throw InvalidCastException if the types don't match (refer to the documentation - "Explicit conversions" section). The second one (the as operator) will produce null value instead.
这个问题在这里已经有了答案: 直接投射vs'as'运营商? 15个答案 如果类型不匹配,第一个将抛出InvalidCastException (请参阅文档 - “显式转换”部分)。 第二个( as运算符)会生成null值。
Possible Duplicate: Direct casting vs 'as' operator? Where could this operator be useful? Instead of writing this: Asset a = new Asset(); Stock s = a as Stock; // s is null; no exception thrown if (s != null) Console.WriteLine (s.SharesOwned); You'd better write something that throws. I saw tons of (s != null) in production code and it really becomes ugly. Exception
可能重复: 直接投射vs'as'运营商? 这个操作员在哪里可以有用? 而不是写这个: Asset a = new Asset(); Stock s = a as Stock; // s is null; no exception thrown if (s != null) Console.WriteLine (s.SharesOwned); 你最好写一些抛出的东西。 我看到了很多 (s!= null) 在生产代码中,它确实变得丑陋。 例外情况在这方面更具描述性和自然性。 即使在概念上:如果不是股票,你怎么能没有资产
This question already has an answer here: Direct casting vs 'as' operator? 15 answers Casting vs using the 'as' keyword in the CLR 18 answers The first ( command.ExecuteScalar() as string; )will do a runtime attempt to convert the result of ExecuteScalar() into a string. If the resulting type is not a string , you will receive null . The as keyword also only performs refe
这个问题在这里已经有了答案: 直接投射vs'as'运营商? 15个答案 在CLR中使用“as”关键字进行投射 第一个( command.ExecuteScalar() as string; )将执行运行时尝试将ExecuteScalar()的结果转换为字符串。 如果结果类型不是string ,则会收到null 。 as关键字也只能执行引用转换,可空转换和装箱转换,所以你不能直接将它与非空值类型一起使用。 第二个( (string)command.ExecuteScalar(); )将直接进行stri
Possible Duplicate: Direct casting vs 'as' operator? Can someone explain the difference to me and which one is better to use? I know in some situations I can only use one or the other. (int)value value as int The latter is invalid. You can use value as int? if you need to "convert if possible". That's slower than if (value is int) { int x = (int) value;
可能重复: 直接投射vs'as'运营商? 有人可以向我解释这个区别,哪一个更好用? 我知道在某些情况下,我只能使用其中一种。 (int)value value as int 后者无效。 您可以使用 value as int? 如果你需要“如果可能的话转换”。 这比比较慢 if (value is int) { int x = (int) value; ... } 虽然。 如果你不确定value实际上是一个int ,那么你应该使用这个。 但是,如果你的代码是这样的,如果valu
Possible Duplicate: Direct casting vs 'as' operator? Anyone can give a comparison between as and cast? A straight cast will fail if the object being casted is not of the type requested. An as -cast will instead return null. For example: object obj = new object(); string str = (string)obj; // Throws ClassCastException However: object obj = new object(); string str = obj as strin
可能重复: 直接投射vs'as'运营商? 任何人都可以对as和cast进行比较? 如果所铸造的物体不是所要求的类型,则直接投射将失败。 as -cast将会返回null。 例如: object obj = new object(); string str = (string)obj; // Throws ClassCastException 然而: object obj = new object(); string str = obj as string; // Sets str to null 当正在铸造的对象属于您要铸造的类型时,对于任一语法,结果都是相同
This question already has an answer here: Direct casting vs 'as' operator? 15 answers Difference between is and as keyword 14 answers At least there are two possibilities for casting and one for type checking. Each has its own purpose and it depends on the situation: Hard cast var myObject = (MyType)source; You normally do that if you are absolutely sure if the given object is
这个问题在这里已经有了答案: 直接投射vs'as'运营商? 15个答案 是和关键字14答案之间的区别 至少有两种铸造可能性和一种用于类型检查。 每个人都有自己的目的,这取决于具体情况: 硬铸 var myObject = (MyType)source; 如果您完全确定给定的对象是否属于该类型,通常会这样做。 如果您订阅了事件处理程序并将发件人对象转换为正确的类型来处理它,则可以使用它。 private void OnButtonClick(object sen
Possible Duplicates: Direct casting vs 'as' operator? Casting: (NewType) vs. Object as NewType What is difference between normal typecasting and using “AS” keyword? 如果对象是错误的类型,使用as将会优雅地失败,并且结果值将为null,而普通的强制类型会抛出InvalidCastException: object x = new object(); string y = x as string; // y == null string z = (string)x; // InvalidCastException T
可能重复: 直接投射vs'as'运营商? Casting:(NewType)与Object作为NewType 正常类型转换和使用“AS”关键字有什么区别? 如果对象是错误的类型,使用as将会优雅地失败,并且结果值将为null,而普通的强制类型会抛出InvalidCastException: object x = new object(); string y = x as string; // y == null string z = (string)x; // InvalidCastException 两个操作符的用例在表面上相似,但语义上完全不同。
Possible Duplicate: Casting vs using the 'as' keyword in the CLR I recently learned about a different way to cast. Rather than using SomeClass someObject = (SomeClass) obj; one can use this syntax: SomeClass someObject = obj as SomeClass; which seems to return null if obj isn't a SomeClass, rather than throwing a class cast exception. I see that this can lead to a NullRefer
可能重复: 投射vs在CLR中使用'as'关键字 我最近了解到一种不同的投射方式。 而不是使用 SomeClass someObject = (SomeClass) obj; 你可以使用这个语法: SomeClass someObject = obj as SomeClass; 如果obj不是SomeClass,它似乎返回null,而不是抛出类转换异常。 我发现如果转换失败,可能会导致NullReferenceException,并尝试访问someObject变量。 所以我想知道这种方法的基本原理是什么? 为什么要使