问题描述
亲爱的朋友,
在学习ASP.NET时,我遇到了这段代码.
我无法理解.
请简单说明一下其目标.
Dear friends,
I have come across this snippet of code while learning ASP.NET.
I am unable to comprehend it.
Please explain in simple words about its objective.
public int ID
{
get { return (ViewState["ID"] == null) ? 0 : Convert.ToInt32(ViewState["ID"]); }
set { ViewState["ID"] = value; }
}
推荐答案
get {return(ViewState ["ID"] == null)吗? 0:Convert.ToInt32(ViewState ["ID"]); }
get { return (ViewState["ID"] == null) ? 0 : Convert.ToInt32(ViewState["ID"]); }
这将检查ID值是否存储在Viewstate中.如果已保存,则从Viewstate检索并发送.如果未存储在viewstate中,则返回的ID值将为0.
This checks if ID value is stored in Viewstate or not. If it is saved then it is retrieved from Viewstate and sent across. If not stored in a viewstate then ID value returned would be 0.
set {ViewState ["ID"] =值; }
set { ViewState["ID"] = value; }
这将设置试图在Viewstate中设置的ID值.因此,将来当您尝试检索ID的值时,它将检查viewstate并发送存储在其中的值.
This sets the ID value tried to be set in a Viewstate. Thus, in future when you try to retrieve the value of ID, it checks the viewstate and send the value stored in it.
公共int ID
在同时具有get&标记的页面上公开了公共属性ID.设置在里面.因此,可以使用暴露的ID属性来检索和设置值.
A public property ID is exposed on the page that has both get & set in it. Thus values can be retrieved and set using this ID property exposed.
return (ViewState["ID"] == null) ? 0 : Convert.ToInt32(ViewState["ID"]);
在此代码中,使用了?:运算符,您可以在下面的链接中了解有关此内容的更多信息.
http://msdn.microsoft.com/en-us/library/ty67wk28(VS .80).aspx [ ^ ]
以下是相同的简化代码.
In this code ?: operator has been used you can read more about this at following link.
http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx[^]
Following is the simplified code for the same.
if (ViewState["ID"] == null)
{
return 0;
}
else
{
return Convert.ToInt32(ViewState["ID"]);
}
这篇关于该代码段的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!