class Person
{
/// <summary>
/// 公有字段、属性 首字母大写
/// </summary>
public string FirstName;
public string LastName;
public string Name
{
get { return string.Format("{0}{1}", FirstName, LastName); }
}
public string GetName()
{
return Name;
}
/// <summary>
/// 如果属性对应一个类型,则直接用类型命名属性名
/// </summary>
public Company Company { get; set; }
/// <summary>
/// 多个类型的属性命名
/// </summary>
public Company SecondCompany { get; set; }
}
class Company
{
/// <summary>
/// 私有字段的命名
/// </summary>
private string firstName;
private string lastName;
/// <summary>
/// 静态变量 s_
/// </summary>
private static int s_price;
/// <summary>
/// 实例变量 m_
/// </summary>
private int m_price;//类的成员变量(私有)(在有静态变量时,添加m_加以区分,如果没有则直接如firstName这种方式即可)
/// <summary>
/// const变量使用名词加下划线表示
/// </summary>
private const int BASED_PRICE = 1000;
/// <summary>
/// 使用肯定语气
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 方法的参数及局部变量首字母小写
/// </summary>
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
{
/// <summary>
/// 委托按照作用分为以Delegate、Callback结尾
/// </summary>
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();
/////<summary>
/////</summary>
//[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
{
/// <summary>
/// 事件的使用
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//注册事件
this.AfterDoSomething1 += Form1_AfterDoSomething1;
}
/// <summary>
/// 执行事件
/// </summary>
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();
///<summary>
///</summary>
[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,即仅在界面层使用