Namespace constant in C#

前端 未结 4 1379
感动是毒
感动是毒 2020-12-08 00:07

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

namespace MyNamespace
{    
    public const st         


        
4条回答
  •  旧巷少年郎
    2020-12-08 00:36

    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);
        }
    }
    

提交回复
热议问题