为什么允许空对象上的扩展方法?

发布于 2024-10-27 17:49:06 字数 69 浏览 5 评论 0原文

允许在空对象上调用扩展方法有什么意义? 这使我不必要地检查扩展方法中的空对象。 AFAIK,我无法理解这一点? 请解释一下。

what is the point of allowing invocation of extension methods on null objects?
this is making me unnecessarily check for a null object in the extension method.
AFAIK,i can't understand this?
Please explain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ぇ气 2024-11-03 17:49:06

扩展方法是 C# 语言的语法糖,它们被编译为 ILCode 中的普通静态方法调用。静态方法在编译时不知道任何有关参数的信息。

Extension methods are syntactic sugar of the C# language, they get compiled to normal static method calls in ILCode. A static method doesn't know anything about the parameters at compile time.

酒绊 2024-11-03 17:49:06

简而言之,为什么不呢?

如果您在扩展中调用的第一个方法也会抛出正确的错误,有时您可以跳过测试。

您本质上要求代码有所不同,以便:

  1. 在空对象上合理的使用将被禁止。
  2. 不需要空检查(因为它隐含在其他内容中)的使用会获得您希望自动发生的不必要检查的开销。

这似乎对其他用途造成了很大的影响,只是为了节省一行:

if(arg == null)throw new ArgumentNullException();

Simply put, why not?

You can sometimes skip the test if the first method you call within the extension would also throw the correct error.

You're essentially asking for the code to be different so that:

  1. Uses which are reasonable on a null object, become disallowed.
  2. Uses which don't need a null check (because it's implicit in something else) get the overhead of the needless check you want to happen automatically.

This seems a lot of an imposition on other uses just to save the one line of:

if(arg == null)throw new ArgumentNullException();
┈┾☆殇 2024-11-03 17:49:06

扩展方法只是语法糖。实际上它们是另一个类上的静态方法,所以既然你可以写

IEnumerable<int> foo = null;
Enumerable.Count(foo);

你也可以写

IEnumerable<int> foo = null;
foo.Count();

Extension methods are just syntactic sugar. In reality they are static methods on another class, so since you can write

IEnumerable<int> foo = null;
Enumerable.Count(foo);

You can also write

IEnumerable<int> foo = null;
foo.Count();
盗琴音 2024-11-03 17:49:06

有时,允许在 null 对象上调用扩展方法可以简化代码,因为您可以将 null 检查移至方法中而不是调用站点。例如,您可能有一个返回 List 的扩展方法,但如果在 null 对象上调用,则会返回空的 List

Sometimes, allowing the extension method to be called on a null object simplifies your code by allowing you to move the null check into the method instead at the call site. For example, you may have an extension method that returns a List<T>, but if called on a null object, returns an empty List<T>.

初懵 2024-11-03 17:49:06
  1. 扩展方法转换为静态方法调用,因此代码仍然需要检查空参数,因为没有办法避免在没有扩展方法语法糖的情况下正常调用静态方法。
  2. 添加诸如检查之类的内容,然后添加 NullArgumentException 可能会花费执行时间,并且用户可能想要断言或使用其他内容。
  3. 这将使替换的解释或自动执行变得更加复杂,因为用相应的静态方法调用简单替换扩展方法将改变代码的行为。
  4. 在某些合理的情况下,您希望允许空参数(例如,从一个对象模型到另一个对象模型的转换,其中一种类型的空对象转换为第二种类型的空对象)
  1. Extension methods are transformed to static method invocations so the code will still need to check for null arguments as there is no way to avoid the static method to be called normally without the extension method syntactic sugar.
  2. Adding something like a check followed by a NullArgumentException could take execution time and the user may want to assert instead or use something else.
  3. It would make the replacement more complex to explain or do automatically as the simple replacement of the extension method with the corresponding static method call will change the behavior of the code.
  4. There are legitimate case where you want to allow null arguments (for exemple conversions from an object model to another where a null object of one type is converted to a null object of the second type)
酷遇一生 2024-11-03 17:49:06

扩展方法只是静态方法:

List<int> x = null;
x.Count()

相当于:

List<int> x = null;
System.Linq.EnumerableExtensions.Count(x); 
//EnumerableExtensions may not be the class, but you get the idea

Extension methods are just static methods:

List<int> x = null;
x.Count()

Is equivalent to:

List<int> x = null;
System.Linq.EnumerableExtensions.Count(x); 
//EnumerableExtensions may not be the class, but you get the idea
深爱成瘾 2024-11-03 17:49:06

另一个美丽的例子,否则是不可能的:

public static bool IsNullOrEmpty(this string value)
{
    return string.IsNullOrEmpty(value);
}

所以你可以使用

string s = null;
if (s.IsNullOrEmpty()) // no null reference error!
    ...

而不是

string s = null;
if (string.IsNullOrEmpty(s))
    ....

Another beautiful example that wouldn't be possible otherwise:

public static bool IsNullOrEmpty(this string value)
{
    return string.IsNullOrEmpty(value);
}

So you can use

string s = null;
if (s.IsNullOrEmpty()) // no null reference error!
    ...

Instead of

string s = null;
if (string.IsNullOrEmpty(s))
    ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文