If you decide to skip using the ResourceManager , you can let Visual Studio handle the code generation for you. Ultimately, the generated code uses the ResourceManager , but you no longer write this code manually. In addition, you get a compile-time check, since you are referring to a generated static class.
If you add a resource file to your project and double-click it in Solution Explorer, Visual Studio will present you with a dialog where you can enter the name of the resource and its value. The dialog box provides options for adding resources in the form of strings, images, audio, etc. (See the dropdown menus at the top of the dialog box). Then, to get the code generation bit, you need to set the Access Modifier both "Public" and "Internal". Third option: "No code generation."
For example, add a resource file called "MyResources", then add a string resource named Greeting and value Hello! . One of the two previous options chosen to generate the code (start by testing it, restrict access as needed), now you can link to resources from your code through MyResources.Greeting . If you donโt see it right away, make sure you save the file and try compiling.
string greeting = MyResources.Greeting;
If you add other types of resources (image, audio, etc.), then the return types will be different, of course.
At this point, you can check the generated .cs file and see that the generated code uses the ResourceManager . Another use of resource files is localization. Let's say you need the Spanish version of MyResources . You would add a new file called MyResources.es.resx , where es matches the desired language code (in this case, Spanish). Now add the Greeting resource name with the Spanish value Hola! .
If you change the stream culture to Spanish, a link to the resource will return the Spanish version:
string defaultGreeting = MyResources.Greeting; // "Hello!" Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("es"); string spanishGreeting = MyResources.Greeting; // "Hola!"
Note that you really need to set an access modifier for one of the code generation options for your default resource file (i.e. MyResources.resx ), and not for all other localized versions that you add. There is no harm in this, but, in my opinion, it is cleaner to have a main file created, while others just have the required resource values โโwithout generating code.