该代码段的作用是什么? [英] What does this code snippet do?

查看:98
本文介绍了该代码段的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,
在学习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; }
}

推荐答案

touseef4pk写道:
touseef4pk wrote:

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.


touseef4pk写道:
touseef4pk wrote:

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.


touseef4pk写道:
touseef4pk wrote:

公共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.


Sandeep很好地解释了所有内容.

我相信您会发现以下代码更加令人困惑.

Sandeep has explained everything very nicely.

I believe that you are finding following code more confusing.

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"]);
        }


好 有语法(如果条件为true,则为1,否则为2)? (2):( 1);
如果id的值== null,则转换为int32,如果不返回0,则返回它;
在第二行中,我们说如何设置viewstate的值
如果想了解更多,请阅读属性
ok there is syntax (if Condition true doing 1 if not doing 2) ? (2):(1);
if value of id == null then converting to int32 and return it if not return 0;
in second line we say how set the value of viewstate
if want know some more ,read about property


这篇关于该代码段的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆