General CS question about constants

后端 未结 7 1369
旧巷少年郎
旧巷少年郎 2021-01-24 02:41

I\'m programming using C#, after programming on C. So I\'m using a lot of constants such as \"DEFAULT_USER_ID\", \"REMOTE_ADDRESS\" and such...

It seems to me that it\'s

7条回答
  •  长情又很酷
    2021-01-24 03:25

    Also you may define constants like that

        public static class Defaults
        {
            public const string MyName = "SuperName";
        }
    
        public  class MyClass
        {
            string s = Defaults.MyName;
        }
    

    In such case you may use class Defaults anywhere in your app

    Also you may want to know that there is two ways of defining constant variables in Static readonly vs const

提交回复
热议问题