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

推荐订阅源

B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
S
Schneier on Security
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
Security Latest
Security Latest
Jina AI
Jina AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recorded Future
Recorded Future
T
Tor Project blog
有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
C
Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
IT之家
IT之家
T
Threatpost
Cyberwarzone
Cyberwarzone
O
OpenAI News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - dragonpig

Html 5 Canvas绘制分形图Mandelbrot .net中反射、emit、expression和dynamic的性能比较 const string 和 static readonly string的区别 SqlServer: Top N per Group 微软的BinarySearch 通过CTE实现Split CSV 通过SQL CTE计算Fibonacci 当json.js遇见dynamic.net烂尾篇 .NET线程安全泛型Singleton 跨域访问Cookie WCF JSON和AspnetCompatibility的配置 Windows安装Memcached node.js初体验 教你如何制作Silverlight Visual Tree Inspector 一道非常有趣的概率题 教你30秒打造强类型ASP.NET数据绑定 当json.js遇见dynamic.net [0] 用Silverlight做雷达图 随机排列算法
C#运算符重载不是没有用武之地
dragonpig · 2011-01-17 · via 博客园 - dragonpig

当年Java批判C++过于臃肿和迷宫般的语法特性,摒弃了一大堆东西包括运算符重载。ThinkingInJava一书中好像有对此事的评价,从负面讲,运算符重载的滥用容易导致语义的混乱,例如apple+person就很难推测出其背后的意图。但是没有它,像复数类Complex的四则运算就显得十分繁琐。运算符重载到底是不是鸡肋呢?个人觉得运算符重载还是有很多用武之地的,尤其适用于和数值相关的场景。下面介绍一个我遇到的案例。

现在定义了一批0到1的数值:

代码

double _discount0;
double Discount0 { get { return _discount0; } set { ValidateRatio(value); _discount0 = value; } }double _discount1;
double Discount1 { get { return _discount1; } set { ValidateRatio(value); _discount1 = value; } }double _discount2;
double Discount2 { get { return _discount2; } set { ValidateRatio(value); _discount2 = value; } }void ValidateRatio(double v)
{
if (v < 0 || v > 1)
throw new ArgumentOutOfRangeException();
}

Discount(折扣)显然是0到1之间的某个值否则抛出异常。现在比较一下使用运算符重载之后的效果。

代码

//[0,1]的数值类型
public struct RatioValue
{
double _value;
public RatioValue(double ratio)
{
if (ratio < 0 || ratio > 1)
throw new ArgumentOutOfRangeException();
_value
= ratio;
}
public double Value { get { return _value; } }
public static implicit operator double(RatioValue ratio) { return ratio.Value; }
public static implicit operator RatioValue(double value) { return new RatioValue(value); }
}
//简化了的Discount
RatioValue Discount0 { get; set; }
RatioValue Discount1 {
get; set; }
RatioValue Discount2 {
get; set; }

这里用到了隐式类型转换也是运算符重载的一种。显然最后Discount的定义大大简化了。并且使用起来和double类型没有区别,一切转换都有定义的RatioValue搞定,包括值域检测。

for (int i = 0; i < 10; i++)
{
Discount0
= new Random().NextDouble();
System.Threading.Thread.Sleep(
100);
Console.WriteLine((
int)(Discount0 * 10));
}
Console.ReadLine();

总之语法的多样性有助于降低语法噪音,缓解人的阅读压力,至少庆幸C#没有像java因噎废食而将运算符重载拒之门外。