C# 基本知识小结

1.??可能是一个被遗忘的运算符,很少看到有人用它,它的用法很简单却很实用:
variable ?? defaultValue
相当于
variable == null ? defaultValue : variable

意思是为null 的时候等于什么值
有了它,一行便能搞定Lazy Evaluation了:

//定义一个全局的方法 

//获取服务单ID
private string getServicesId
{
    get
   {
         return (string)Request.Params["servicesId"] ?? ""; //相当于三元运算符 

        //相当于return (string)Request.Params["servicesId"] == "" ? "" : Request.Params["servicesId"]; 
   }
set
   {
         Request.Params["servicesId"] = value;
    }
}

 

注意: 

Request.Params["ID"];//建议使用 因为是从特定页面传过来的值
Request.Form["ID"];//可以从本页面传值过来  

 

2.string result = default(string); 这样定义很少,一般多事string result = default(string);

default(string):表示string的默认值为null

result = ((CrmDateTimeProperty)item).Value.IsNull ? default(string) : ((CrmDateTimeProperty)item).Value.Value;

 

原文地址:https://www.cnblogs.com/allenhua/p/2987865.html