masoud ramezani
masoud ramezani

Reputation: 22920

which one is better for storing data in viewstate?

I create a new asp.net custom control. This control get its data via a list of string. Now I want to know what is the better solution for me? storing List<String> or string[] ?

Upvotes: 1

Views: 980

Answers (4)

Rawling
Rawling

Reputation: 50104

Based on this answer to another question, you'd be better off storing it as an array, as the list itself adds some storage overhead.

Whether or not you create a list out of the array after you retrieve it from the viewstate is up to you.

To those saying "use a list, it gives you more niceties than an array" - once the collection is in the ViewState, it doesn't matter whether it has nice Add methods or not.

public List<string> MyItemsFromViewState
{
    get { return new List<string>((string[])ViewState["MyItems"]); }
    set { ViewState["MyItems"] = value.ToArray(); }
}

Upvotes: 1

Samir Adel
Samir Adel

Reputation: 2499

list of string would be better for you to put on the viewsate and get from view state because list of string will provide you with more flexibility in work

Upvotes: 0

Icarus
Icarus

Reputation: 63956

It makes little difference as far as ViewState is concerned. The size of both should be almost the same. I would prefer List<string> for the extra niceties provided by List<T>

Upvotes: 1

Mathew Thompson
Mathew Thompson

Reputation: 56429

The general practice is use string[] when you are working with static arrays, basically when you don't need to add /remove elements (only access via index).

If the collection needs to be modified, use List<string>.

So in your case, use List<string> :)

Upvotes: 1

Related Questions