Namespace Constant in C #

Is there a way to define a constant for the entire namespace, not just inside the class? For example:

namespace MyNamespace { public const string MY_CONST = "Test"; static class Program { } } 

Gives a compilation error as follows:

Expected class, delegate, enumeration, interface, or structure

+42
c # namespaces const
May 12 '10 at 11:30 a.m.
source share
3 answers

I believe this is not possible. But you can create a class with only constants.

 public static class GlobalVar { public const string MY_CONST = "Test"; } 

and then use it like

 class Program { static void Main() { Console.WriteLine(GlobalVar.MY_CONST); } } 
+75
May 12 '10 at 11:35 a.m.
source share

It's impossible

From MSDN :

The const keyword is used to change the declaration of a field or local variable.

Since you can only have a field or local variable inside the class, this means that you cannot have a global const . (e.g. const namespace)

+11
May 12 '10 at 11:32
source share

No no. Put it in a static class or enumeration.

+2
May 12 '10 at 11:34
source share



All Articles