方法重载——设计好还是坏?

发布于 2024-09-16 08:45:15 字数 1431 浏览 7 评论 0原文

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

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

发布评论

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

评论(4

很酷又爱笑 2024-09-23 08:45:15

重载对性能没有影响;它由编译器在编译时解决。

至于设计指南,请参阅设计指南:

http://msdn.microsoft.com /en-us/library/ms229029.aspx

Overloading has no impact on performance; it's resolved by the compiler at compile-time.

As for design guidance, see the design guidelines:

http://msdn.microsoft.com/en-us/library/ms229029.aspx

沐歌 2024-09-23 08:45:15

如果您使用的是 C# 4.0,您可以节省一些工作并使用 可选参数

If you're using C# 4.0 you can save your fingers some work and use optional parameters.

孤独难免 2024-09-23 08:45:15
  1. 性能影响,
    据我所知,这就像定义一个新方法。性能影响是硬盘上的空间。
  2. 建议重载方法,
    确实,它提供了便利,
  3. 但有什么限制,
    尽可能多的硬盘空间。
  1. Performance impact,
    as far as I know, it's like defining a new method. The performance impact is space on your harddrive.
  2. Advisable to overload methods,
    Definitely, it's provides convenience
  3. What is the limit,
    As much Harddrive space as you have.
执笔绘流年 2024-09-23 08:45:15

远离性能,如果集中使用重载肯定会导致混乱,一旦有人开始维护代码,我更喜欢这样做:

Person GetPersonByID( int id );
Person GetPersonByName( String name );

对此:

Person GetPerson( int id );
Person GetPerson( String name );

Away from performance, overloading if used intensively will for sure lead to confusion once somebody starts to maintain the code, I prefer doing this:

Person GetPersonByID( int id );
Person GetPersonByName( String name );

on this:

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