扩展方法与静态实用程序类

发布于 2024-10-11 08:56:59 字数 1431 浏览 4 评论 0原文

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

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

发布评论

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

评论(3

墨落画卷 2024-10-18 08:57:00

归根结底,这两种方法都使用静态方法。 之间的唯一区别

string foo = "bob";
StringUtils.DoSomething(foo);

string foo = "bob";
foo.DoSomething();

是语法糖。这归结为个人喜好和编码标准。有时,方法名称可能具有足够的描述性,以至于不需要看到静态类名称。其他时候,包含类名更有意义。

最后,扩展方法也可以作为静态方法调用!

string foo = "bob";
StringExtensions.DoSomething(foo);

上面使用与第二个示例相同的代码,但调用方式不同。记住最后一点,您实际上可以创建静态实用程序类作为扩展方法,然后根据需要调用它们。

At the end of the day, both approaches use static methods. The only difference between

string foo = "bob";
StringUtils.DoSomething(foo);

and

string foo = "bob";
foo.DoSomething();

is syntactic sugar. It boils down to personal preference and coding standards. Sometimes the method name can be descriptive enough to not warrant seeing the static class name. Other times it makes more sense to include the class name.

Finally, extension methods can be invoked as static methods too!

string foo = "bob";
StringExtensions.DoSomething(foo);

The above uses the same code as in the second example, but is invoked differently. With this last tidbit in mind, you could really create static utility classes as extension methods and then invoke them however you wish.

一城柳絮吹成雪 2024-10-18 08:57:00

我个人喜欢扩展方法提供的可读性和链式调用(隐式提供可读性)。

 1) Readability:  
     bool empty = String.IsNullOrEmpty (myString)
     //in comparison to
     bool empty = myString.IsNullOrEmpty ();

 2) Chain calls:  
    var query = Enumerable.Range(0, 10)
                         .Where(x => x % 2 == 0)
                         .Reverse();
    //instead of
    var query = Enumerable.Reverse(Enumerable.Where(Enumerable.Range(0, 10), x => x % 2 == 0));

缺点是如果您不小心这样做,您的扩展方法可能会被实例成员覆盖。我个人不喜欢这个。如果它发生在同一个程序集中,至少编译器应该尖叫。

I personally like readabilty and chain calls(implicitly provides readability) that is provided by Extension methods.

 1) Readability:  
     bool empty = String.IsNullOrEmpty (myString)
     //in comparison to
     bool empty = myString.IsNullOrEmpty ();

 2) Chain calls:  
    var query = Enumerable.Range(0, 10)
                         .Where(x => x % 2 == 0)
                         .Reverse();
    //instead of
    var query = Enumerable.Reverse(Enumerable.Where(Enumerable.Range(0, 10), x => x % 2 == 0));

Con is your extension method can be overriden by instance member if you accidentally do it. Personally I dont like this. At least compiler should have screamed, if it is happening with in the same assembly.

空城仅有旧梦在 2024-10-18 08:56:59

我想说的一个优点是它模糊了框架中的内容和框架外的内容之间的界限:您可以像框架代码一样自然地使用自己的代码,在框架类型上进行操作。

当然,扩展方法不应该任意使用 - 并不是所有静态方法都应该成为扩展方法。

我尝试将其视为该方法是否在其第一个参数上进行逻辑操作。如果您能够将其作为实例方法包含在内,那么作为实例方法是否有意义?

您可能没有意识到的一个“缺点”:如果扩展类型稍后添加适用于相同参数的同名实例方法,则您的调用代码将在您下次重新编译时透明地开始调用该实例方法。例如,Stream 获得< .NET 4 中的 code>CopyTo...我之前编写了一个 CopyTo 扩展方法,但该方法不会被调用。没有任何警告表明这种情况正在发生,因此您必须保持警惕。

需要注意的是:扩展方法出现的时间还不够长,最佳实践还没有真正建立起来。你应该仔细权衡所有的观点(甚至——或者也许尤其是——我自己的观点)。

I would say that a pro is that it blurs the lines between what is in the framework and what isn't: you can use your own code as naturally as framework code, operating on framework types.

Extension methods shouldn't be used arbitrarily, of course - it's not like all static methods should become extension methods.

I try to think of it as whether the method is logically operating "on" its first parameter. Would it make sense as an instance method, if you were able to include it as an instance method?

A "con" which you may not be aware of: if the extended type later adds an instance method of the same name which is applicable with the same parameters, your calling code will transparently start calling that instance method next time you recompile. For example, Stream gained CopyTo in .NET 4... I'd previously written a CopyTo extension method, which then wouldn't be called. There's no warning that this is occurring, so you have to be on your guard.

One caveat: extension methods haven't been around for long enough for best practices to really become established. You should weigh up all opinions (even - or perhaps especially - my own ones) carefully.

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