惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
C
Check Point Blog
Vercel News
Vercel News
Y
Y Combinator Blog
G
Google Developers Blog
P
Proofpoint News Feed
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
L
LangChain Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园_首页
B
Blog RSS Feed
The Cloudflare Blog
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog

博客园 - 谢绝围观

LeetCode 910. Smallest Range II EAC 抓取CD为AAC文件 Binary Indexed Tree (Fenwick Tree) Lint Code 1365. Minimum Cycle Section LintCode 896. Prime Product 简明题解 UVa 1471 - Defense Lines Windows下安装配置MinGW GCC调试环境 详解LeetCode 137. Single Number II LeetCode 309. Best Time to Buy and Sell Stock with Cooldown LeetCode 84. Largest Rectangle in Histogram 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项 LeetCode 312. Burst Balloons LeetCode 287. Find the Duplicate Number LeetCode 10. Regular Expression Matching LeetCode 117. Populating Next Right Pointers in Each Node II 在Windows Azure VM下搭建SSTP VPN - 谢绝围观 利用.NET Code Contracts实现运行时验证 Log4net 配置实例 解决Windows Server 2012 下利用RRAS创建VPN断线的问题 - 谢绝围观
慎用静态类static class
谢绝围观 · 2015-02-21 · via 博客园 - 谢绝围观

偶然翻到一篇有趣的帖子:

class - When to Use Static Classes in C# - Stack Overflow
http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp

觉得说的挺有道理,很多项目都会有一个静态的utility类,但如果预期系统会变得非常复杂,且现有方法未来会出现多种实现,请谨慎使用这样的静态类。主要原因:

  1. 无法使用多态,如果需要对某一方法的实现做一小小改动,会很难看。
  2. 参数爆炸。随着方法在不同的场合被使用,需要的参数种类会越来越多,出现越来越多的重载。虽然可以使用optional parameter,但如果增加一个新的必选参数,则这个参数必须跟在已有必选参数后面,从而改变已有调用的参数顺序,造成混乱。如果是非静态类可以考虑把可选参数都做成构造函数的参数。
  3. 无法使用接口,造成编写单元测试困难。