是否有意义使用" AS"而不是就算没有空检查强制转换? [英] Does it make sense to use "as" instead of a cast even if there is no null check?

查看:102
本文介绍了是否有意义使用" AS"而不是就算没有空检查强制转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发博客,网络code例子和(最近)甚至是一本书,我一直跌跌撞撞约code是这样的:

In development blogs, online code examples and (recently) even a book, I keep stumbling about code like this:

var y = x as T;
y.SomeMethod();

,或者更糟:

(x as T).SomeMethod();

这是没有道理给我。如果你确信 X 的类型 T ,你应该使用直接转换: (T)X 。如果你不知道,你可以使用,但需要执行一些操作前检查。所有上述code的作用是把一个(有用) InvalidCastException的成(没用)的NullReferenceException

That doesn't make sense to me. If you are sure that x is of type T, you should use a direct cast: (T)x. If you are not sure, you can use as but need to check for null before performing some operation. All that the above code does is to turn a (useful) InvalidCastException into a (useless) NullReferenceException.

我是唯一一个谁认为这在公然滥用关键字?还是我错过了什么明显的,上面的图案实际上是有意义的?

Am I the only one who thinks that this a blatant abuse of the as keyword? Or did I miss something obvious and the above pattern actually makes sense?

推荐答案

您的理解是真实的。这听起来就像试图微型优化给我。当您确定类型的,你应该使用正常的演员。除了产生一个更明智的例外,它也失败快。如果你错了你对类型的假设,你的程序将立即失败,你就可以看到失败的原因立即而不是等待的NullReferenceException ArgumentNullException ,甚至在将来某个时候一个逻辑错误。一般来说,前pression这不是后跟一个检查的地方是一个code气味。

Your understanding is true. That sounds like trying to micro-optimize to me. You should use a normal cast when you are sure of the type. Besides generating a more sensible exception, it also fails fast. If you're wrong about your assumption about the type, your program will fail immediately and you'll be able to see the cause of failure immediately rather than waiting for a NullReferenceException or ArgumentNullException or even a logical error sometime in the future. In general, an as expression that's not followed by a null check somewhere is a code smell.

在另一方面,如果你不能确定的演员,并期望它失败了,你应该使用,而不是一个正常的投包裹着的try-catch 块。此外,建议在一个类型检查,随后通过流延用。相反的:

On the other hand, if you are not sure about the cast and expect it to fail, you should use as instead of a normal cast wrapped with a try-catch block. Moreover, use of as is recommended over a type check followed by a cast. Instead of:

if (x is SomeType)
   ((SomeType)x).SomeMethod();

,它产生一个<一href=\"http://msdn.microsoft.com/en-us/library/system.reflection.emit.op$c$cs.isinst.aspx\"><$c$c>isinst指令为关键字和<一个href=\"http://msdn.microsoft.com/en-us/library/system.reflection.emit.op$c$cs.castclass.aspx\"><$c$c>castclass指令了解投(有效执行转换两次),你应该使用:

which generates an isinst instruction for the is keyword, and a castclass instruction for the cast (effectively performing the cast twice), you should use:

var v = x as SomeType;
if (v != null)
    v.SomeMethod();

这只是生成一个 isinst 指令。前一种方法在多线程应用程序的潜在缺陷为竞争条件可能导致变量设置为检查后,将其类型更改成功,并在投行失败。后一种方法是不容易出现这种错误

This only generates an isinst instruction. The former method has a potential flaw in multithreaded applications as a race condition might cause the variable to change its type after the is check succeeded and fail at the cast line. The latter method is not prone to this error.

<子>下面的解决方案是的不建议的用于生产code使用。如果你真的在C#恨这样的基本结构,你可能会考虑换用VB或其他语言。

The following solution is not recommended for use in production code. If you really hate such a fundamental construct in C#, you might consider switching to VB or some other language.

在情况下,一个拼命讨厌剧组的语法,他/她可以写一个扩展方法来模拟转换:

In case one desperately hates the cast syntax, he/she can write an extension method to mimic the cast:

public static T To<T>(this object o) { // Name it as you like: As, Cast, To, ...
    return (T)o;
}

和使用一个整洁的语法[?]

and use a neat[?] syntax:

obj.To<SomeType>().SomeMethod()

这篇关于是否有意义使用&QUOT; AS&QUOT;而不是就算没有空检查强制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆