通过Agathas网站总结的C#编码命名规范

来源:互联网 发布:开票系统网络连接失败 编辑:程序博客网 时间:2024/05/28 05:13

代码命名一般分为:

MyData 是帕斯卡命名;
myData是骆驼命名法它第一个单词的第一个字母小写,后面的单词首字母大写,看起来像一个骆驼;
iMyData是匈牙利命名法它的小写的i说明了它的型态,后面的和帕斯卡命名相同,指示了该变量的用途;

类的定义:帕斯卡命名

如:

public classPayment : ValueObjectBase

{

        privatereadonlyDateTime_datePaid;       //成员变量 “_” + 骆驼命名法

        privatereadonlystring_transactionId;

        privatereadonlystring_merchant;

        privatereadonlydecimal_amount;

 

        publicPayment()

        {

............

        }

 

        publicPayment(DateTime datePaid, string transactionId,  stringmerchant, decimal amount)   //参数的命名

        {

            _datePaid = datePaid;

            _transactionId = transactionId;

            _merchant = merchant;

            _amount = amount;

 

            base.ThrowExceptionIfInvalid();

        }

 

        public DateTime DatePaid                    //属性,方法,帕斯卡命名法;

        {

            get{ return _datePaid; }

        }

        public string TransactionId

        {

            get{ return _transactionId; }

        }

      

 

        protectedoverridevoidValidate()

        {

            if(string.IsNullOrEmpty(_transactionId))

                base.AddBrokenRule(PaymentBusinessRules.TransactionIdRequired);

 

            if(string.IsNullOrEmpty(_merchant))

                base.AddBrokenRule(PaymentBusinessRules.MerchantRequired);

 

            if(_amount < 0)

                base.AddBrokenRule(PaymentBusinessRules.AmountValid);

        }

    }

 

}

 

 

接口的定义:I + 帕斯卡命名

public interfaceIOrderState

{

        int Id{ get; set; }      //接口中属性命名,帕斯卡命名;

        OrderStatusStatus { get; }    //接口中方法,帕斯卡命名;

        boolCanAddProduct();

        voidSubmit(Order order);

 

}   

 

局部变量,方法参数:一个单词的话就小写,多个单词采用骆驼命名法

public void Page_Load(object sender, System.EventArgse)     //

{

            // Changethe current path so that the Routing handler can correctly interpret

            // therequest, then restore the original path so that the OutputCache module

            // cancorrectly process the response (if caching is enabled).

 

            stringoriginalPath = Request.Path;   //originalPath为局部变量,采用骆驼命名法

            HttpContext.Current.RewritePath(Request.ApplicationPath,false);

            IHttpHandlerhttpHandler =new MvcHttpHandler();    //httpHandler为局部变量,采用骆驼命名法;

            httpHandler.ProcessRequest(HttpContext.Current);

            HttpContext.Current.RewritePath(originalPath,false);

}