{"id":107,"date":"2022-09-16T07:29:30","date_gmt":"2022-09-16T07:29:30","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=107"},"modified":"2022-09-16T07:39:27","modified_gmt":"2022-09-16T07:39:27","slug":"c-how-to-create-your-own-variable-type","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/c-how-to-create-your-own-variable-type\/","title":{"rendered":"C#, how to create your own variable type"},"content":{"rendered":"\n<p>Do you know that you can create your own variable types in C# programming language.<\/p>\n\n\n\n<p>Here is a good example.<br>In this code, only 1 or 0 can be assigned to the variable.<\/p>\n\n\n\n<p>(Source: <a href=\"https:\/\/gist.github.com\/mrna0\/0de92abcce271c02ead6d73773f920a8\" data-type=\"URL\" data-id=\"https:\/\/gist.github.com\/mrna0\/0de92abcce271c02ead6d73773f920a8\">My Gist<\/a>)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>struct BoolInt\n{\n    private readonly int value;\n    private BoolInt(int value)\n    {\n        this.value = value;\n    }\n\n    private int Value\n    {\n        get { return value; }\n    }\n\n    public static implicit operator BoolInt(int value)\n    {\n        if (value &gt; 1 || value &lt; 0)\n        {\n            throw new Exception(&quot;Value can be only 1 or 0&quot;);\n        }\n        else\n        {\n            return new BoolInt(value);\n        }\n    }\n\n    public static implicit operator int(BoolInt i)\n    {\n        return i.Value;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>Usage:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>BoolInt val = 0;\n\nBoolInt val2 = new BoolInt();\nval2 = 1;<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Do you know that you can create your own variable types in C# programming language. Here is a good example.In this code, only 1 or<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[16,29,28],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-struct","tag-variable"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":1,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}