What's the Difference between Session.Add("key",value) and Session["key"] = value?
asp.net, c#, session
Solution
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.
Problem
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?