{"id":67,"date":"2016-02-11T15:50:17","date_gmt":"2016-02-11T15:50:17","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=67"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"constants","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/memory\/constants","title":{"rendered":"Constants"},"content":{"rendered":"\n<p>You have to use constants within a class. &nbsp;So like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public const int MY_NUMBER = 9;<\/code><\/pre>\n\n\n\n<p>You&#8217;d need to put it in a class somewhere, and the usage would be ClassName.MY_NUMBER<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Number Defines<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public const int SOFTWARE_VERSION = 42;\nconst int SOFTWARE_VERSION = 42;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">String Defines<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public const string MY_THINGS_PATH = \"c:\\\\windows\\\\\";\nconst string MY_THINGS_PATH = \"c:\\\\windows\\\\\";\nprivate const string MY_THINGS_PATH = \"c:\\\\windows\\\\\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Array Defines<\/h4>\n\n\n\n<p>Const can&#8217;t be used because it can only be applied to a field whose value is known at compile-time and arrays are not a constant in C#.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate static readonly string&#91;] MyConstArray = { \"ab\", \"cd\" };<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You have to use constants within a class. &nbsp;So like this: You&#8217;d need to put it in a class somewhere, and the usage would be ClassName.MY_NUMBER Number Defines String Defines Array Defines Const can&#8217;t be used because it can only be applied to a field whose value is known at compile-time and arrays are not [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/67\/revisions\/448"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}