.NET "global" variables; Which one is better?

Which should i use?

It:

public class MyClass
{
    static string m_MyString = "My string here";
    public static string MyString { get { return m_MyString; } }
}

Or that:

public class MyClass
{
    public static readonly string MyString = "My string here";
}
+3
source share
3 answers

Use the latter, but with a slight modification:

public static class CommonStrings
{
    public const string MyString = "My string here";
}

In another note, “global” variables have been branded to be bad practice, but in this case, in my opinion, the common container for reusable strings is preferred; I would not do everything that you want to use publicly available, especially if they are not permanent.

+1
source

. , : ? , ? , , MyClass? - , , MyClass?

+1

, (resx) , .

.

, , , , .NET "CustomEnvironment" ( "" ).

"" , , , .

" " , . " " , " ?":

  • : , , ( ) : .

  • : "" , "" , , , .

  • . , , Microsoft.NET.

  • . ;)

, , " " - , , - , .

Perhaps you need to calculate or process something before setting such a “constant”: you will use the field for reading only, but for me it’s just an opinion — I suggest you use the property to expose it (maybe you don’t need such a field for reading, because sometimes you prefer to calculate / process access to this property of the code each time, but if you always do this with properties, you can encapsulate this implementation detail).

+1
source

All Articles