tag:blogger.com,1999:blog-865923359735383241.post1845554795393383559..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: C#: Static readonly vs constUnknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-865923359735383241.post-57610173825048088942012-06-01T22:26:59.320-06:002012-06-01T22:26:59.320-06:00My preference is to use const whenever I can, whic...My preference is to use const whenever I can, which as mentioned above is limited to literal expressions or something that does not require evaluation. <br /><br />If I hot up against that limitation, then I fallback to static readonly, with one caveat. I would generally use a public static property with a getter and a backing private static readonly field as Marc mentions here.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-53204178938368689992012-06-01T22:26:58.428-06:002012-06-01T22:26:58.428-06:00Some other things
const int a
must be initiali...Some other things<br /><br />const int a <br /><br /><br />must be initialized <br />initialization must be at compile time<br /><br /><br />readonly int a<br /><br /><br />can use default value, without initializing<br />initialization can be at run time<br /><br /><br />but please see : http://en.csharp-online.net/const,_static_and_readonlyUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-50314298268701663932012-06-01T22:26:54.989-06:002012-06-01T22:26:54.989-06:00One thing to note is const is restricted to primit...One thing to note is const is restricted to primitive/value types (the exception being strings)Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-59231211413777391692012-06-01T22:26:54.142-06:002012-06-01T22:26:54.142-06:00I would use static readonly if the Consumer is in ...I would use static readonly if the Consumer is in a different assembly. Having the const and the consumer in two differen assemblies is a nice way to shoot yourself in the foot.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-40041320381270978372012-06-01T22:26:52.382-06:002012-06-01T22:26:52.382-06:00Public static readonly fields are a little unusual...Public static readonly fields are a little unusual; public static properties (with only a get) would be more common (perhaps backed by a private static readonly field).<br /><br />Const values are burned directly into the call-site; this is double edged:<br /><br /><br />it is useless if the value is fetched at runtime, perhaps from config<br />if you change the value of a const, you need to rebuild all the clients<br />but it can be faster, as it avoids a method call...<br />...which might sometimes have been inlined by the JIT anyway<br /><br /><br />If the value will never change, then const is fine - Zero etc make reasonable consts ;-p Other than that, static properties are more common.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com