Static or Resource class?

Asked

Viewed 33 times

1

I want to abstract the phrases/words often used in my project, however, arose the doubts where to put them Static Class or create a Resource? What is the recommendation for best practice and performance?

Obs: This project will not be multilingual.

Ex:

public Static class Mensagem
    {
        public const string Ok = "OK";
        public const string Cancelar = "Cancelar";
    }

Ex:

<data name="OK" xml:space="preserve">
    <value>OK</value>
</data>
<data name="CANCELAR" xml:space="preserve">
    <value>Cancelar</value>
</data>

1 answer

2


If it is your wish that these phrases/words can be exchanged at some point, use Resource. It is the feature that was made for this, and the design pattern has several features that can be interesting in refactoring, so it can be considered the best practice for your case. In that reply I address these functionalities from the point of view of multiple languages, but can perfectly serve for your project.

Now, if it’s just recurring messages, I see no problem using static class. The fulfilled function will be the same, although the approach is a little more homely.

In performance the difference is imperceptible as the Resource is a key-value dictionary (therefore indexed), and the static class is compiled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.