class Person { ////// 公有字段、属性 首字母大写 /// public string FirstName; public string LastName; public string Name { get { return string.Format("{0}{1}", FirstName, LastName); } } public string GetName() { return Name; } ////// 如果属性对应一个类型,则直接用类型命名属性名 /// public Company Company { get; set; } ////// 多个类型的属性命名 /// public Company SecondCompany { get; set; } } class Company { ////// 私有字段的命名 /// private string firstName; private string lastName; ////// 静态变量 s_ /// private static int s_price; ////// 实例变量 m_ /// private int m_price;//类的成员变量(私有)(在有静态变量时,添加m_加以区分,如果没有则直接如firstName这种方式即可) ////// const变量使用名词加下划线表示 /// private const int BASED_PRICE = 1000; ////// 使用肯定语气 /// public bool IsEnable { get; set; } ////// 方法的参数及局部变量首字母小写 /// private int doSomething(int numberA, int numberB)//(本人习惯用 int argNumberA 带arg开头的作为方法的参数传入) { //局部变量不要求使用 m_ 开头的前缀,以区分全局变量 int localNumber = 1;//局部变量 myName int _localNumber2 = 2;//私有成员变量(也可以以下划线开头+首字母小写) return numberA + numberB + localNumber + _localNumber2; } } class DelegateNaming { ////// 委托按照作用分为以Delegate、Callback结尾 /// public delegate void HttpContinueDelegate(int argStatusCode, System.Net.WebHeaderCollection argHttpHeaders); public delegate bool ValidateValueCallback(object argValue); public delegate void AsyncCallback(IAsyncResult ar); //public delegate void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e); //事件、委托变量应该用动词、形容词短语命名 } class EventNaming { 触发事件 //OnDoSomething1(sender); 注册事件 //this.AfterDoSomething1 += Form1_AfterDoSomething1; //声明事件: //private static readonly object EventDoSomething1 = new object(); // //[Category("行为"), Description("后处理")] //public event DelegateTest1 AfterDoSomething1 //{ // add // { // Events.AddHandler(EventDoSomething1, value); // } // remove // { // Events.RemoveHandler(EventDoSomething1, value); // } //} //public virtual void OnDoSomething1(object sender) //{ // var objDoSomething1 = (DelegateTest1)Events[EventDoSomething1]; // if (objDoSomething1 != null) // { // objDoSomething1(this); // } //} //public delegate void DelegateTest1(object sender); }
有关事件的用法示例:
using System; using System.ComponentModel; using System.Windows.Forms; namespace WinFormsApp_Naming { ////// 事件的使用 /// public partial class Form1 : Form { public Form1() { InitializeComponent(); //注册事件 this.AfterDoSomething1 += Form1_AfterDoSomething1; } ////// 执行事件 /// void Form1_AfterDoSomething1(object sender) { MessageBox.Show("DoSomething.."); } private void button1_Click(object sender, EventArgs e) { //触发事件 OnDoSomething1(sender); } private static readonly object EventDoSomething1 = new object(); ////// [Category("行为"), Description("后处理")] public event DelegateTest1 AfterDoSomething1 { add { Events.AddHandler(EventDoSomething1, value); } remove { Events.RemoveHandler(EventDoSomething1, value); } } public virtual void OnDoSomething1(object sender) { var objDoSomething1 = (DelegateTest1)Events[EventDoSomething1]; if (objDoSomething1 != null) { objDoSomething1(this); } } public delegate void DelegateTest1(object sender); } }
其中 Events继承至IComponent,即仅在界面层使用