C#动作/代表风格问题 [英] C# Action/Delegate Style Question

查看:131
本文介绍了C#动作/代表风格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么才算好样式的事件定义:

What is considered better style for an event definition:

public event Action<object, double> OnNumberChanged;

public delegate void DNumberChanged(object sender, double number);
public event DNumberChanged OnNumberChanged;

第一时间不打字,但委托人给名称的参数。当我键入​​此,我觉得2号是赢家,但我可能是错的。

The first takes less typing, but the delegate one gives names to the parameters. As I type this, I think number 2 is the winner, but I could be wrong.

编辑:不同的(第三个)的做法是赢家。阅读下文。

A different (third) approach is the winner. Read below.

推荐答案

无论是1或2。第三个选项是赢家

Neither 1 or 2. A third option is the winner

public event EventHandler<NumberChangedEventArgs> NumberChanged;

你打破了一些风格指南开发在C#中,如使用类型事件参数不扩展EventArgs的。

You're breaking a number of style guidelines for developing in C#, such as using a type for event args that doesn't extend EventArgs.

是的,你能做到这样,因为编译器不关心。然而,人们阅读你的code会做跆拳道。

Yes, you can do it this way, as the compiler doesn't care. However, people reading your code will do a WTF.

这篇关于C#动作/代表风格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆