静态和扩展方法策略

发布于 2024-12-11 16:30:21 字数 224 浏览 0 评论 0原文

我正在为我的项目开发一个个人助手库。 我的库有很多辅助类和扩展方法,但我无法决定该辅助类的命名空间和方法是静态还是扩展?

在什么情况下方法应该是静态的? StringUtility.ToBase64("xxxx") 在什么情况下,方法应该是扩展静态的? (我知道扩展必须是静态的) "xxxx".ToBase64()

我如何轻松确定这种行为的策略?

I'm working on a personal helper library for my projects.
I have a lot of helper class and extension methods for my library but I can not decide this helper class namespaces and method to static or extension?

In which case the methods should be static? StringUtility.ToBase64("xxxx")
In which case the methods should be extension-static? (I know extensions must be static) "xxxx".ToBase64()

How can i determine strategy easily for this behaviours?

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

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

发布评论

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

评论(2

锦上情书 2024-12-18 16:30:21

我的决定如下:“此方法是否对其扩展的对象执行某些操作,返回某些内容并且不会更改扩展的对象”,然后我使用扩展方法。当这些辅助函数执行其他处理对象类型的操作时,我使用静态类方法。我希望这对你有一点帮助。

My decision is following: "Is this method doing something with the object it is extending, returning somethind and doesn't change the extended object" then I use extended methods. I use static class methods when those helper functions do some other which deals with the type of object. I hope that helps you a bit.

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