{"id":115,"date":"2021-12-22T14:16:06","date_gmt":"2021-12-22T07:16:06","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=115"},"modified":"2022-06-03T13:16:30","modified_gmt":"2022-06-03T06:16:30","slug":"csharp-integer","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-integer\/","title":{"rendered":"C# Integers"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# integer types to represent the integer numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# integers<\/h2>\n\n\n\n<p>Integers are whole numbers for example -1, 0, 1, 2, 3. C# uses the integral numeric types to represent integer numbers. So far, you have learned how to use the <code>int<\/code> type to declare a <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-variables\/\">variable<\/a> that holds an integer. <\/p>\n\n\n\n<p>Besides the <code>int<\/code> type, C# has other integer types with their specific keyword, range, and size.<\/p>\n\n\n\n<p>The following table illustrates the characteristics of all the integer types in C#:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>C# type\/keyword<\/th><th>Range<\/th><th>Size<\/th><\/tr><\/thead><tbody><tr><td><code>sbyte<\/code><\/td><td>-128 to 127<\/td><td>Signed 8-bit integer<\/td><\/tr><tr><td><code>byte<\/code><\/td><td>0 to 255<\/td><td>Unsigned 8-bit integer<\/td><\/tr><tr><td><code>short<\/code><\/td><td>-32,768 to 32,767<\/td><td>Signed 16-bit integer<\/td><\/tr><tr><td><code>ushort<\/code><\/td><td>0 to 65,535<\/td><td>Unsigned 16-bit integer<\/td><\/tr><tr><td><code>int<\/code><\/td><td>-2,147,483,648 to 2,147,483,647<\/td><td>Signed 32-bit integer<\/td><\/tr><tr><td><code>uint<\/code><\/td><td>0 to 4,294,967,295<\/td><td>Unsigned 32-bit integer<\/td><\/tr><tr><td><code>long<\/code><\/td><td>-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807<\/td><td>Signed 64-bit integer<\/td><\/tr><tr><td><code>ulong<\/code><\/td><td>0 to 18,446,744,073,709,551,615<\/td><td>Unsigned 64-bit integer<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>C# also has other two integer types <code>nint<\/code> and <code>nunit<\/code> whose ranges depend on the specific platform.<\/p>\n\n\n\n<p>If you declare a variable with an integer type and assign a value that is out of range, you&#8217;ll get a compilation error.<\/p>\n\n\n\n<p>For example, the following declares a variable age with the type byte and initializes its value to an invalid value 256:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">byte<\/span> age = <span class=\"hljs-number\">256<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you compile the code, the compiler issues the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">error CS0031: Constant value '256' cannot be converted to a 'byte'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Min and max values<\/h2>\n\n\n\n<p>Each integer type has the <code>MinValue<\/code> and <code>MaxValue<\/code> constants that provide the minimum and maximum of the type. To access these values, you use the dot operator (<code>.<\/code>). For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">int<\/span><span class=\"hljs-selector-class\">.MinValue<\/span>\n<span class=\"hljs-selector-tag\">int<\/span><span class=\"hljs-selector-class\">.MaxValue<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following displays the range of the <code>int<\/code> type:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Console.WriteLine(<span class=\"hljs-string\">$\"int range: (<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">int<\/span>.MinValue}<\/span>,<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">int<\/span>.MaxValue}<\/span>)\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">int range: (-2147483648,2147483647)<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Integer literals<\/h2>\n\n\n\n<p>Integer literals can be decimal, hexadecimal, and binary. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Decimal<\/h3>\n\n\n\n<p>The following example shows the integer literals in decimal without any prefix:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> quantity = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">int<\/span> amount = <span class=\"hljs-number\">20<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If a number is big, you can use the digit separator (_) to make it more readable. Note that you can use the digit separator (<code>_<\/code>) for all integer literals, not just decimal. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> prize = <span class=\"hljs-number\">1<\/span>_000_000;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Hexadecimal<\/h3>\n\n\n\n<p>Hexadecimal numbers have the <code>0x<\/code> or <code>0X<\/code> prefix. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> address = <span class=\"hljs-number\">0x5A<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Binary<\/h3>\n\n\n\n<p>Binary numbers have the <code>0b<\/code> or <code>0B<\/code> prefix. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> flag = <span class=\"hljs-number\">0b10011110<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, you can use the digit separator (<code>_<\/code>) to separate the digits like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> flag = <span class=\"hljs-number\">0b<\/span>_1001_1110;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use C# integer type to represent integer numbers.<\/li><li>Use digit separator (<code>_<\/code>) with big numbers to make them more readable.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"115\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-integer\/\"\n\t\t\t\tdata-post-title=\"C# Integers\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"115\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-integer\/\"\n\t\t\t\tdata-post-title=\"C# Integers\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn to use the C# integral types to represent the integer numbers.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-115","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/115\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/115\/revisions\/700"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}