ViewState .Add(.... or ViewState["XXXXX"] is efficient ?

2.9k Views Asked by At

I have a reasonable dout,

ViewState .Add(.... or ViewState["XXXXX"] is efficient ?

Eg.

1).

ViewState.Add("Example1", value );

2).

ViewState["Example1"]=value;

Both gives the same output, 1st one add the Value to the NameValueCollection if that Key does not exists.

2nd also does the same. Is there any difference in both. ?

1

There are 1 best solutions below

0
On

There is no difference as far as performance. (Any trivial check is irrelevant to all other code execution.)

Add will concatenate with existing values, however, as per the documentation.

With this in mind, choose the most appropriate construct. In my mind, that is ViewState[k] = v in most circumstances -- unless of course the concatenation is desired.

Happy coding.