{"componentChunkName":"component---src-templates-blog-template-js","path":"/blog/2020-09-02-working-with-variables-operators-and-expressions-in-c-sharp","result":{"data":{"markdownRemark":{"html":"<h1>INDEX</h1>\n<ul>\n<li>Understanding statements, identifiers and keywords</li>\n<li>Variables</li>\n<li>Primitive data types</li>\n<li>Operators</li>\n<li>Implicitly Typed Local Variables</li>\n</ul>\n<h2>Understanding statements, identifiers and keywords</h2>\n<h3>Statements</h3>\n<p>A <code>statement</code> is a command that performs and action, such as calculating a value and storing a result or displaying a message to a user. You combine statements to create methods. Statements in C# follow a rules describing their format and construction, also known as <code>syntax</code>. Meanwhile, the specification of what a statement does is known as <code>semantics</code>. For example, in contrast with Javascript, which is a more permissive programming language, you don't have to worry so much about ending your statements with a semicolon (;), since the compiler adds them asumming you forgot to. But in C#, if there's a statement with out semicolon, you'll get a compiling error.</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\">Console<span class=\"token punctuation\">.</span><span class=\"token function\">WriteLine</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Hello, World!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// C# syntax</span></code></pre></div>\n<h3>Identifiers</h3>\n<p><code>Identifiers</code> are the names that you use to identify the elements in your programs, such as <code>namespaces</code>, classes, methods and variables. There's two main rules when selecting identifiers:</p>\n<ul>\n<li>Only use letters (uppercase and lowercase), digits, and underscore characters.</li>\n<li>Identifiers MUST start with a letter or underscore.\nSomething to take into consideration is that C# is a case-sensitive language: <code>thisIdentifier</code> is different than <code>ThisIdentifier</code>.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token keyword\">namespace</span> <span class=\"token namespace\">TestHello</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">class</span> <span class=\"token class-name\">Program</span>\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">static</span> <span class=\"token return-type class-name\"><span class=\"token keyword\">void</span></span> <span class=\"token function\">Main</span><span class=\"token punctuation\">(</span><span class=\"token class-name\"><span class=\"token keyword\">string</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span></span> args<span class=\"token punctuation\">)</span>\n        <span class=\"token punctuation\">{</span>\n            Console<span class=\"token punctuation\">.</span><span class=\"token function\">WriteLine</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"I love programming :)\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>In the example above, <code>TestHello</code>, <code>Program</code> and <code>Main</code> are consider identifiers.</p>\n<h3>Keywords</h3>\n<p>There's 77 identifiers that the C# programming language reserves for its own use and you cannot reuse these for your own purposes, they're called <code>keywords</code>. Here you can find the full list of keywords:</p>\n<ul>\n<li><a href=\"https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords\">C# Keywords</a>.</li>\n</ul>\n<h2>Variables</h2>\n<p>A <code>variable</code> is a storage location that holds a value, just like a box in the computer's memory that holds some specific information. You use a variable's name to later refer to the value it holds. A good practice in any programming language is to adopt a naming convention that helps to avoid confusion and can reduce the scope for bugs. You should avoid differentiating variables just by camel case. Here's a list of rule-of-thumb guidelines to follow when it comes to choose variable names:</p>\n<ul>\n<li>Don't create identifiers that differ only by case.</li>\n<li>Start the name with a lowercase letter.</li>\n<li>Use <code>camelCase</code> notation.</li>\n<li>Avoid Hungarian notation.</li>\n</ul>\n<p>Variables can hold many different types of values, in C# you must specify the type of data it will store. You declare the type and name of a variable in declaration statements.</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token class-name\"><span class=\"token keyword\">int</span></span> age<span class=\"token punctuation\">;</span>\nage <span class=\"token operator\">=</span> <span class=\"token number\">21</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>In the last example, we first declare what the date type of the variable, in this case we use the primitive data type <code>int</code> which denotes a integer. Then we assign the value of the variable, which in this case is 42. To assign the value, you use the <code>equals</code> sign (=) and is known as the <code>assignment operator</code>.</p>\n<h2>Primitive data types</h2>\n<p>Common types such as numeric, strings, character and boolean values, are collectively know as the <code>primitive data types</code>. </p>\n<ul>\n<li><a href=\"https://condor.depaul.edu/sjost/nwdp/notes/cs1/CSDatatypes.htm\">C# Primitive Data Types</a>.</li>\n</ul>\n<h2>Operators</h2>\n<h3>Arithmetic Operators</h3>\n<p>C# supports the regular arithmetic operations we all learn throughout childhood. The symbols <code>+, -, *, /</code> are called operators because they \"operate\" on values creating new ones. It's important to clarify that not all operators apply to all data types. The arithmetic operators can't be used on, for example, <code>string</code> types or <code>booleans</code>. However, you can use the <code>+</code> operator to concatenate string values, for example:</p>\n<div class=\"gatsby-highlight\" data-language=\"c-sharp\"><pre class=\"language-c-sharp\"><code class=\"language-c-sharp\">Console.WriteLine(&quot;43&quot; + &quot;1&quot;); // This will output 431, not 44!</code></pre></div>\n<p>Soemthing to take into consideration is that the result of arithmetic operations depends on the type of the operands used. For example, the value of the expressions <code>5.0/2.5</code> is 2.5; the type of both operands is double, so the ttype of result is also double (Remember that in C#, literal numbbers with decimal points are always double, not float, to maintain as much accuracy as possible). Another trick in C# is the representation of <code>Infinity</code> just for float and double data types, which can be represented as 5.0/0.0. Also NaN (Not a Number), which cas appear by paradoxes such as 0.0/0.0.</p>\n<p>Another less-familiar arithmetic operator is the <code>remainder</code>, or <code>modulus</code>, operator, which is represented by the percent sign (%). The best way to describe its purpose is: the result of x % y is the integer remainder after dividing the integer value x by the integer value y.</p>\n<h3>Assignment Operator</h3>\n<p>The equal sign (=) is the assignment operator. The operand on the right side is evaluated and then stored in the operand on the left side. The value of the assignment operator is the value that is assigned to the left of the operand.</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token class-name\"><span class=\"token keyword\">int</span></span> myInteger<span class=\"token punctuation\">;</span>\nmyInteger <span class=\"token operator\">=</span> <span class=\"token number\">10</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// value of assignment expression is 10.</span></code></pre></div>\n<h3>Precendence and Associativity</h3>\n<p>In C#, the multiplicative operators <code>(/, *, and %)</code> have <code>precendence</code> over the additive operators <code>(+ and -)</code>, so in expressions such as 2 + 3 * 4, the multiplication is performed first, followed by the addition.</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token number\">2</span> <span class=\"token operator\">+</span> <span class=\"token number\">3</span> <span class=\"token operator\">*</span> <span class=\"token number\">4</span> <span class=\"token comment\">// This will output 14, not 20.</span></code></pre></div>\n<p>But what happends when an expression contains different operators with the same precendence? <code>Associativity</code> comes into play, which determines if an expression is calculated from left to right or from right to left. Arithmetic operators are left associative and the assignment operator (=) is right associative.</p>\n<h3>Unary operators</h3>\n<p>C# provides operators with the only task of adding or substracting 1 to a variable (++ and --). This operators are unary, which means that they take only one single operand. They share the same precendence and are both left associative. They are also unusual in that you can place them either before or after the variable:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\">count<span class=\"token operator\">++</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// postfix increment</span>\n<span class=\"token operator\">++</span>count<span class=\"token punctuation\">;</span> <span class=\"token comment\">//prefix increment</span>\ncount<span class=\"token operator\">--</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// postfix decrement</span>\n<span class=\"token operator\">--</span>count<span class=\"token punctuation\">;</span> <span class=\"token comment\">// prefix decrement</span></code></pre></div>\n<p>The benefits of this is seen when evaluating operations. The value returned by count++ is the value of count before the increment takes place, whereas the value returned by ++count is the value of count after the increment takes place. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token class-name\"><span class=\"token keyword\">int</span></span> x<span class=\"token punctuation\">;</span>\nx <span class=\"token operator\">=</span> <span class=\"token number\">42</span><span class=\"token punctuation\">;</span>\n\nConsole<span class=\"token punctuation\">.</span><span class=\"token function\">WriteLine</span><span class=\"token punctuation\">(</span>x<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Returns 42, not 43.</span>\nConsole<span class=\"token punctuation\">.</span><span class=\"token function\">WriteLine</span><span class=\"token punctuation\">(</span><span class=\"token operator\">++</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Returns 43.</span></code></pre></div>\n<h2>Implicitly Typed Local Variables</h2>\n<p>Before ending this, I would like to introduce <code>Implicitly Typed Local Variables</code>, which are represented by using the <code>var</code> keyword when declaring/assigning a variable:</p>\n<div class=\"gatsby-highlight\" data-language=\"csharp\"><pre class=\"language-csharp\"><code class=\"language-csharp\"><span class=\"token class-name\"><span class=\"token keyword\">var</span></span> myVariable <span class=\"token operator\">=</span> <span class=\"token number\">99</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Type of int number</span>\n<span class=\"token class-name\"><span class=\"token keyword\">var</span></span> myOtherVariable <span class=\"token operator\">=</span> <span class=\"token string\">\"Hello\"</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Type of string</span></code></pre></div>\n<p>By doing this you are asking the C# compiler to infer the type of a variable from an expression and use this type when declaring the variable. This causes myVariable to only be able to take int data types. you should also understand that you can only use the <code>var</code> keyword when programming full declarations, so it's purpose its just for convenience.</p>","frontmatter":{"date":"September 01, 2020","slug":"/blog/2020-09-02-working-with-variables-operators-and-expressions-in-c-sharp","title":"Working with Variables, Operators, and Expressions in C#"}}},"pageContext":{"slug":"/blog/2020-09-02-working-with-variables-operators-and-expressions-in-c-sharp"}},"staticQueryHashes":[]}