开发者

Confusion regarding C# property

开发者 https://www.devze.com 2023-01-29 00:40 出处:网络
i got a sample code from a site. one thing i just do not understand regarding how value is added in ViewState automatically.

i got a sample code from a site. one thing i just do not understand regarding how value is added in ViewState automatically.

the code as follows

private Dictionary<Guid, string> Names
{
    get { return (Dictionary<Guid, string>开发者_StackOverflow中文版)ViewState["Names"]; }
    set { ViewState["Names"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // to keep this sample really simple, we will populate some
        // sample data in a Dictionary and store in the viewstate
        var names = new Dictionary<Guid, string>
        {
            { Guid.NewGuid(), "John" },
            { Guid.NewGuid(), "Smith" },
            { Guid.NewGuid(), "Arther" },
            { Guid.NewGuid(), "Hari" }
        };
        //store the list in the viewstate.
        ViewState.Add("Names", names);

        //init grid           
    }
}

protected void btnAddSave_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Names.Add(Guid.NewGuid(), txtNewName.Text);            
    }
}

in page load event i understand that few values has been added in Dictionary collection and after that the whole collection is added in ViewState named "Names".

in btnAddSave_Click event the code Names.Add(Guid.NewGuid(), txtNewName.Text); adds name again in collection.

but one things i just do not understand that how new value is added in viewstate.

if someone look private Dictionary<Guid, string> Names property there is no any setter so how automatically new value is added in ViewState . i hope my question is clear. in simple word i just want to know there no any setter but then how new values has been added in ViewState when the code goes like Names.Add(Guid.NewGuid(), txtNewName.Text);.

automatically how new values has been added in ViewState . because new values is added in dictionary collection but there is no code available where all dictionary values added in ViewState after adding any value to the dictionary collection.i run the code and it works fine.so this code become very ambiguous for me. so please help me to understand how new values is added in ViewState when any new values is added in dictionary collection. Thanks!


internal implementation of ViewState is Dictionary thats why it is possible

when you access

  Names.Add(Guid.NewGuid(), txtNewName.Text);            

following steps involved

1- Get the backing store of property represented by Names which is in your case is ViewState["Names"].

see the getter part of property Names

private Dictionary<Guid, string> Names
{   
 **get { 
         return (Dictionary<Guid, string>)ViewState["Names"];
     }**   
 set { ViewState["Names"] = value; }}

Edit,For Thomas comments , i am adding my response

forget about the setter , its only getter , now when you dereference Names readonly property , the actual type behind dereferencing is statebag and statebag implements IDictionary ,tha's why you can cast ViewState property to Dictionary , now When yo call Add method of Dictionary , it will be stored in the StateBag which is known s Control's ViewState. so ViewState is a just a Control's property , it is not a type


What's added in the ViewState is not the contents of the dictionary, but a reference to the dictionary. You can still change what the dictionary contains after that.

When the execution of the page is complete, and it's about to be sent to the browser, the contents of the dictionary will be serialised. Until that point you can change the dictionary, and the changes will end up in the ViewState.


The value at Names.Add is not added (directly) to the view state. It's added to a collection that's already in viewstate. With the result that when the collection is serialized the value added to Names will be serialized. This is a matter of understanding reference types (which a Dictionary is)

0

精彩评论

暂无评论...
验证码 换一张
取 消