views:

139

answers:

2

in my C# application i need to store huge amounts of constant strings in arrays, like one array for first names and one for last name and so on...

These strings never change so my question is how to store them ?

Make a static constant class with these arrays ?

Load them at runtime from somewhere?

Or any other solution...

PS: I don't really want external files so if i load them from somewhere they have to be included in the .exe

EDIT:// So i just make resource files with string[] arrays, alrigt :)

+5  A: 

In cases like this i use resource files.
I create a resource file called Constants for example, and then i can call it from any where in my application.

http://www.sliver.com/dotnet/articles/resinweb.aspx

Also in past i did it twice to create a class which contains all variables as 'const'.

  public const string myVariable = "some static text";

but i felt its the wrong place to do that and started using resource files.

Update: The Question is telling that the fixed strings are in string[] array, which is a case i didn't remember i met, so don't know which way will be better for you in this case regarding performance and code maintainability.

Amr ElGarhy
+1. Also, with VS2003+, you can add text files to your project, and drag them into a resource in the project. The resource class will then contain a `String` constant with the contents of the text file, but the text file is infinitely easier to manage and update than trying to maintain a grid cell in the resource editor. With this method, you can easily generate your content file as CSV, then on app startup, have a process that loads the content and parses it into a static array to be used in code.
Toby
+1  A: 

Make a dedicated class in which you put the strings as readonly fields. When first requested you can put it in the Cache object so afterwards you can get them from there for fast retrieval.

Grz, Kris.

XIII
It's not bad idea in general. However the better way is using the resources, if you can.
TcKs