Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace constant in C#

Is there any way to define a constant for an entire namespace, rather than just within a class? For example:

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

Gives a compile error as follows:

Expected class, delegate, enum, interface, or struct

like image 462
Paul Michaels Avatar asked May 12 '10 11:05

Paul Michaels


People also ask

Are variables in namespace static?

When you declare a variable as static , it means that its scope is limited to the given translation unit only. Without static the scope is global. When you declare a variable as static inside a . h file (within or without namespace ; doesn't matter), and include that header file in various .


1 Answers

I believe it's 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);     } } 
like image 84
RvdK Avatar answered Sep 19 '22 14:09

RvdK