Amc_rtty
Amc_rtty

Reputation: 3813

How to avoid hardcoding strings

This is a question regarding best coding practices. I would like to know the consensus about how to best avoid hardcoding strings and values in a .NET application. What I've seen so far where I have previously worked:

However, is not the 3rd method, just another hardcode? Does it make sense to have such a class? The benefit would be that all strings are in one place, but is that really avoiding a hardcode?

Also, if you have developed other habits for this situation, feel free to post.

Upvotes: 6

Views: 4025

Answers (4)

Barry Kaye
Barry Kaye

Reputation: 7759

In my opinion ApplicationSettings is only useful if your settings are (i) genuinely constants and (ii) of course truly public. If either of these cases is not true then this is not ideal and your choice is back to a .config file .resx

Upvotes: 1

plague
plague

Reputation: 1908

in general i believe it is good practice to store all configurable data in a central config file so all the data is in one place and can be shared by other apps

Upvotes: 1

Mark Segal
Mark Segal

Reputation: 5550

Well, I think that ApplicationStrings is a good solution

Upvotes: 0

Joey
Joey

Reputation: 354576

The main benefit is indeed to have all strings in one place and you only need to change it there to update the whole program. If different parts of your program use the same string and one instance gets updated, but another not, then you have a problem. This holds true for all literals, by the way.

And then with resource files there is the benefit of i18n and l10n. If you need it, but many large applications should.

Upvotes: 5

Related Questions