What's the Difference between Session.Add("key",value) and Session["key"] = value?

Go To StackoverFlow.com

24

Can somebody please explain to me the difference between:

Session.Add("name",txtName.text); and Session["name"] = txtName.text;

It was an interview question and I answered that both store data in key = "Value" format like Dictionary class in C#.

Am I right, or is there any difference?

2012-04-04 19:29
by Chandan Kumar
I'm sure there's some nice msdn documentation regarding this. Perhaps you could research it yourself and be better prepared for the next interview - jpm 2012-04-04 19:33
That is a terrible interview question - Eric Lippert 2012-04-04 20:29
@EricLippert, I completely agree. Process of hiring a developer is highly broken at most organizations - SolutionYogi 2012-04-04 20:38
@EricLippert If nothing else, it throws up flags for inexperienced developers who may not know the two are identical. IMO most web devs should know this. But in general yes it's a weak question - KP. 2012-04-05 12:26
@EricLippert Off-topic and years later... I was once asked to convert a decimal number into hexidecimal during an interview - Yuck 2016-12-29 18:09


24

Looking at the code for HttpSessionState shows us that they are in fact the same.

public sealed class HttpSessionState : ICollection, IEnumerable
{
    private IHttpSessionState _container;
...
    public void Add(string name, object value)
    {
        this._container[name] = value;
    }

    public object this[string name]
    {
        get
        {
            return this._container[name];
        }
        set
        {
            this._container[name] = value;
        }
    }
...
}

As for them both

Storing data in key = "Value" format like Dictionary class in C#.

They actually store the result in an IHttpSessionState object.

2012-04-04 20:04
by Sean Kearney
This is a great answer because it shows in code exactly why the two functions are equivalent which helps to internalize the concept. I like the note in another Comment by @matthew about Dictionary, because I've been thinking all along that we were using a Dictionary or other hash table in session state, and frankly I've always done the silly dance to check to see if a value already existed before either adding or modifying based on that - TonyG 2013-12-24 22:06
That is not the same because: When the key exist the value is update but if the key doesn't exist they create with the value, if like *SaveOrUpdate* method - Adhemar 2016-07-04 15:21


10

The two code snippets you posted are one and the same in functionality. Both update (or create if it doesn't exist) a certain Session object defined by the key.

Session.Add("name",txtName.text);

is the same as:

Session["name"] = txtName.text;

The first is method-based, where the second is string indexer-based.

Both overwrite the previous value held by the key.

2012-04-04 19:43
by KP.
In contrast to a Dictionary, if you try and Add to a dictionary twice with the same key, it will throw an exception. The indexer of a Dictionary works similarly to the Session object (it will either add or update, and will not throw an exception) - Matthew 2012-04-04 20:02
@Matthew great additional note.. - KP. 2012-04-05 12:23
@Mathew - Thanks for the extra not - Chandan Kumar 2013-07-09 05:24
Ads