<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kristina (Coding Mama)</title>
    <description>The latest articles on DEV Community by Kristina (Coding Mama) (@coding_mama).</description>
    <link>https://dev.to/coding_mama</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F220759%2Fa39705cd-de35-4421-bb1b-7d202fcaa30e.png</url>
      <title>DEV Community: Kristina (Coding Mama)</title>
      <link>https://dev.to/coding_mama</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coding_mama"/>
    <language>en</language>
    <item>
      <title>C# For Beginners - Lesson 10: Methods</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Sat, 28 Aug 2021 09:18:15 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-10-methods-3e01</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-10-methods-3e01</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a method? What is it used for?&lt;/li&gt;
&lt;li&gt;How do I declare a method? How do I call it?&lt;/li&gt;
&lt;li&gt;What is a method parameter?&lt;/li&gt;
&lt;li&gt;What is a return statement?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;In our final lesson, we will learn about methods, C#'s equivalent to functions or subroutines in other programming languages.&lt;/p&gt;

&lt;p&gt;As our programs get bigger and more complex, we can organise our code into more manageable chunks by grouping related statements together. A &lt;em&gt;method&lt;/em&gt; is a group of statements that can be called to perform a single task.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method declaration and invocation
&lt;/h3&gt;

&lt;p&gt;Let's go back to our very first program, Hello World. Instead of just printing the message, perhaps we want to make it fancier by adding an &lt;a href="https://www.asciiart.eu/art-and-design/patterns" rel="noopener noreferrer"&gt;ASCII art border&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Below we've used a &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-8-loops-3mf0"&gt;for loop&lt;/a&gt; to make a string that repeats &lt;code&gt;+-&lt;/code&gt; 20 times. We then use this string as our border.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Creates a border like this: +-+-+-+-+-+-&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"+-"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Prints the message with the border&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose we wanted to display this message at various times in our program. To do that, we would have to keep repeating these lines. A better way would be to group them first into a method called &lt;code&gt;ShowMessage&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...steps go here!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To declare a method in C#, we specify the method's &lt;em&gt;return type&lt;/em&gt;, followed by the name of the method, and round brackets. The curly brackets contains the method's body - the statements that we want to run when the method is called. We'll explain return type later in this lesson.&lt;/p&gt;

&lt;p&gt;Once the method is declared, we can call &lt;code&gt;ShowMessage&lt;/code&gt; every time we want to display the message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Displays the message twice&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To call or &lt;em&gt;invoke&lt;/em&gt; a method in C#, we use the name of the method followed by round brackets. A method call by itself is a valid statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rsi6mnpg3wb769bpvr5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rsi6mnpg3wb769bpvr5.png" alt="Methods calls are valid statements" width="800" height="452"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Method parameters and arguments
&lt;/h3&gt;

&lt;p&gt;Suppose we wanted to display a different message, but we still want a fancy border. We can't use &lt;code&gt;ShowMessage&lt;/code&gt; because it would say "Hello, World!". But the steps are pretty much the same, so can we reuse it somehow?&lt;/p&gt;

&lt;p&gt;Instead of duplicating then varying it slightly, we could make our method more generic by adding &lt;em&gt;parameters&lt;/em&gt;. A method parameter is a variable that can be used in the method to alter its behaviour. &lt;/p&gt;

&lt;p&gt;In our method declaration, we put parameters in the round brackets. First, we specify the type of the parameter, then the name of the parameter. It's similar to how we declare a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Creates a border like this: +-+-+-+-+-+-&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"+-"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Prints the message with the border&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// The parameter is used here!&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call a method, we can supply values (also called &lt;em&gt;arguments&lt;/em&gt;) inside the round brackets. If a parameter is defined for that method, that argument gets passed in as the value of the parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Display a different message&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Greetings, human!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Display another message&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"こんにちは、世界！"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5zn57pn77dtzwei7om0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5zn57pn77dtzwei7om0o.png" alt="Passing arguments to methods" width="800" height="452"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We can have more than one parameter in a method. For example, we could change the border by supplying the pattern to repeat. Below, we have added a &lt;code&gt;string&lt;/code&gt; parameter called &lt;code&gt;borderPattern&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;borderPattern&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Creates a border&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;border&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;borderPattern&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// borderPattern is used here!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Prints the message with the border&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// message is used here!&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;border&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Call the ShowMessage method&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey, you. You're finally awake."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"-&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call &lt;code&gt;ShowMessage&lt;/code&gt; with multiple arguments, the order matters. For example, if we do this, can you guess what will happen?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"-&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hey, you. You're finally awake."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tla085y1bkmqis7ac5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tla085y1bkmqis7ac5d.png" alt="Not the output we wanted!" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The order of the arguments need to match the parameter order!&lt;/p&gt;

&lt;h3&gt;
  
  
  Named arguments
&lt;/h3&gt;

&lt;p&gt;If a method accepts many parameters, it can be hard to keep track of the order. Fortunately, C# allows us to name the arguments as we pass them in the method. Replace Line 17 of the previous program with the line below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;borderPattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"-&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hey, you. You're finally awake."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we supplied the border pattern first, C# knows to assign this value to the &lt;code&gt;borderPattern&lt;/code&gt; parameter.  The same goes with the &lt;code&gt;message&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yy36idr99emrh4zd3o2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yy36idr99emrh4zd3o2.png" alt="Using named parameters" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Optional parameters
&lt;/h3&gt;

&lt;p&gt;We've made our method more flexible. However, when we invoke the &lt;code&gt;ShowMessage&lt;/code&gt; method, we can no longer just supply a message - we will need to include the border pattern as well.&lt;/p&gt;

&lt;p&gt;If we don't supply a border pattern, we want the method to default to our original pattern. To do this, in our parameter declaration we need to assign a value to the parameter. Add &lt;code&gt;= "+-"&lt;/code&gt; next to the &lt;code&gt;borderPattern&lt;/code&gt; parameter in the declaration, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;borderPattern&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"+-"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// Call the ShowMessage method&lt;/span&gt;
&lt;span class="nf"&gt;ShowMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey, you. You're finally awake."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can call &lt;code&gt;ShowMessage&lt;/code&gt; again with just the message!&lt;/p&gt;

&lt;h3&gt;
  
  
  Method return type
&lt;/h3&gt;

&lt;p&gt;Aside from performing a task, methods can also return a value after the task has been completed. Our method &lt;code&gt;ShowMessage&lt;/code&gt; displays a message for us, but it doesn't really return a value we can use. That's why its return type is &lt;code&gt;void&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is an example method that returns a value. Can you guess what the method does?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Double&lt;/code&gt; method takes a number of type &lt;code&gt;int&lt;/code&gt; and returns the number multiplied by 2, effectively doubling it. The value that is returned is of type &lt;code&gt;int&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We use the &lt;code&gt;return&lt;/code&gt; keyword to specify the value that is returned by the method. If the method return type is not &lt;code&gt;void&lt;/code&gt;, the &lt;code&gt;return&lt;/code&gt; statement is &lt;strong&gt;required&lt;/strong&gt; to be present in the method's body.&lt;/p&gt;

&lt;p&gt;We can add more statements before the &lt;code&gt;return&lt;/code&gt; statement if needed, and they will be executed before the method finishes and returns the expression &lt;code&gt;number * 2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When the method has either returned a value or reached the end of its body, program execution will go back to the statement that called the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling the method Double"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"And we're back here!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvinkn1wpudny9oorihu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvinkn1wpudny9oorihu.png" alt="Returning a value from a method" width="800" height="221"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Methods with a return value can be used in expressions, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints 24&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;True or False:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To declare a method, we specify the return type and method name, followed by square brackets.&lt;/li&gt;
&lt;li&gt;The order of the arguments passed in a method must match the order of the parameters.&lt;/li&gt;
&lt;li&gt;To make a parameter optional, we mark it with the keyword &lt;code&gt;optional&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;void&lt;/code&gt; method cannot be used in expressions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Console.WriteLine()&lt;/code&gt; is a method.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You are a human compiler. Can you find all the errors in this code?&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Adding {x} and {y}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What is the difference between a method parameter and a method argument?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;Multiply&lt;/code&gt; method similar to the &lt;code&gt;Double&lt;/code&gt; method in the lesson. Calling &lt;code&gt;Multiply(10,12)&lt;/code&gt; should return &lt;code&gt;120&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Convert the code in &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-7-conditionals-2e07"&gt;Lesson 7&lt;/a&gt; into a method called &lt;code&gt;IsAllowedToDrive&lt;/code&gt;. The method should have two parameters, &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;hasLicence&lt;/code&gt; . The return type should be &lt;code&gt;bool&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 9: Arrays</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Sat, 14 Aug 2021 05:36:33 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-9-arrays-1bh0</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-9-arrays-1bh0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is an array?&lt;/li&gt;
&lt;li&gt;What is an element? What is an index?&lt;/li&gt;
&lt;li&gt;How do I declare an array in C#?&lt;/li&gt;
&lt;li&gt;How do I access the values in an array?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;So far, we have been storing single values in our variables. But in some situations, we need variables that store more than one value.&lt;/p&gt;

&lt;p&gt;An array is a data structure that holds multiple values of the same type. It's denoted by the square brackets at the end of the type name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we have declared an array of integers. Because we have not initialised it yet, its value is &lt;strong&gt;null&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To be able to use the array, we need to initialise it first by allocating how many values we need. This number determines the &lt;strong&gt;size&lt;/strong&gt; of the array. Note that the size of the array is &lt;strong&gt;fixed&lt;/strong&gt; and cannot be changed once it's allocated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To initialise the array, we use the &lt;em&gt;new&lt;/em&gt; keyword, followed by the type of the array, and the number of values we want in the square brackets. Line 2 shows another way of declaring and initialising an array, using the var keyword that we learned in &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-5-variables-1pi4"&gt;Lesson 5&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can access the values in the array by their position using the square brackets. Each value is called an &lt;em&gt;element&lt;/em&gt; and its position is its &lt;em&gt;index&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's try printing some values. Type the following in your code box and run the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The array is initialised, but the elements in the array are still the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values" rel="noopener noreferrer"&gt;default values&lt;/a&gt; of each type. That's why &lt;code&gt;scores[1]&lt;/code&gt; prints &lt;code&gt;0&lt;/code&gt;, the default value for &lt;code&gt;int&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Try accessing the last element of the array. Do you know which index to use?&lt;/p&gt;

&lt;p&gt;Since we have 10 elements, wouldn't the last element be the 10th one? If you tried to run the code below, you might get a surprise!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An error happens because &lt;code&gt;10&lt;/code&gt; is not the index of the last element! Arrays in C# are &lt;em&gt;zero-based&lt;/em&gt;, meaning the index starts at &lt;code&gt;0&lt;/code&gt;. So an array of 10 elements would have indexes from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fir2dc436dno99vo1e158.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fir2dc436dno99vo1e158.png" alt="An error happens because 10 is not the index of the last element!" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To demonstrate this more clearly, let's first put some actual values into our array. We can do this during our array declaration. The following are all valid ways to declare and initialise an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can declare the array elements inside curly brackets next to the type. &lt;/p&gt;

&lt;p&gt;For Lines 1 and 2, we can also replace the first &lt;code&gt;int[]&lt;/code&gt; in Lines 1 and 2 with a &lt;code&gt;var&lt;/code&gt; keyword and the syntax will be valid. We can't do the same with Line 3, though.&lt;/p&gt;

&lt;p&gt;For Lines 2 and 3, the size of the array is inferred by C# from the number of values.&lt;/p&gt;

&lt;p&gt;Use one of the ways above to declare a &lt;code&gt;scores&lt;/code&gt; array with some values. Then print the first and last element by accessing the elements with the index &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;9&lt;/code&gt; respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Declare an array with 10 elements&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;93&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;98&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;74&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;99&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Print the first element&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="c1"&gt;// Print the last element&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the program. The first element is &lt;code&gt;100&lt;/code&gt;, and the last element is &lt;code&gt;99&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can get the total number of elements in an array using the &lt;code&gt;Length&lt;/code&gt; property. Add the following to the code above and run the program. The output should be &lt;code&gt;10&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F690m79534n0oga9umkt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F690m79534n0oga9umkt6.png" alt="Total number of elements in an array using the  raw `Length` endraw  property" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you recall where we used a &lt;code&gt;Length&lt;/code&gt; property before? In &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-6-strings-2n6h"&gt;Lesson 6&lt;/a&gt; we learned that strings have a &lt;code&gt;Length&lt;/code&gt; property that returns the number of characters in the string. Coincidence?&lt;/p&gt;

&lt;p&gt;No, because strings are actually arrays of &lt;code&gt;char&lt;/code&gt; values! You can do the following with a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"consecutive normal punches"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"The 4th character of '&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;' is '&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s"&gt;'."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdud8yp0ilfkpv8ri3t8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdud8yp0ilfkpv8ri3t8.png" alt="A string is an array of characters" width="800" height="134"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;True or False:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once allocated, the size of an array is fixed and cannot change.&lt;/li&gt;
&lt;li&gt;You can have both &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;string&lt;/code&gt; values in an &lt;code&gt;int[]&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;The first element of an array&lt;code&gt;arr&lt;/code&gt; is in &lt;code&gt;arr[0]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The last element of an array &lt;code&gt;arr2&lt;/code&gt; is in &lt;code&gt;arr2[arr2.Length-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This is a valid array declaration: &lt;code&gt;int[] arr3 = new int[];&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;string&lt;/code&gt; array with your desired number of elements. Populate it using one of the initialisation expressions shown in the lesson. Then print the first element, last element, and the total number of elements in the array. Use &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-6-strings-2n6h"&gt;string interpolation&lt;/a&gt; to make the output like this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;First element:
Last element:
Total number of elements:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;string&lt;/code&gt; array in the previous challenge, print out all the elements in the array. (Hint: You can use a &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-8-loops-3mf0"&gt;for loop&lt;/a&gt;!)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 8: Loops</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Mon, 09 Aug 2021 07:16:49 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-8-loops-3mf0</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-8-loops-3mf0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do I write code that runs repeatedly?&lt;/li&gt;
&lt;li&gt;How do I make a while loop?&lt;/li&gt;
&lt;li&gt;How do I make a for loop?&lt;/li&gt;
&lt;li&gt;What is an infinite loop?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;Imagine if you had to repeat a task many, many times. Even if it was a simple one, like saying "Happy Birthday to you!", you'd probably get tired of doing it after more than a few times.&lt;/p&gt;

&lt;p&gt;Not computers, though. They are great at doing the same things over and over again. They will happily repeat tasks, faster than we are capable of, until programmed to stop - or until they run out of resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  While loop
&lt;/h3&gt;

&lt;p&gt;We can tell C# to say "Happy Birthday to you!" 10 times, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;    
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Happy Birthday to you!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;    
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"END"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type the code above in your code box and run the code. You will see that C# has done that task without complaining. And in 0.4 seconds too!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9khdar2k3royw1iowu9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9khdar2k3royw1iowu9u.png" alt="A C# loop" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's break down the code.&lt;/p&gt;

&lt;p&gt;First, we declared a variable called &lt;code&gt;counter&lt;/code&gt; with a value of &lt;code&gt;0&lt;/code&gt;. This counter will hold how many times we have run the loop.&lt;/p&gt;

&lt;p&gt;The next statement is a &lt;em&gt;while&lt;/em&gt; loop block. Just like the &lt;em&gt;if&lt;/em&gt; statement in the previous lesson, the while statement block consists of a keyword (&lt;code&gt;while&lt;/code&gt;) followed by round brackets that contain the loop condition.&lt;/p&gt;

&lt;p&gt;The condition can be an expression, and the result of the expression must be a &lt;code&gt;bool&lt;/code&gt; or Boolean value. If the condition is &lt;strong&gt;true&lt;/strong&gt;, our program enters the loop body, the part surrounded by the curly brackets. The statements in the loop body will then be executed.&lt;/p&gt;

&lt;p&gt;When the program reaches the end of the loop body, it won't go to the statement outside of the curly brackets (&lt;code&gt;Console.WriteLine("END")&lt;/code&gt;). Instead, it will go back to the condition again and test if the condition is still true. It will keep going back to the loop body until the loop condition results in a &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In plain words: &lt;strong&gt;while a condition is true, keep doing something!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is why Line 6 is very important. &lt;code&gt;counter++&lt;/code&gt; is shorthand for &lt;code&gt;counter = counter + 1&lt;/code&gt;. We are increasing the value of &lt;code&gt;counter&lt;/code&gt; by 1, then assigning that value to &lt;code&gt;counter&lt;/code&gt; again. &lt;/p&gt;

&lt;p&gt;We do this just before the loop body ends. When our program returns to the loop condition to test it, the value of &lt;code&gt;counter&lt;/code&gt; has changed.&lt;/p&gt;

&lt;p&gt;Let's make our program print out the value of &lt;code&gt;counter&lt;/code&gt; so it's easier to see how the loop is working.&lt;/p&gt;

&lt;p&gt;Add the following line after &lt;code&gt;counter++;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the program. You will see the value of &lt;code&gt;counter&lt;/code&gt; increasing after every iteration of the loop. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fccepvzx467j3fqtqh2fa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fccepvzx467j3fqtqh2fa.png" alt="Output of the variable counter" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;counter&lt;/code&gt; has reached &lt;code&gt;10&lt;/code&gt;, the program tests the condition &lt;code&gt;counter &amp;lt; 10&lt;/code&gt; for the last time. Because 10 is &lt;strong&gt;not&lt;/strong&gt; less than 10, the condition is now false. The program exits the loop body and goes to the next statement outside, which is &lt;code&gt;Console.WriteLine("END")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we remove &lt;code&gt;counter++&lt;/code&gt; and run the code, the condition &lt;code&gt;counter &amp;lt; 10&lt;/code&gt; will never become false. Our program will enter an &lt;em&gt;infinite loop&lt;/em&gt;! &lt;/p&gt;

&lt;p&gt;An infinite loop is a loop that never ends. The only way we can stop it is if we forcibly terminate the program, or the computer runs out of memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  For loop
&lt;/h3&gt;

&lt;p&gt;The loop structure we have described is so commonplace in C# that we can declare it in a more concise manner using the &lt;em&gt;for&lt;/em&gt;  loop syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Happy Birthday to you!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"END"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax might look weird at first glance. But let's break it down first. You'll find that the expressions in the round brackets are already familiar to us.&lt;/p&gt;

&lt;p&gt;First, we don't need to declare the &lt;code&gt;counter&lt;/code&gt; variable separately anymore (Line 1 in our original program) - it is now declared within the for loop, inside the round brackets. &lt;/p&gt;

&lt;p&gt;The next part is our loop condition (Line 3), which is still &lt;code&gt;counter &amp;lt; 10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And the all-important Line 6 earlier - the statement that increases the value of &lt;code&gt;counter&lt;/code&gt; - is the last expression inside the round brackets.&lt;/p&gt;

&lt;p&gt;You can see that by using a for loop, we've shortened the program from 9 lines of code to 5 lines. We have moved some statements into expressions inside the round brackets.&lt;/p&gt;

&lt;p&gt;The statements inside the curly braces is still our loop body, which will keep executing as long as the condition (the middle expression) is true.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Find the error in this program. (Hint: Compare the syntax with the sample program above.)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Happy Birthday to you!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What is the output of this program? (Hint: &lt;code&gt;--&lt;/code&gt; is an operator that decreases a value by 1.)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;--;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Blast off!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Convert the code above to use the for loop syntax.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using a for loop, get the sum of numbers from 1 to 10. Print the sum to the console. &lt;/p&gt;

&lt;p&gt;Hint #1: You would need to declare an extra variable before the for loop that would hold the sum.&lt;/p&gt;

&lt;p&gt;Hint #2: For every iteration of the loop, you have the current number and the current sum. You would need to add them together to get a new value for the sum.&lt;/p&gt;

&lt;p&gt;Hint #3: The statement&lt;code&gt;sum = sum + number&lt;/code&gt; would evaluate &lt;code&gt;sum + number&lt;/code&gt; first, then the result of that expression is assigned to &lt;code&gt;sum&lt;/code&gt;, giving &lt;code&gt;sum&lt;/code&gt; a new value.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 7: Conditionals</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Mon, 02 Aug 2021 11:37:53 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-7-conditionals-2e07</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-7-conditionals-2e07</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do I write a conditional statement in C#?&lt;/li&gt;
&lt;li&gt;What is an else clause?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;In life, we make decisions every moment. We can show this decision making process through a programming construct called a &lt;em&gt;conditional&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  If statement
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;happy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;happy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clap your hands!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"END"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above contains an &lt;em&gt;if&lt;/em&gt; statement block. If the condition in the round brackets is &lt;strong&gt;true&lt;/strong&gt;, the program will execute the statements in the curly brackets. Otherwise, it will just skip the block and go to the next statement after it.&lt;/p&gt;

&lt;p&gt;Type the code above in the code box and run it. We should see two lines in the output, &lt;code&gt;Clap your hands!&lt;/code&gt; followed by &lt;code&gt;END&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95tjpkc2dppz5b6xqs4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F95tjpkc2dppz5b6xqs4h.png" alt="The condition is true" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Change the value of the variable &lt;code&gt;happy&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;, then run the program again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F01chna65p9bm5sjik8xt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F01chna65p9bm5sjik8xt.png" alt="The condition is false" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we don't see the &lt;code&gt;Clap your hands!&lt;/code&gt; line printed anymore. This is because our condition is the value of &lt;code&gt;happy&lt;/code&gt;. Since it is now false, our program does not run the lines inside the curly braces. Instead, it goes to the next statement, &lt;code&gt;Console.WriteLine("END");&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also use expressions as the condition in the round brackets, as long as the expression's result is a &lt;code&gt;bool&lt;/code&gt; value. Recall from &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke"&gt;Lesson 3&lt;/a&gt; that &lt;code&gt;bool&lt;/code&gt; or Boolean values have only two possible values, &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. Lesson 4 has a list of &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-4-operators-4en2"&gt;comparison and logical&lt;/a&gt; operators that we can use to make expressions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"abc123"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Your password is too short!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above checks the length of a password. In &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-6-strings-2n6h"&gt;Lesson 6&lt;/a&gt; we learned how to get the length of a string, by calling &lt;code&gt;.Length&lt;/code&gt;. The result is an &lt;code&gt;int&lt;/code&gt; value which we can now compare with another integer, &lt;code&gt;8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can even nest if statements, by putting another if statement inside the statement block.&lt;/p&gt;

&lt;p&gt;Type the code below into a code box and run the program. Try changing the values of &lt;code&gt;passedFirstTest&lt;/code&gt;, &lt;code&gt;passedSecondTest&lt;/code&gt;, and &lt;code&gt;passedThirdTest&lt;/code&gt; to see the different outputs you can make.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;passedFirstTest&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passedFirstTest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You've passed the first test!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;passedSecondTest&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passedSecondTest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You've passed the second test!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;passedThirdTest&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passedThirdTest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Congratulations! You passed all the tests!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzxgjvbua26mpytwpz75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzxgjvbua26mpytwpz75.png" alt="Output of the program above" width="800" height="451"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you may have noticed, we indent the statements inside the curly brackets. It's not required by C#, but it makes it easier to see which statements are part of which if block. Otherwise, the code can become unreadable, causing all sorts of program errors!&lt;/p&gt;

&lt;h3&gt;
  
  
  Else clause
&lt;/h3&gt;

&lt;p&gt;When we make a choice, often there is an alternate path that happens if the condition isn't true. We can show this in our code through the &lt;em&gt;else&lt;/em&gt; clause. The &lt;em&gt;else&lt;/em&gt; clause is an optional addition to an &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;

&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement doesn't always have to have an &lt;em&gt;else&lt;/em&gt; clause, but an &lt;em&gt;else&lt;/em&gt; clause can't exist without an &lt;em&gt;if&lt;/em&gt; statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're not allowed to drive yet."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're allowed to drive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is checking the value of &lt;code&gt;age&lt;/code&gt;. If &lt;code&gt;age&lt;/code&gt; is less than &lt;code&gt;16&lt;/code&gt;, the text &lt;code&gt;You're not allowed to drive yet&lt;/code&gt; is displayed. Otherwise, &lt;code&gt;You're allowed to drive&lt;/code&gt; is shown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvwx5k8en4wiohk3jbxt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvwx5k8en4wiohk3jbxt.png" alt="Else clause" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also nest another if statement inside the else clause, if we wanted to check for more conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hasLicence&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're not allowed to drive yet."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hasLicence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're allowed to drive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You need to have a licence to drive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jlqyhotq8e5aau2fzv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jlqyhotq8e5aau2fzv8.png" alt="Output of the program above, with a nested if statement inside the else clause" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# has special syntax to make this code more concise and readable. We can put the nested if statement on the same level as the else, creating an &lt;em&gt;else-if&lt;/em&gt; clause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hasLicence&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're not allowed to drive yet."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hasLicence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're allowed to drive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You need to have a licence to drive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr99njogi3qndsyuslv2j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr99njogi3qndsyuslv2j.png" alt="The same output, with different syntax" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What will be the output when this program is run?&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isHappy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isKnown&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isHappy&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;isKnown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clap your hands!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"END"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;True or False:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can put arithmetic expressions (e.g. &lt;code&gt;10 + 5&lt;/code&gt;) as the condition of an if statement.&lt;/li&gt;
&lt;li&gt;An else clause can only exist as part of an if statement.&lt;/li&gt;
&lt;li&gt;You can put an if statement inside another if statement.&lt;/li&gt;
&lt;li&gt;You can't put an if statement inside an else clause.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's make songs into code! Did you guess which popular song our sample code was about? If you guessed "If You're Happy And You Know It", you're correct!&lt;/p&gt;

&lt;p&gt;For this challenge, find a popular song with a conditional and try to represent it as code! Turn the "if" part into an if statement, and the rest into a &lt;code&gt;Console.WriteLine()&lt;/code&gt;statement.&lt;/p&gt;

&lt;p&gt;Example songs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"If You Leave Me Now" by Chicago

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;If you leave me now, you'll take away the biggest part of me&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;"If I Ain't Got You" by Alicia Keys

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;But everything means nothing, if I ain't got you&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;"I Will Always Love You" by Whitney Houston

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;If I should stay, I would only be in your way&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;"Time After Time" by Cyndi Lauper

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;If you're lost, you can look and you will find me&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;If you fall, I will catch you, I will be waiting&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 6: Strings</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Tue, 27 Jul 2021 08:25:22 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-6-strings-2n6h</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-6-strings-2n6h</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do I get the length (number of characters) of a string?&lt;/li&gt;
&lt;li&gt;How do I join multiple strings together?&lt;/li&gt;
&lt;li&gt;How do I make a template string for string interpolation?&lt;/li&gt;
&lt;li&gt;What is a verbatim string?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;Let's have fun with strings!&lt;/p&gt;

&lt;h3&gt;
  
  
  String Length
&lt;/h3&gt;

&lt;p&gt;Recall that strings represent text values. All strings have a property called &lt;code&gt;Length&lt;/code&gt; which returns the number of characters in the string. To get the length, attach &lt;code&gt;.Length&lt;/code&gt; to a string value or variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Calling Length directly from a string&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"12345"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Calling Length from a string variable&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"abcdef"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fykhkhh2lobxl4che9q8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fykhkhh2lobxl4che9q8k.png" alt="Using the Length property to get the string length" width="800" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  String Concatenation
&lt;/h3&gt;

&lt;p&gt;Declare two &lt;code&gt;string&lt;/code&gt; variables, one to hold a first name and one for a last name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Shepard"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can join two strings together to form a combined string using the &lt;code&gt;+&lt;/code&gt; operator. This is called &lt;em&gt;string concatenation.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Shepard"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;name&lt;/code&gt; contains a new string, the value &lt;code&gt;JaneShepard&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1wihdy1ekab522bbo07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1wihdy1ekab522bbo07.png" alt="String concatenation output" width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait! That doesn't look right. It would look better if there was a space in between them.&lt;/p&gt;

&lt;p&gt;To do this, we'll need to add a space in one of the variables. We can add it after &lt;code&gt;firstName&lt;/code&gt; or before &lt;code&gt;lastName&lt;/code&gt;. We could also just add a space string between the two variables.&lt;/p&gt;

&lt;p&gt;Add a space string (&lt;code&gt;" "&lt;/code&gt;) between the expression &lt;code&gt;firstName + lastName&lt;/code&gt;. Remember to use another &lt;code&gt;+&lt;/code&gt; to add this new string into the mix!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Shepard"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our name has a space between the first and last names.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezaqzg60kz7q81kh8uf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezaqzg60kz7q81kh8uf2.png" alt="Adding a space between the concatenated strings" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  String Interpolation
&lt;/h3&gt;

&lt;p&gt;When we have a lot of strings to join together, it can get convoluted quickly. Instead of joining the strings using &lt;code&gt;+&lt;/code&gt;, we could &lt;strong&gt;insert&lt;/strong&gt; our strings into a template string. This is called &lt;em&gt;string interpolation&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;String interpolation can provide a more readable syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Shepard"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is almost the same as the one before it, except that the expression assigned to &lt;code&gt;name&lt;/code&gt; is different.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The string value begins with a &lt;code&gt;$&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The variables &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; are inside the string.&lt;/li&gt;
&lt;li&gt;The variables &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; are wrapped in curly braces &lt;code&gt;{}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There is a space separating &lt;code&gt;{firstName}&lt;/code&gt; and &lt;code&gt;{lastName}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The variable &lt;code&gt;name&lt;/code&gt; has a template string assigned to it. &lt;/p&gt;

&lt;p&gt;We made a template string by putting a &lt;code&gt;$&lt;/code&gt; before the string value. Then we inserted the variables we wanted by wrapping them in curly braces and placing them in their desired positions inside the string. &lt;/p&gt;

&lt;p&gt;We want a space between the first name and last name, so there is a space already in the template string.&lt;/p&gt;

&lt;p&gt;If we wanted the output to be &lt;code&gt;Shepard, Jane&lt;/code&gt; instead, we can change the template string like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;name&lt;/code&gt; with this template string and run the code. The output should be &lt;code&gt;Shepard, Jane&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj16knaacv0jrj3vcsxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj16knaacv0jrj3vcsxu.png" alt="String interpolation example" width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Verbatim Strings
&lt;/h3&gt;

&lt;p&gt;We can make a string span multiple lines. A &lt;em&gt;verbatim string&lt;/em&gt; is a string that keeps the lines and spaces in a string as it is defined.&lt;/p&gt;

&lt;p&gt;To make a verbatim string, place the &lt;code&gt;@&lt;/code&gt; character in front of a string value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;multiLine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"This string spans 
multiple lines 
    and keeps the spaces too!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type the program above in a code box and run it. The string will print in multiple lines, even if we only used one &lt;code&gt;Console.WriteLine()&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnbwyz37ohrxowsr3pdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnbwyz37ohrxowsr3pdf.png" alt="Verbatim string output" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why would one use string interpolation over concatenation?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Find the error in this code and fix it. The output should be &lt;code&gt;I came, I saw, I conquered.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;action1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I came"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;action2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I saw"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;action3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I conquered."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;action1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;action2&lt;/span&gt; &lt;span class="s"&gt;", "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;action3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Copy the code in the question above and change it to use string interpolation. Replace &lt;code&gt;actions&lt;/code&gt; with a template string. The output should be &lt;code&gt;I came, I saw, I conquered.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make a Fill-In-The-Blanks story! &lt;/p&gt;

&lt;p&gt;Come up with a story that has 3 or 4 sentences. Replace a word in each sentence with a variable. Declare those variables with initial values and print out the complete story.&lt;/p&gt;

&lt;p&gt;Sample story:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;story&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"Three little _________,
They lost their __________,
And they began to ________."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 5: Variables</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Wed, 21 Jul 2021 02:43:30 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-5-variables-1pi4</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-5-variables-1pi4</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a variable?&lt;/li&gt;
&lt;li&gt;How do I declare a variable in C#?&lt;/li&gt;
&lt;li&gt;How do I assign a value to a variable in C#?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;When C# evaluates an expression and returns a new value, it would be nice if the value could be stored somewhere, so we could use it later in the program. Otherwise, we'd need to keep typing the same expression to get the value.&lt;/p&gt;

&lt;p&gt;In programming, we have a concept called a &lt;em&gt;variable&lt;/em&gt;. A variable is a named representation of some value. Variables can hold values of the type it was declared with. For example, a &lt;code&gt;string&lt;/code&gt; variable can contain values of the &lt;code&gt;string&lt;/code&gt; type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring Variables
&lt;/h3&gt;

&lt;p&gt;To declare a variable in C#, we need the type of the variable and a name to call it. We use the C# keyword in &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke"&gt;Lesson 3&lt;/a&gt; to specify the type of the variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The statement above is called a &lt;em&gt;declaration&lt;/em&gt; statement. We have declared a variable called &lt;code&gt;number&lt;/code&gt; that has the type of &lt;code&gt;int&lt;/code&gt;.  This variable can hold integer numbers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What's in a Name?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User-defined names in C# are called &lt;em&gt;identifiers&lt;/em&gt;. Identifiers must follow certain rules - for example, they must start with a letter or an underscore (&lt;code&gt;_&lt;/code&gt;). See &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names" rel="noopener noreferrer"&gt;this page&lt;/a&gt; for more information about identifiers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we declare a variable, we can also assign a value to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use this variable in a &lt;code&gt;Console.WriteLine()&lt;/code&gt; statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you guess what the output will be? That's right, the console will display &lt;code&gt;50&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfrkligo67mostswia9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfrkligo67mostswia9i.png" alt="Variable output" width="800" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we don't assign a value to a variable, like the first statement in this lesson, the variable will have a &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values" rel="noopener noreferrer"&gt;default value&lt;/a&gt;. For numeric types like &lt;code&gt;int&lt;/code&gt;, that value is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remove the  &lt;code&gt;= 50&lt;/code&gt; from the program above and run it. The value in the output will be &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Forwhoqs35bvauivhdwzz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Forwhoqs35bvauivhdwzz.png" alt="Default value of variable" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning to Variables
&lt;/h3&gt;

&lt;p&gt;Once you've declared a variable, we can assign a different value to it. The syntax will be similar to the declaration, except that we don't need to specify the type again. C# already knows its type from the declaration.&lt;/p&gt;

&lt;p&gt;We can also assign expressions to a variable, like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This statement is an &lt;em&gt;assignment&lt;/em&gt; statement. We have assigned a value to the &lt;code&gt;number&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Remember that C# will evaluate expressions like &lt;code&gt;5 + 20&lt;/code&gt; into its resulting value.&lt;/p&gt;

&lt;p&gt;Type the program below into your code box. Can you guess what the output will be?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you answered &lt;code&gt;25&lt;/code&gt;, you're correct! The program won't display &lt;code&gt;50&lt;/code&gt; because we assigned a new value to &lt;code&gt;number&lt;/code&gt; in Line 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5tv1uwwtom10ttq9q15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5tv1uwwtom10ttq9q15.png" alt="Assigning to a variable" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An important thing to remember is that once a variable has been declared with a certain type, that type can't be changed. The value can be changed, but only values of its declared type are acceptable.&lt;/p&gt;

&lt;p&gt;For example, we can't assign a &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;bool&lt;/code&gt; value to an &lt;code&gt;int&lt;/code&gt; variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Will it blend?"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This statement will cause an error!&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;anotherNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;anotherNumber&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// This statement will ALSO cause an error!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the var keyword
&lt;/h3&gt;

&lt;p&gt;There's another way to declare a variable in C#. Instead of specifying the type ourselves, we can let C# guess the type for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Full Metal Alchemist"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;var&lt;/code&gt; keyword is used to declare variables like &lt;code&gt;title&lt;/code&gt; above. But what is the data type of &lt;code&gt;title&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Because we have assigned the value &lt;code&gt;"Full Metal Alchemist"&lt;/code&gt; to it, which is a &lt;code&gt;string&lt;/code&gt; value, the type of &lt;code&gt;title&lt;/code&gt; is &lt;code&gt;string&lt;/code&gt;. Recall that strings are text values surrounded by double quotes (&lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke"&gt;Lesson 3&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Since C# needs an initial value in order to guess the type of the variable, using the &lt;code&gt;var&lt;/code&gt; keyword means we can't declare without an assigned value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myVariable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This will cause an error - C# doesn't know what type it is!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Why use the &lt;code&gt;var&lt;/code&gt; keyword at all? &lt;/p&gt;

&lt;p&gt;Some types in C# can get really long, like &lt;code&gt;Dictionary&amp;lt;string, string&amp;gt;&lt;/code&gt; and using &lt;code&gt;var&lt;/code&gt; instead of the type name will make our declarations more concise. Read more about the &lt;code&gt;var&lt;/code&gt; keyword &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What is the output of this program?&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;colour&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"blue"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;colour&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yellow"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;colour&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Questions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;True or False: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can assign a value of any type to a variable after it's been declared.&lt;/li&gt;
&lt;li&gt;When you declare a variable, you &lt;strong&gt;cannot&lt;/strong&gt; assign a value to it in the same statement.&lt;/li&gt;
&lt;li&gt;You can assign an expression to a variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Declare three variables with different types: &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, and &lt;code&gt;bool&lt;/code&gt;. Assign either a value or an expression to each variable. Then print each variable using &lt;code&gt;Console.WriteLine()&lt;/code&gt;. Did you get the output you were expecting?&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 4: Operators</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Sun, 18 Jul 2021 05:15:47 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-4-operators-4en2</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-4-operators-4en2</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is an operator?&lt;/li&gt;
&lt;li&gt;What is an expression?&lt;/li&gt;
&lt;li&gt;What are some of the commonly used operators in C#?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;In the &lt;a href="https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke"&gt;previous lesson&lt;/a&gt;, we mentioned that the type of a value can determine the &lt;em&gt;operations&lt;/em&gt; that can be performed with it.&lt;/p&gt;

&lt;p&gt;For example, we can add two integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; symbol between the two numbers is an &lt;em&gt;operator&lt;/em&gt;. It represents an action or operation that can be done to the values supplied with it. The &lt;code&gt;+&lt;/code&gt; operator here is the same used in arithmetic addition.&lt;/p&gt;

&lt;p&gt;The whole line above is called an &lt;em&gt;expression&lt;/em&gt;. An expression can be evaluated further by C# to produce a new value.&lt;/p&gt;

&lt;p&gt;Type the line into your code box and run the program. You'll see that C# evaluates the expression and displays the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3schuiwnw9rgzoeo2dm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3schuiwnw9rgzoeo2dm5.png" alt="10 + 2 evaluation" width="800" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why did it display the result even if we didn't call &lt;code&gt;Console.WriteLine()&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;Dotnet Interactive is also a &lt;a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="noopener noreferrer"&gt;REPL&lt;/a&gt; - an environment that allows quick evaluation of an expression, after which the output is displayed. &lt;/p&gt;

&lt;p&gt;If we write more than one statement in the code box, it won't be a single expression anymore and instead of doing the read-evaluate-print loop, Dotnet Interactive will compile the program as a whole.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also do subtraction, multiplication, and division - they each have their own operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;    &lt;span class="c1"&gt;// subtraction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;    &lt;span class="c1"&gt;// multiplication&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;    &lt;span class="c1"&gt;// division&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type each line above in the code box one at a time and run the program each time. The output will be the result of the arithmetic expression.&lt;/p&gt;

&lt;p&gt;If you try to write all of them at the same time and run the program, you'll get an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if we put semicolons at the end of each line, the program will still be invalid!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrd1t27hw8v27fontia8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrd1t27hw8v27fontia8.png" alt="Expressions are not valid statements" width="800" height="241"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;That's because expressions are not valid statements. As we learned in &lt;a href="//lesson-1-hello-world.md"&gt;Lesson 1&lt;/a&gt;, C# programs are made of statements. Expressions in C# programs must be used as part of a valid statement.&lt;/p&gt;

&lt;p&gt;What's a statement we've already learned? Why, the &lt;strong&gt;print&lt;/strong&gt; statement of course!&lt;/p&gt;

&lt;p&gt;Wrap each expression into a &lt;code&gt;Console.WriteLine()&lt;/code&gt; call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of each expression is now displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8v3rf3g01lubouvuxac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8v3rf3g01lubouvuxac.png" alt="Printing out each expression" width="800" height="230"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here are the common operators in C#.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arithmetic Operators
&lt;/h3&gt;

&lt;p&gt;These operators work with numeric values.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Addition&lt;/td&gt;
&lt;td&gt;&lt;code&gt;17 + 18&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Subtraction&lt;/td&gt;
&lt;td&gt;&lt;code&gt;65 - 21&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Multiplication&lt;/td&gt;
&lt;td&gt;&lt;code&gt;12 * 4&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Division&lt;/td&gt;
&lt;td&gt;&lt;code&gt;60 / 15&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remainder (modulo)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;20 % 3&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Comparison Operators
&lt;/h3&gt;

&lt;p&gt;These operators compare two values. The result will either be &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than&lt;/td&gt;
&lt;td&gt;&lt;code&gt;5 &amp;gt; 2&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1 &amp;lt; 7&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than or equal to&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9 &amp;gt;= 6&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than or equal to&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1000 &amp;lt; 84&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Equal to&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"apple" == "orange"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not equal to&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"apple" != "orange"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Logical Operators
&lt;/h3&gt;

&lt;p&gt;These operators perform &lt;a href="https://en.wikipedia.org/wiki/Boolean_algebra" rel="noopener noreferrer"&gt;Boolean logic operations&lt;/a&gt;. The result will either be &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Logical AND&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true &amp;amp;&amp;amp; false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;||&lt;/td&gt;
&lt;td&gt;Logical OR&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; || &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Logical NOT&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Is the following an expression? Why or why not?&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="s"&gt;"100 + 25"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;True or False: An expression by itself is a valid statement.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Wrap each example in the tables above into a &lt;code&gt;Console.WriteLine()&lt;/code&gt; statement. Can you guess what each result will be?&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 3: Data Types</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Thu, 15 Jul 2021 02:37:20 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-3-data-types-14ke</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a data type?&lt;/li&gt;
&lt;li&gt;What are some of the commonly used data types in C#?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;C# is a very meticulous language. Every piece of data it encounters must have a &lt;em&gt;type.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The type of the data determines the set of values that it can have, and also which &lt;em&gt;operations&lt;/em&gt; can be performed with it. Depending on the syntax, C# can sometimes infer the type of the data.&lt;/p&gt;

&lt;p&gt;Remember our &lt;a href="https://dev.to/coding_mama/c-for-beginners-introduction-5ami"&gt;Hello, World program&lt;/a&gt; earlier? We needed to enclose the text &lt;code&gt;Hello, World!&lt;/code&gt; in double quotes (&lt;code&gt;""&lt;/code&gt;) for it to be a valid value. Enclosing the text in double quotes made that entire piece of data be of the &lt;strong&gt;string&lt;/strong&gt; type.&lt;/p&gt;

&lt;p&gt;C# has many built-in data types. Some types have a special keyword in C#.  We'll learn where these keywords are used later.&lt;/p&gt;

&lt;p&gt;Here is a subset of the basic types you will likely encounter:&lt;/p&gt;

&lt;h3&gt;
  
  
  String
&lt;/h3&gt;

&lt;p&gt;The string type can contain text values. The text can have letters, numbers, and symbols.&lt;/p&gt;

&lt;p&gt;The length of the string can range from a single character to a whole paragraph - it can even be empty. They need to be enclosed in double quotes.&lt;/p&gt;

&lt;p&gt;C# keyword:  &lt;code&gt;string&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="s"&gt;"To you, 2000 years from now"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Integer
&lt;/h3&gt;

&lt;p&gt;The integer type can contain integer (whole number) values. Integers can have a negative value and can be written as-is.&lt;/p&gt;

&lt;p&gt;C# keyword:  &lt;code&gt;int&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;

&lt;p&gt;The boolean type has only two possible values, true or false. Booleans are used in conditional expressions (which we'll learn in another lesson.)&lt;/p&gt;

&lt;p&gt;C# keyword: &lt;code&gt;bool&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Character
&lt;/h3&gt;

&lt;p&gt;The character type can contain a single character. Characters can be letters, numbers, and symbols. They need to be enclosed in single quotes (&lt;code&gt;''&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;C# keyword: &lt;code&gt;char&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Floating-Point Numeric
&lt;/h3&gt;

&lt;p&gt;The floating-point numeric type can contain numbers with fractional values, represented in decimal format. Like integers, they can be written as-is.&lt;/p&gt;

&lt;p&gt;C# keyword: &lt;code&gt;double&lt;/code&gt; (short for double-precision floating-point)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="m"&gt;3.1416&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Console.WriteLine()&lt;/code&gt; doesn't just take string values. It can also print out values of other types.&lt;/p&gt;

&lt;p&gt;Try printing out each example above by calling &lt;code&gt;Console.WriteLine()&lt;/code&gt; and replacing the value in the round brackets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// String&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"To you, 2000 years from now"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Integer&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;99&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Boolean&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Character&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Floating-Point Numeric&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3.1416&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the program - you should see the values in the console output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfxzsc12apo8qxvhtr04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfxzsc12apo8qxvhtr04.png" alt="Printing out different data types" width="800" height="454"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What is the data type of each value below?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;"true"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt; &lt;code&gt;-24601&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1.38064852&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;' '&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Try to come up with your own values for each data type! Replace the code above with your values to print them out.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 2: Comments</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Tue, 13 Jul 2021 15:13:52 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-2-comments-2ea1</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-2-comments-2ea1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are comments? Why should I use them?&lt;/li&gt;
&lt;li&gt;How do I write comments in C#?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;Programs are written for computers, but sometimes we need to add more context to help other programmers read and understand our code. (Sometimes that programmer is ourselves!)&lt;/p&gt;

&lt;p&gt;To do this, we can write comments in our program. Comments are lines of text that are ignored by C# when the program is run.&lt;/p&gt;

&lt;p&gt;Good code is usually self-explanatory, but comments are useful to explain &lt;strong&gt;why&lt;/strong&gt; a program has been written a certain way.&lt;/p&gt;

&lt;p&gt;To write a comment, type &lt;code&gt;//&lt;/code&gt;, followed by the comment. Everything after the &lt;code&gt;//&lt;/code&gt; is ignored until the next line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// My first program&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the code above after typing it inside the code box. &lt;code&gt;My first program&lt;/code&gt; won't show up on the displayed output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8rwt4zvzoeofox6738p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8rwt4zvzoeofox6738p.png" alt="Comments" width="800" height="453"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can also write comments inline using &lt;code&gt;//&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// My first program&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this prints out a message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comments are also useful to take out bits of code you don't want to run. For example, if you're trying to find the cause of an error in your code (also called &lt;em&gt;debugging&lt;/em&gt;), you can comment out the code that you want to make sure is not causing the error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Yesterday it worked."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Today it is not working."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Console.WriteLine("This line will not print.");&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Coding is like that."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way to write comments is the multi-line comment. As the name suggests, you can use it to write comments that span multiple lines.&lt;/p&gt;

&lt;p&gt;The multi-line comment starts with &lt;code&gt;/*&lt;/code&gt; and ends with &lt;code&gt;*/&lt;/code&gt;. Everything between these two elements is ignored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My favourite dinosaurs:"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Triceratops"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Velociraptor"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
Console.WriteLine("Mosasaurus");
Console.WriteLine("Dimetrodon");
Console.WriteLine("Pteranodon");
*/&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Apatosaurus"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tyrannosaurus Rex"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add some comments to your answers to the Lesson 1 challenges!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# For Beginners - Lesson 1: Hello, World!</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Mon, 12 Jul 2021 15:14:32 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-lesson-1-hello-world-3gml</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-lesson-1-hello-world-3gml</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;At the end of this lesson, you should be able to answer the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a statement?&lt;/li&gt;
&lt;li&gt;How do I display a message on the screen using C#?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;p&gt;Let's make our program print a message. We're going to print out "Hello, World!" to the console. &lt;/p&gt;

&lt;p&gt;A console (also known as terminal) is a text-only display or environment that enables us to interact with the computer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Did you know?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" rel="noopener noreferrer"&gt;"Hello, World!" program&lt;/a&gt; is the traditional first program for programming languages. It is often the first program written by people who are learning to code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To make our C# program print a message, we'll use a command called &lt;code&gt;Console.WriteLine()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The message you want to display goes inside the round brackets &lt;code&gt;()&lt;/code&gt;. We also need to put the message inside double quotes, like this: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;"Hello, World!"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the code box on your notebook, type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of instruction is called a &lt;em&gt;statement&lt;/em&gt;. C# programs are made up of statements. &lt;/p&gt;

&lt;p&gt;Statements end with a semicolon (&lt;code&gt;;&lt;/code&gt;). Think of a statement like a sentence - the semicolon is equivalent to the full stop or period. &lt;/p&gt;

&lt;p&gt;If there were no full stops, the reader (in our case, the C# compiler) will have trouble understanding us, since it doesn't know where our sentences begin and end.&lt;/p&gt;

&lt;p&gt;Next, run the program by clicking on the Execute button on the code box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimlefcbfurepubz9mnoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimlefcbfurepubz9mnoq.png" alt="The location of the Execute button" width="800" height="155"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The output of the program will be shown at the bottom of the code box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9g68ozph836z2vtsyqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9g68ozph836z2vtsyqy.png" alt="Hello, World!" width="800" height="453"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations&lt;/strong&gt;! You have written and run your first C# program.&lt;/p&gt;

&lt;p&gt;Let's display another message. Write &lt;code&gt;I'm learning C#!&lt;/code&gt; to the console.&lt;/p&gt;

&lt;p&gt;After the first &lt;code&gt;Console.WriteLine()&lt;/code&gt;, press Enter to go to a new line and type the next statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm learning C#!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full program should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm learning C#!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the program again by clicking the Execute button.&lt;/p&gt;

&lt;p&gt;Your output should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzstwm40q9gw1o2etsx8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzstwm40q9gw1o2etsx8.png" alt="The output of your second program" width="800" height="453"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you may have noticed, statements run &lt;strong&gt;in sequence&lt;/strong&gt;. If you placed &lt;code&gt;I'm learning C#!&lt;/code&gt; before the &lt;code&gt;Hello World!&lt;/code&gt; statement, the output would be different.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You may have notice a box popping up while typing. This feature is part of &lt;a href="https://code.visualstudio.com/docs/editor/intellisense" rel="noopener noreferrer"&gt;&lt;strong&gt;IntelliSense&lt;/strong&gt;&lt;/a&gt;, a helpful tool for writing code. It provides hints, suggestions, and documentation for your code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9m0xd0lb61d9gy3vbnpn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9m0xd0lb61d9gy3vbnpn.png" alt="IntelliSense at work" width="800" height="160"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What happens when you remove the semicolon from the end of the statements? Will the program run?&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm learning C#!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Can you spot the error in this program? (If you're stuck, copy and paste this to the code box. The squiggly red lines will show you where the error is!)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Hello&lt;/span&gt; &lt;span class="n"&gt;World&lt;/span&gt;&lt;span class="p"&gt;!);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduce yourself to the computer! Display the message &lt;strong&gt;&lt;code&gt;Hi! My name is &amp;lt;NAME&amp;gt;&lt;/code&gt;&lt;/strong&gt;. Replace &lt;strong&gt;&lt;code&gt;&amp;lt;NAME&amp;gt;&lt;/code&gt;&lt;/strong&gt; with your name, of course.&lt;/li&gt;
&lt;li&gt;Print out your favourite haiku (Japanese poem) on the console. If you don't have one, use one of the examples &lt;a href="https://examples.yourdictionary.com/examples-of-haiku-poems.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Each line of the haiku should be printed on its own line.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C# For Beginners - Getting Started</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Sun, 11 Jul 2021 12:12:09 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-getting-started-4poa</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-getting-started-4poa</guid>
      <description>&lt;h2&gt;
  
  
  How The Tutorial Works
&lt;/h2&gt;

&lt;p&gt;The tutorial is divided into a number of lessons. Each lesson introduces new concepts, and sample code is displayed to demonstrate those concepts. &lt;/p&gt;

&lt;p&gt;If possible, try not to copy and paste the code. Instead, do your best to follow along by typing out each line. You will start building coding "muscle memory" this way.&lt;/p&gt;

&lt;p&gt;At the end of each lesson the tutorial will test your knowledge, with questions or challenges for you to complete yourself. Make sure that before you tackle them, you fully understand what the question or the challenge is looking for. &lt;/p&gt;

&lt;p&gt;Sometimes beginner programmers get stuck because they have gone through the material too fast, and accidentally formed an incorrect assumption because of a small error. &lt;/p&gt;

&lt;p&gt;If you find yourself unable to solve a challenge, try going through the lesson again. Don't be afraid to go slowly - you might have missed something small.  C# can be very strict, so you need to ensure that your code is precise and follows the exact structure expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Up Your Workspace
&lt;/h2&gt;

&lt;p&gt;You'll use &lt;a href="https://github.com/dotnet/interactive" rel="noopener noreferrer"&gt;Dotnet Interactive Notebooks&lt;/a&gt; as your workspace. It's an extension for Visual Studio Code (a code editor) that allows you to write and run C# code directly in the virtual notebook.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visual Studio Code and the Dotnet Interactive Notebooks extension can be downloaded as a single installer. Go to &lt;a href="https://dotnet.microsoft.com/learntocode" rel="noopener noreferrer"&gt;https://dotnet.microsoft.com/learntocode&lt;/a&gt; and download the ".NET Coding Pack".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Download and install &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk7v1hb87zigeal2k2md.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk7v1hb87zigeal2k2md.png" alt="Visual Studio Code website" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Launch Visual Studio Code. You'll see a screen similar to the one below.&lt;/p&gt;

&lt;p&gt;Open the Extensions panel by clicking the button on sidebar, or from the menu bar by clicking on &lt;em&gt;View&lt;/em&gt; then selecting &lt;em&gt;Extensions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4z3akfq7s1xg7bai0a9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4z3akfq7s1xg7bai0a9.png" alt="Visual Studio Code, showing the Extensions panel button" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Type ".NET Interactive Notebooks" in the search bar on the Extensions panel. Click on the Install button to install the extension.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn4bd3gkivp4xbxyug9ph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn4bd3gkivp4xbxyug9ph.png" alt=".NET Interactive Notebooks in Extensions" width="800" height="453"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Create a new Notebook by opening the Command Palette (Ctrl + Shift + P in Windows, Cmd + Shift + P on Mac) and selecting ".NET Interactive: Create new blank notebook".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can also use the keyboard shortcut Ctrl + Shift + Alt + N (Windows) or Cmd + Shift + Alt + N (Mac) to create the notebook.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdem9p945rsiewr27lj99.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdem9p945rsiewr27lj99.png" alt="Create new blank notebook" width="800" height="246"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Select "Create as .dib" in the next section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1e4ladm4b2yuki6ptyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1e4ladm4b2yuki6ptyy.png" alt="Create as .dib" width="800" height="115"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next, select "C#" as the language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzwkjya37arpbvx25jnd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzwkjya37arpbvx25jnd.png" alt="Select C# as the language" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The .NET Interactive files will download if they haven't been installed yet.&lt;/p&gt;

&lt;p&gt;When you see this screen, your notebook is ready! Write your C# code in the box. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47znuste89a43f7jzqtx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47znuste89a43f7jzqtx.png" alt="Dotnet Interactive is ready" width="800" height="229"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;To run the code, click on the Execute button on the left-hand side, or press Ctrl + Alt + Enter (Windows) / Cmd + Alt + Enter (Mac).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ijt4i8dtkweptt7fuf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ijt4i8dtkweptt7fuf1.png" alt="How to execute the code in the code box" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>vscode</category>
      <category>dotnet</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# For Beginners - Introduction</title>
      <dc:creator>Kristina (Coding Mama)</dc:creator>
      <pubDate>Sun, 11 Jul 2021 11:59:31 +0000</pubDate>
      <link>https://dev.to/coding_mama/c-for-beginners-introduction-5ami</link>
      <guid>https://dev.to/coding_mama/c-for-beginners-introduction-5ami</guid>
      <description>&lt;p&gt;Hi there! This tutorial is a gentle introduction to the C# programming language. &lt;/p&gt;

&lt;p&gt;You'll be learning the basics of C# and programming by following along with the tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is programming?
&lt;/h2&gt;

&lt;p&gt;Programming is a way to tell computers to do things. &lt;/p&gt;

&lt;p&gt;Computers are very fast at doing things - whether it's calculating complex formulas or finding duplicates in a list with a thousand entries. &lt;/p&gt;

&lt;p&gt;But a computer can't do these things by itself. To instruct it to solve a problem, someone else needs to come in, tell the computer to solve the problem and also, &lt;strong&gt;how&lt;/strong&gt; to solve it.&lt;/p&gt;

&lt;p&gt;This "someone" is called a &lt;em&gt;programmer&lt;/em&gt;. A programmer writes instructions for a computer so that it can solve the problem. But a computer - a machine - can't understand human language. In fact, a computer can only understand &lt;em&gt;machine language&lt;/em&gt; - a language that uses only 1s and 0s.&lt;/p&gt;

&lt;p&gt;It's possible to write a set of instructions in 1s and 0s, but as humans, this will get really complicated quickly. Not to mention, doing it this way would probably take a human programmer a very long time.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;High-level&lt;/em&gt; &lt;em&gt;programming languages&lt;/em&gt; were created to solve this issue. These programming languages use human-readable elements, but these elements have special meaning as well. They can be translated, either by an &lt;em&gt;interpreter&lt;/em&gt; or a &lt;em&gt;compiler&lt;/em&gt; into machine code, which the computer can now understand.&lt;/p&gt;

&lt;p&gt;C# is one example of a &lt;em&gt;high-level programming language&lt;/em&gt;. It was created by Microsoft in 2000 and is one of the top 5 programming languages in use today.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why C#?
&lt;/h2&gt;

&lt;p&gt;C# is a modern, general-purpose programming language. It's a versatile language that is used to build different types of applications.&lt;/p&gt;

&lt;p&gt;Desktop apps, mobile apps, websites, and games are some of the things that are currently being built using C#. Once you've learnt the basics of C#, you can move on to building all kinds of programs!&lt;/p&gt;

&lt;p&gt;C# syntax (the structure of the language) is easy to read. It's part of the C family of languages, so learning it will mean you can easily pick up languages like C, Java, JavaScript, and Swift.&lt;/p&gt;

&lt;p&gt;C# is a powerful language and allows you to build both simple and complex programs.&lt;/p&gt;

&lt;p&gt;Lastly, C# is a popular language, especially in the enterprise sector. Learning C# means you've got a skill that is highly sought by many industries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;A big thank you to &lt;a href="https://www.nodegirls.com/" rel="noopener noreferrer"&gt;NodeGirls&lt;/a&gt;! This tutorial was heavily inspired by their &lt;a href="https://node-girls.gitbook.io/beginners-javascript/tutorial/step-1-hello-world" rel="noopener noreferrer"&gt;Beginners JavaScript&lt;/a&gt; tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  About Me
&lt;/h2&gt;

&lt;p&gt;Hi, I'm Coding Mama! I'm a Senior Software Engineer who loves to work with C#, so I've made it a personal goal to introduce C# to as many people as possible.&lt;/p&gt;

&lt;p&gt;Whether you're an absolute beginner to programming, or a developer who wants to learn C#, I'm here to help!&lt;/p&gt;

&lt;p&gt;I hope you find my tutorials useful! If you do, please drop me a note on &lt;a href="https://twitter.com/coding_mama" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or &lt;a href="https://dev.to/coding_mama"&gt;Dev.to&lt;/a&gt; - I'd love to hear from you!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
