<?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: Codebuns</title>
    <description>The latest articles on DEV Community by Codebuns (@codebuns).</description>
    <link>https://dev.to/codebuns</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%2F260630%2F4eecbb72-a103-414d-9a62-a9a014be36f4.png</url>
      <title>DEV Community: Codebuns</title>
      <link>https://dev.to/codebuns</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codebuns"/>
    <language>en</language>
    <item>
      <title>C# Arrays (With STEP-BY-STEP Videos)</title>
      <dc:creator>Codebuns</dc:creator>
      <pubDate>Sun, 12 Mar 2023 15:12:04 +0000</pubDate>
      <link>https://dev.to/codebuns/c-arrays-with-step-by-step-video-3lp8</link>
      <guid>https://dev.to/codebuns/c-arrays-with-step-by-step-video-3lp8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zpn9R1ac--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2023/03/arrays-1024x592.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zpn9R1ac--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2023/03/arrays-1024x592.jpg" alt="C# Arrays" title="C# Arrays" width="880" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;C# Arrays tutorial has related videos. Watch it with the written material for a &lt;strong&gt;STEP-BY-STEP explanation&lt;/strong&gt; with &lt;strong&gt;CODE examples&lt;/strong&gt;. Videos will reduce your learning curve and deepen your understanding where you’re uncertain: &lt;a href="https://codebuns.com/csharp-intermediate/single-dimensional-array/"&gt;C# Single Dimensional Array&lt;/a&gt; | &lt;a href="https://codebuns.com/csharp-intermediate/rectangular-array/"&gt;C# Rectangular Array&lt;/a&gt; | &lt;a href="https://codebuns.com/csharp-intermediate/jagged-array/"&gt;C# Jagged Array&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What Is an Array?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Array is a data structure that stores multiple values of the same type in a single variable&lt;/strong&gt;. You access each individual value through an integer index with an array name. Arrays can be of different dimensions like Single-dimension, Multi-dimension or Jagged array. The simplest of all is Single dimension, so you’ll start learning how to declare, initialize, and use single dimensional array.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Single-Dimensional Array?
&lt;/h2&gt;

&lt;p&gt;The below image shows a &lt;strong&gt;C# single-dimensional array&lt;/strong&gt; of eight integers, which contains array elements from 0 to 7. Array’s smallest and largest indexes are called its lower and upper bound, respectively. The lower bound is always 0, and the upper bound is always one less than the length of the array (&lt;strong&gt;arrayLength – 1&lt;/strong&gt;). That means if an array has eight elements, their indices are &lt;strong&gt;0&lt;/strong&gt;, &lt;strong&gt;1&lt;/strong&gt;, &lt;strong&gt;2&lt;/strong&gt;, &lt;strong&gt;3&lt;/strong&gt;, &lt;strong&gt;4&lt;/strong&gt;, &lt;strong&gt;5&lt;/strong&gt;, &lt;strong&gt;6&lt;/strong&gt;, and &lt;strong&gt;7&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oJGhg8jn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2018/10/single-dimension-array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oJGhg8jn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2018/10/single-dimension-array.png" alt="C# Single-Dimensional Array" title="C# Single-Dimensional Array" width="555" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Arrays also have &lt;strong&gt;GetLowerBound&lt;/strong&gt; and &lt;strong&gt;GetUpperBound&lt;/strong&gt; methods that return the lower and upper bounds for a particular dimension in an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;if you use an index that’s greater than the upper bound, an &lt;strong&gt;IndexOutOfRangeException&lt;/strong&gt; will be thrown&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/aMA49_rlaXM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Edit and test all the CODE examples used in this article with C# online editor: &lt;a href="https://codebuns.com/csharp/csharp-intermediate-code/#single-dimensional-array/"&gt;Single-Dimensional Array Code Playground&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Declare and Initialize One-Dimensional Array in C#.
&lt;/h3&gt;

&lt;p&gt;You can create an array using either one or two statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Two Statements
&lt;/h4&gt;

&lt;p&gt;An array is declared by defining the type of elements inside the array, followed by empty square brackets &lt;strong&gt;[ ]&lt;/strong&gt; and a variable name.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[] arrayName; // declaration: initial value is null&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;integerValues&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://res.cloudinary.com/practicaldev/image/fetch/s--14TGhYTJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/1-Single-Dimensional-Array-Declaration.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--14TGhYTJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/1-Single-Dimensional-Array-Declaration.png" alt="1-Single-Dimensional-Array-Declaration" title="1-Single-Dimensional-Array-Declaration" width="364" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, the above statement only declares the variable. It does not yet initialize an array. You do the initialization part with the &lt;strong&gt;new&lt;/strong&gt; keyword, followed by the data type and a second set of square brackets containing the length or size of the array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName = new type[arrayLength]; // initialization: Creates an array object.&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;integerValues&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;4&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://res.cloudinary.com/practicaldev/image/fetch/s--rwUMOtJH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/2-Single-Dimensional-Array-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rwUMOtJH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/2-Single-Dimensional-Array-Initialization.png" alt="2-Single-Dimensional-Array-Initialization" title="2-Single-Dimensional-Array-Initialization" width="366" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Using One Statement
&lt;/h4&gt;

&lt;p&gt;Declaration and initialization of an array simultaneously on a single line.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[] arrayName = new type[arrayLength]; // declaration &amp;amp; initialization&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;integerValues&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;4&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://res.cloudinary.com/practicaldev/image/fetch/s--RNSDvIad--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/2.1-Single-Dimensional-Array-Declaration-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RNSDvIad--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/2.1-Single-Dimensional-Array-Declaration-Initialization.png" alt="2.1-Single-Dimensional-Array-Declaration-Initialization" title="2.1-Single-Dimensional-Array-Declaration-Initialization" width="366" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create and Assign Values to One-Dimensional Array
&lt;/h3&gt;

&lt;p&gt;You can reference array elements using an index inside square brackets, one at a time, and then assign values, as shown below. The index of the first element always starts with 0.&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;integerValues&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;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;integerValues&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="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;integerValues&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;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;integerValues&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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;integerValues&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;4&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://res.cloudinary.com/practicaldev/image/fetch/s--AG-Yj57q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/3-Single-Dimensional-Array-Initialization-With-Value.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AG-Yj57q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/3-Single-Dimensional-Array-Initialization-With-Value.png" alt="3-Single-Dimensional-Array-Initialization-With-Value" title="3-Single-Dimensional-Array-Initialization-With-Value" width="361" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use another method, which is quite convenient. With this method, values can be assigned simultaneously using &lt;strong&gt;CURLY BRACKET {…} NOTATION&lt;/strong&gt;. This syntax is valid when creating an array of a known size and wants to specify the initial values quickly.&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;integerValues&lt;/span&gt; &lt;span class="p"&gt;=&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;span class="m"&gt;2&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="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the &lt;strong&gt;var&lt;/strong&gt; keyword with an array to declare a variable that infers its type from its assigned value. Use &lt;strong&gt;new&lt;/strong&gt; keyword when using this approach.&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;integerValues&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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;span class="m"&gt;2&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="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Update One-Dimensional Array Value
&lt;/h3&gt;

&lt;p&gt;You can access a value of an indexed array element by using an indexer (&lt;strong&gt;[ ]&lt;/strong&gt;) and then 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="n"&gt;integerValues&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;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;Above statement assigns a value &lt;strong&gt;20&lt;/strong&gt; to the 2nd column with an index (&lt;strong&gt;[1]&lt;/strong&gt;), which updates the value from &lt;strong&gt;2&lt;/strong&gt; to &lt;strong&gt;20&lt;/strong&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;integerValues&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;40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement assigns a value of &lt;strong&gt;40&lt;/strong&gt; to the 4th column with an index (&lt;strong&gt;[3]&lt;/strong&gt;), which updates the value from &lt;strong&gt;4&lt;/strong&gt; to &lt;strong&gt;40&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---cMw0i1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/6-Single-Dimensional-Array-Set-Individual-Element.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---cMw0i1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/6-Single-Dimensional-Array-Set-Individual-Element.png" alt="6-Single-Dimensional-Array-Set-Individual-Element" title="6-Single-Dimensional-Array-Set-Individual-Element" width="628" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Access One-Dimensional Array Values
&lt;/h3&gt;

&lt;p&gt;You can retrieve a specific value in the array using an index position inside the square brackets &lt;strong&gt;[ ]&lt;/strong&gt;. Access array elements using 0, 1, 2, and 3 indexes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName[index];&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;integerValues&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;span class="c1"&gt;// return 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement gets the value 2 from the 2nd column with an index &lt;strong&gt;[1]&lt;/strong&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;integerValues&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="c1"&gt;// return 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement gets the value 4 from the 4th column with an index &lt;strong&gt;[3]&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fqzkHYFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/5-Single-Dimensional-Array-Get-Individual-Element.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fqzkHYFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codebuns.com/wp-content/uploads/2019/05/5-Single-Dimensional-Array-Get-Individual-Element.png" alt="5-Single-Dimensional-Array-Get-Individual-Element" title="5-Single-Dimensional-Array-Get-Individual-Element" width="377" height="221"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;There are two kinds of multi-dimensional arrays: &lt;a href="https://codebuns.com/csharp-intermediate/rectangular-array/"&gt;Rectangular&lt;/a&gt; and &lt;a href="https://www.codebuns.com/csharp-intermediate/jagged-array/"&gt;Jagged&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Rectangular Array in C#?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;C# rectangular array is an array of two (or more) dimensions separated by a COMMA. You can think of a rectangular array as a table, where the first dimension is the number of rows, and the second dimension is the number of columns keeping in mind that every row is the same length&lt;/em&gt;&lt;/strong&gt;. Due to 2 dimensions, it’s also known as a two-dimensional or 2D array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MHLdA3iq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/1-Rectangular-Array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MHLdA3iq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/1-Rectangular-Array.png" alt="1-Rectangular-Array" title="1-Rectangular-Array" width="250" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ADt-33O9uP4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Edit and test all the CODE examples used in this article with C# online editor: &lt;a href="https://codebuns.com/csharp/csharp-intermediate-code/#rectangular-array"&gt;Rectangular Array Code Playground&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Declare and Initialize Rectangular Array in C#.
&lt;/h3&gt;

&lt;p&gt;You can create an array using either one or two statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Two Statements
&lt;/h4&gt;

&lt;p&gt;On the left side of the declaration, you need a &lt;strong&gt;data type&lt;/strong&gt;, the type of values in the array like it will hold integer values, decimal, string etc. Followed by &lt;strong&gt;COMMA&lt;/strong&gt; (&lt;strong&gt;,&lt;/strong&gt;) within the set of &lt;strong&gt;BRACKETS&lt;/strong&gt;(&lt;strong&gt;[ ]&lt;/strong&gt;). The comma indicates that the array has two dimensions. Two or more commas would indicate three or more dimensions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[,] arrayName;&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;intArray&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://res.cloudinary.com/practicaldev/image/fetch/s--TVWfFIdJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/1-Rectangular-Array-Declaration.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TVWfFIdJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/1-Rectangular-Array-Declaration.png" alt="1-Rectangular-Array-Declaration" title="1-Rectangular-Array-Declaration" width="332" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;new&lt;/strong&gt; keyword creates an array on the right side of instantiation. You specify the number of &lt;strong&gt;ROWS&lt;/strong&gt; in the array, followed by a &lt;strong&gt;COMMA&lt;/strong&gt;, and then the number of &lt;strong&gt;COLUMNS&lt;/strong&gt; to set the size of each dimension in the array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName = new type[rows, columns]; // 2D array&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;type[,]&lt;/code&gt; indicates &lt;strong&gt;2D array&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;type[rows, columns]&lt;/code&gt; indicates size of &lt;strong&gt;ROW&lt;/strong&gt; and &lt;strong&gt;COLUMN&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;intArray&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="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;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nkGM_NME--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/2.1-Rectangular-3x2-array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nkGM_NME--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/2.1-Rectangular-3x2-array.png" alt="2.1-Rectangular-3x2-array" title="2.1-Rectangular-3x2-array" width="312" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sB76LSXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/2-Rectangular-Array-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sB76LSXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/2-Rectangular-Array-Initialization.png" alt="2-Rectangular-Array-Initialization" title="2-Rectangular-Array-Initialization" width="639" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;Multi-dimensional array is indexed by two or more integers&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using One Statement
&lt;/h4&gt;

&lt;p&gt;Declaration and initialization of an array simultaneously on a single line.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[,] arrayName = new type[rows, columns];&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;intArray&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="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;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fZKZOwlk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/3-Rectangular-Array-Declaration-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fZKZOwlk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/3-Rectangular-Array-Declaration-Initialization.png" alt="3-Rectangular-Array-Declaration-Initialization" title="3-Rectangular-Array-Declaration-Initialization" width="639" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create and Assign Values to a Rectangular Array
&lt;/h3&gt;

&lt;p&gt;Array elements can be referenced using two integers with an indexer, one statement at a time to assign values to the elements of the &lt;strong&gt;intArray&lt;/strong&gt; as shown 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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[,]&lt;/span&gt; &lt;span class="n"&gt;intArray&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="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;intArray&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="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="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&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;intArray&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;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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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;0&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;intArray&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;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s a shorter form 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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[,]&lt;/span&gt; &lt;span class="n"&gt;intArray&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="m"&gt;1&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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;},&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;6&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://dotnetfiddle.net/iD04hE"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Access Individual Rectangular Array Values
&lt;/h3&gt;

&lt;p&gt;You refer array element using its &lt;strong&gt;ROW&lt;/strong&gt; and &lt;strong&gt;COLUMN&lt;/strong&gt; index because each element of multi-dimensional array is identified with an index for each dimension.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName[rowIndex, columnIndex]&lt;/code&gt;&lt;br&gt;
You access each array element using two integers with an indexer.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example
&lt;/h4&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;intArray&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MdNRVgV3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/4-Rectangular-Array-Get-Value-By-Index.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MdNRVgV3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codebuns.com/wp-content/uploads/2019/05/4-Rectangular-Array-Get-Value-By-Index.png" alt="4-Rectangular-Array-Get-Value-By-Index" title="4-Rectangular-Array-Get-Value-By-Index" width="639" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/70LErJ"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Accessing Rectangular Array Values Using Nested for Loops
&lt;/h3&gt;

&lt;p&gt;Never hardcode the total number of elements when iterating through the array. Always use properties or methods such as &lt;strong&gt;Length&lt;/strong&gt; and &lt;strong&gt;GetLength&lt;/strong&gt; to determine the total number of iterations needed. Let’s look at the following examples.&lt;/p&gt;
&lt;h4&gt;
  
  
  3 by 2 Array Example
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&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;intArray&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="m"&gt;1&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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;},&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;6&lt;/span&gt; &lt;span class="p"&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;row&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;row&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetLength&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;row&lt;/span&gt;&lt;span class="p"&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;column&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;column&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetLength&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;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//Console.WriteLine(intArray[row, column]);&lt;/span&gt;
        &lt;span class="c1"&gt;// OR&lt;/span&gt;
        &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"\n"&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;display&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://dotnetfiddle.net/m4EyjP"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  OUTPUT
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;1 2&lt;br&gt;
3 4&lt;br&gt;
5 6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First, declared and initialized rectangular &lt;strong&gt;intArray&lt;/strong&gt; variable. On the left side of the declaration, a single &lt;strong&gt;COMMA&lt;/strong&gt;(,) within square brackets indicate that the array is two-dimensional, where the dimensions are &lt;strong&gt;3 × 2&lt;/strong&gt; (3 &lt;strong&gt;ROWS&lt;/strong&gt; and 2 &lt;strong&gt;COLUMNS&lt;/strong&gt;). The array is two-dimensional, so you need to use 2 separate &lt;strong&gt;&lt;em&gt;for&lt;/em&gt; loops&lt;/strong&gt; to iterate through each dimension. Outer for loop is to get the number of &lt;strong&gt;ROWS&lt;/strong&gt; by calling the &lt;strong&gt;GetLength&lt;/strong&gt; method and passing &lt;strong&gt;0&lt;/strong&gt; for the &lt;strong&gt;1st dimension&lt;/strong&gt; and &lt;strong&gt;nested &lt;em&gt;for&lt;/em&gt; loop&lt;/strong&gt;, which brings the number of &lt;strong&gt;COLUMNS&lt;/strong&gt; for each row by passing &lt;strong&gt;1&lt;/strong&gt; for the length of the &lt;strong&gt;2nd dimension&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The line &lt;code&gt;Console.WriteLine(intArray[row, column]);&lt;/code&gt; prints each element to the console. When retrieving the individual value from an array element, you use variable &lt;strong&gt;intArray&lt;/strong&gt; with the loop counter row for &lt;strong&gt;rowIndex&lt;/strong&gt; and column for &lt;strong&gt;columnIndex&lt;/strong&gt; within square brackets &lt;strong&gt;[ ]&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  What Is a Jagged Array in C#?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;C# jagged array is an array of arrays, which means it’s an array that contains other arrays (inner arrays for clarity). Of course, such inner arrays can have different lengths or even be not initialized. Think of a table with rows of unequal lengths&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you create a jagged array, you declare the number of &lt;strong&gt;fixed rows&lt;/strong&gt;. Then, each row can have a different number of columns. Specifically, &lt;strong&gt;jagged&lt;/strong&gt; is a single-dimensional array, with each row holding another array of dimensions different dimensions 😊.&lt;/p&gt;

&lt;p&gt;If you look at the following example, you will see a jagged array with three rows/elements. &lt;strong&gt;ROW 0&lt;/strong&gt; has two columns, &lt;strong&gt;ROW 1&lt;/strong&gt; has four columns, while the last &lt;strong&gt;ROW 2&lt;/strong&gt; has three columns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FuPga2jr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/C-Jagged-Array.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FuPga2jr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/C-Jagged-Array.gif" alt="C-Jagged-Array" title="C-Jagged-Array" width="650" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The elements of the jagged array can be &lt;strong&gt;one-dimensional&lt;/strong&gt; and &lt;strong&gt;multi-dimensional arrays&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/S1CyNBGZ3eo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;_ Edit and test all the CODE examples used in this article with C# online editor: &lt;a href="https://codebuns.com/csharp/csharp-intermediate-code/#jagged-array"&gt;Jagged Array Code Playground&lt;/a&gt;_&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Declare and Initialize a Jagged Array in C#.
&lt;/h3&gt;

&lt;p&gt;You can create an array using either one or two statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Two Statements
&lt;/h4&gt;

&lt;p&gt;To declare a jagged array, we use two sets of square brackets in the array’s declaration. The notation (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) is used after the data type to represent the number of dimensions. Here is an example of a jagged array declaration:&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[ ][ ] arrayName;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JjCwedWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Declare-Jagged-Array-Array-of-Arrays.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JjCwedWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Declare-Jagged-Array-Array-of-Arrays.gif" alt="Syntax-to-Declare-Jagged-Array-Array-of-Arrays" title="Syntax-to-Declare-Jagged-Array-Array-of-Arrays" width="300" height="88"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only the size determining the number of &lt;strong&gt;ROWS&lt;/strong&gt; in the first pair of brackets is used to initialize the jagged array. Also, notice that the second bracket that defines the number of elements inside the row is &lt;strong&gt;EMPTY&lt;/strong&gt; because every row has a different number of columns. The column sizes must be set individually.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;arrayName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rows&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://res.cloudinary.com/practicaldev/image/fetch/s--ueZC3iq8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Initialize-Jagged-Array-Array-of-Arrays.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ueZC3iq8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Initialize-Jagged-Array-Array-of-Arrays.gif" alt="Syntax-to-Initialize-Jagged-Array-Array-of-Arrays" title="Syntax-to-Initialize-Jagged-Array-Array-of-Arrays" width="300" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Using One Statement
&lt;/h4&gt;

&lt;p&gt;Another way is to declare and initialize an array on a single line simultaneously.&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;type[ ][ ] arrayName = new type[rows][ ];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vGSyJmor--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Declare-and-Initialize-Jagged-Array-Array-of-Arrays.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vGSyJmor--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Syntax-to-Declare-and-Initialize-Jagged-Array-Array-of-Arrays.gif" alt="Syntax-to-Declare-and-Initialize-Jagged-Array-Array-of-Arrays" title="Syntax-to-Declare-and-Initialize-Jagged-Array-Array-of-Arrays" width="300" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a Jagged Array
&lt;/h3&gt;

&lt;p&gt;The below line creates &lt;strong&gt;fruits&lt;/strong&gt; with &lt;strong&gt;two elements&lt;/strong&gt;, each of type &lt;strong&gt;string[]&lt;/strong&gt; and with an initial null 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;string&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;fruits&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;2&lt;/span&gt;&lt;span class="p"&gt;][];&lt;/span&gt; &lt;span class="c1"&gt;// correct&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oL4ua8Aw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Declare-Jagged-Array-of-string.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oL4ua8Aw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Declare-Jagged-Array-of-string.gif" alt="Declare-Jagged-Array-of-string" title="Declare-Jagged-Array-of-string" width="650" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;string[2][ ]&lt;/code&gt; indicates a jagged array has 2 rows, and the number of columns is unclear.&lt;/p&gt;

&lt;p&gt;When you use the &lt;strong&gt;new&lt;/strong&gt; keyword to create the jagged array, you specify the number of rows that the array will contain.&lt;/p&gt;

&lt;p&gt;The following statement is a &lt;strong&gt;syntax error&lt;/strong&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;string&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;fruits&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;2&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="c1"&gt;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A jagged array cannot be created entirely with a single statement. Instead, you need to initialize the elements of a jagged array separately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assign Values to a Jagged Array
&lt;/h3&gt;

&lt;p&gt;Once the array is declared, you need to construct each row and fill it with data. There are two ways to allocate the elements—one per row or using an &lt;strong&gt;initialization list&lt;/strong&gt; to assign data simultaneously.&lt;/p&gt;

&lt;p&gt;Using the &lt;strong&gt;first method&lt;/strong&gt;, you can write:&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;fruits&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="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;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;fruits&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;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;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above lines initialize the two elements of &lt;strong&gt;fruits&lt;/strong&gt; with individual array instances of different dimensions—one statement at a time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XNHwQ0Lw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Declare-And-Initialize-Jagged-Array-of-string.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XNHwQ0Lw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Declare-And-Initialize-Jagged-Array-of-string.gif" alt="Declare-And-Initialize-Jagged-Array-of-string" title="Declare-And-Initialize-Jagged-Array-of-string" width="650" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fruits[0] = new string[2]&lt;/code&gt; indicates row &lt;strong&gt;0&lt;/strong&gt; has &lt;strong&gt;2&lt;/strong&gt; columns.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fruits[1] = new string[3]&lt;/code&gt; indicates row &lt;strong&gt;1&lt;/strong&gt; has &lt;strong&gt;3&lt;/strong&gt; columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at the index values for the above array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AFAVq9-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Index-Values-of-a-Jagged-Array-of-string.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AFAVq9-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Index-Values-of-a-Jagged-Array-of-string.gif" alt="Index-Values-of-a-Jagged-Array-of-string" title="Index-Values-of-a-Jagged-Array-of-string" width="650" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can access an indexed array element, which is part of the jagged array using two indexers (&lt;strong&gt;[rowIndex] [columnIndex]&lt;/strong&gt;), and then assign a value to it with the assignment operator (&lt;strong&gt;=&lt;/strong&gt;). Look at the following statements.&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;fruits&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="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="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;fruits&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="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;fruits&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;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="s"&gt;"Mango"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;fruits&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;fruits&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;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="s"&gt;"Melon"&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://res.cloudinary.com/practicaldev/image/fetch/s--pQmHF_zI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Assign-values-to-the-Indexes-of-a-Jagged-Array-of-string.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pQmHF_zI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Assign-values-to-the-Indexes-of-a-Jagged-Array-of-string.gif" alt="Assign-values-to-the-Indexes-of-a-Jagged-Array-of-string" title="Assign-values-to-the-Indexes-of-a-Jagged-Array-of-string" width="880" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fruits[0][0] = “Apple”&lt;/code&gt; indicates value &lt;strong&gt;Apple&lt;/strong&gt; assigned to the 1st column &lt;strong&gt;[0]&lt;/strong&gt; of the 1st row &lt;strong&gt;[0]&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;fruits[0][1] = “Apricot”&lt;/code&gt; indicates value &lt;strong&gt;Apricot&lt;/strong&gt; assigned to the 2nd column &lt;strong&gt;[1]&lt;/strong&gt; of the 1st row &lt;strong&gt;[0]&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;fruits[1][0] = “Mango”&lt;/code&gt; indicates value &lt;strong&gt;Mango&lt;/strong&gt; assigned to the 1st column &lt;strong&gt;[0]&lt;/strong&gt; of the 2nd row &lt;strong&gt;[1]&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;fruits[1][1] = “Orange”&lt;/code&gt; indicates value &lt;strong&gt;Orange&lt;/strong&gt; assigned to the 2nd column &lt;strong&gt;[1]&lt;/strong&gt; of the 2nd row &lt;strong&gt;[1]&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;fruits[1][2] = “Melon”&lt;/code&gt; indicates value &lt;strong&gt;Melon&lt;/strong&gt; assigned to the 3rd column &lt;strong&gt;[2]&lt;/strong&gt; of the 2nd row &lt;strong&gt;[1]&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another way that allows initial values of the array elements to be assigned is with the &lt;strong&gt;new&lt;/strong&gt; operator using an &lt;strong&gt;array initializer&lt;/strong&gt;, a list of values written between the curly bracket &lt;strong&gt;{&lt;/strong&gt; and &lt;strong&gt;}&lt;/strong&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;fruits&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="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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;fruits&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;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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Mango"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Melon"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code adds three separate one-dimensional arrays—one statement at a time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first element &lt;strong&gt;[0]&lt;/strong&gt;,which is an &lt;strong&gt;string&lt;/strong&gt; array with two columns. The columns are initialized with the values &lt;strong&gt;Apple&lt;/strong&gt; and &lt;strong&gt;Apricot&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The second element &lt;strong&gt;[1]&lt;/strong&gt;,which is an &lt;strong&gt;string&lt;/strong&gt; array with three columns. The columns are initialized with the values &lt;strong&gt;Mango&lt;/strong&gt;, &lt;strong&gt;Orange&lt;/strong&gt; and &lt;strong&gt;Melon&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;strong&gt;second method&lt;/strong&gt;, you can use a modified form of the above and assign values simultaneously using an array initializer.&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;string&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="p"&gt;=&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Mango"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Melon"&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;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The length of the array is inferred from the number of values between the delimiters &lt;strong&gt;{&lt;/strong&gt; … &lt;strong&gt;}&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Another approach is to use the &lt;strong&gt;var&lt;/strong&gt; keyword. This keyword instructs the compiler to implicitly type a local 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;var&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Mango"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Melon"&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://dotnetfiddle.net/qgvGhL"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Access Individual Values of a Jagged Array
&lt;/h3&gt;

&lt;p&gt;You refer to each element of the array using it's &lt;strong&gt;ROW&lt;/strong&gt; and &lt;strong&gt;COLUMN&lt;/strong&gt; index with two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName[rowIndex][columnIndex]&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;fruits&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;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;&lt;strong&gt;Let me repeat this.&lt;/strong&gt;&lt;br&gt;
You enclose the &lt;strong&gt;ROW&lt;/strong&gt; and &lt;strong&gt;COLUMN&lt;/strong&gt; index in their brackets to access an item stored at a particular row and column in a jagged array. For example, the above &lt;strong&gt;fruits[1][1]&lt;/strong&gt; statement displays the value stored at &lt;strong&gt;ROW 1&lt;/strong&gt;, &lt;strong&gt;COLUMN 1&lt;/strong&gt;, of the &lt;strong&gt;fruits&lt;/strong&gt;, as shown here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---BqRcrPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Access-Individual-Elements-of-a-Jagged-Array.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---BqRcrPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Access-Individual-Elements-of-a-Jagged-Array.gif" alt="Access-Individual-Elements-of-a-Jagged-Array" title="Access-Individual-Elements-of-a-Jagged-Array" width="880" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Access the whole row of data using the following syntax.&lt;/p&gt;
&lt;h4&gt;
  
  
  Syntax
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;arrayName[row]&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;fruits&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;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xkty0qwp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Access-Whole-Row-Data-of-Jagged-Array.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xkty0qwp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Access-Whole-Row-Data-of-Jagged-Array.gif" alt="Access-Whole-Row-Data-of-Jagged-Array" title="Access-Whole-Row-Data-of-Jagged-Array" width="880" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s display values using the &lt;strong&gt;&lt;em&gt;foreach&lt;/em&gt; loop&lt;/strong&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;string&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="k"&gt;foreach&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;rowValue&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fruits&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;span class="p"&gt;{&lt;/span&gt;        
    &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt;   &lt;span class="n"&gt;rowValue&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="c1"&gt;// line break&lt;/span&gt;
    &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"\n"&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;display&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://dotnetfiddle.net/k7LKAO"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  OUTPUT
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Mango&lt;br&gt;
Orange&lt;br&gt;
Melon&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Jagged Array vs Multidimensional Array
&lt;/h2&gt;

&lt;p&gt;Both jagged and rectangular arrays are a form of &lt;strong&gt;multidimensional arrays&lt;/strong&gt;, but the simplest one is a two-dimensional array.&lt;/p&gt;

&lt;p&gt;A jagged array is like a two-dimensional array, but the “&lt;strong&gt;ROWS&lt;/strong&gt;” in a jagged array can have a different number of columns. If you use (&lt;strong&gt;[ ][ ]&lt;/strong&gt;), you are creating an array of arrays, where each array within a larger array has a different length.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;Inner arrays within the main array need not be equal size&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A traditional two-dimensional array has a rectangular size, where each “&lt;strong&gt;ROW&lt;/strong&gt;” has the same number of columns. For this reason, a &lt;strong&gt;2D array&lt;/strong&gt; is often called a square array or a rectangular array, where each array within a larger array has the same length.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;Inner arrays within the main array are equal size&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1-0V3U_L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Jagged-vs-Multidimensional-Array.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1-0V3U_L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Jagged-vs-Multidimensional-Array.gif" alt="Jagged-vs-Multidimensional-Array" title="Jagged-vs-Multidimensional-Array" width="880" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The jagged array shown contains three rows: the &lt;strong&gt;first row&lt;/strong&gt; contains two elements, the &lt;strong&gt;second row&lt;/strong&gt; has four elements, and the &lt;strong&gt;third row&lt;/strong&gt; has three elements.&lt;/p&gt;

&lt;p&gt;The following code shows how the jagged array is created and initialized.&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;// Create an array of 3 int arrays.&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;intArray&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="c1"&gt;// Create each array that is an element of the jagged  &lt;/span&gt;
&lt;span class="n"&gt;intArray&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="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;1&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;intArray&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;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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;6&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;intArray&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="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;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;Let’s look at the following image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LyQ8ZSKT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Jagged-Array-Example.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LyQ8ZSKT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/Jagged-Array-Example.gif" alt="Jagged-Array-Example" title="Jagged-Array-Example" width="880" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rectangular arrays&lt;/strong&gt; are created using commas to separate each dimension. The following code declares a two-dimensional array where the dimensions are 3 × 2:&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;intArray&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="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;Assign values to the elements of the &lt;strong&gt;intArray&lt;/strong&gt; using one statement at a time, as shown 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;intArray&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="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="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&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;intArray&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;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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;intArray&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;0&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;intArray&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;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;6&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://res.cloudinary.com/practicaldev/image/fetch/s--Kmmsr6Tk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/2d-array-example.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kmmsr6Tk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://codebuns.com/wp-content/uploads/2020/09/2d-array-example.gif" alt="2d-array-example" title="2d-array-example" width="650" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/gwqnYg"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C# Reference | Microsoft Docs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays"&gt;Single Dimensional Arrays&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays"&gt;Multidimensional Arrays&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays"&gt;C# Jagged Arrays&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.array.rank?view=netcore-3.1"&gt;Array.Rank Property&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.array.length?view=netcore-3.1"&gt;Array.Length Property&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>csharp</category>
      <category>intermediat</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# for Loop (With STEP-BY-STEP Video)</title>
      <dc:creator>Codebuns</dc:creator>
      <pubDate>Mon, 28 Sep 2020 13:02:57 +0000</pubDate>
      <link>https://dev.to/codebuns/c-for-loop-animated-examples-n44</link>
      <guid>https://dev.to/codebuns/c-for-loop-animated-examples-n44</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-loop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-loop.png" alt="C# for loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The for Loop
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;C# for loop&lt;/strong&gt; is very common in programming. This loop is ideal for performing a &lt;strong&gt;fixed number of iterations&lt;/strong&gt; when the programmer needs to iterate over a block of code a specific number of times. &lt;strong&gt;C# for loop&lt;/strong&gt; uses in-built syntax for initializing, incrementing, and testing the value of a &lt;strong&gt;counter variable&lt;/strong&gt; that determines how many times the loop will execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn about the &lt;strong&gt;for loop&lt;/strong&gt;, It’s syntax and execution order used for &lt;strong&gt;definite iteration&lt;/strong&gt; (number of repetitions specified in advance)&lt;/li&gt;
&lt;li&gt;You will cover different variations of for loop.&lt;/li&gt;
&lt;li&gt;Jumping out of a loop or loop iteration prematurely&lt;/li&gt;
&lt;li&gt;See how to stop the current iteration and start the next iteration without breaking out of the loop.&lt;/li&gt;
&lt;li&gt;Then you will explore &lt;strong&gt;infinite&lt;/strong&gt; and &lt;strong&gt;nested&lt;/strong&gt; loops&lt;/li&gt;
&lt;li&gt;Finally, you will learn the use of for loop with array and list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you’re finished, you should have a good understanding of how to use definite iteration in C#.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Once you’ve read this article, I’d recommend you to check out my free &lt;strong&gt;C# Quick Start Guide&lt;/strong&gt; &lt;a href="https://www.codebuns.com/tag/csharp/" rel="noopener noreferrer"&gt;Here&lt;/a&gt;, where I explained most of the topics in detail with &lt;a href="https://www.youtube.com/codebuns" rel="noopener noreferrer"&gt;YOUTUBE videos&lt;/a&gt;. Read this article &lt;a href="https://www.codebuns.com/csharp-basics/for-loop/" rel="noopener noreferrer"&gt;Here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qoHLHVcduYo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Edit, and test all the &lt;strong&gt;CODE examples&lt;/strong&gt; used in this article with C# online editor: &lt;a href="https://www.codebuns.com/csharp/csharp-basics-code/#for-loop" rel="noopener noreferrer"&gt;Code Playground&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;The structure of a &lt;strong&gt;C# for loop&lt;/strong&gt; is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FC-for-Loop-Syntax.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FC-for-Loop-Syntax.gif" alt="C# for Loop Syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You begin &lt;strong&gt;C# for loop&lt;/strong&gt; syntax with the “&lt;strong&gt;for&lt;/strong&gt;” keyword followed by a set of parentheses. Within the parentheses are three different parts separated by two semicolons. These parts control the looping mechanism. Let’s examine each.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initializer:&lt;/strong&gt; The first part declares and initializes the counter variable. It happens only once before the loop begins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Condition:&lt;/strong&gt; The second part is the Boolean expression that determines when the loop stops. If the expression returns true, the code within the loop’s body executes. Otherwise, the loop stops. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterator:&lt;/strong&gt; The third part is something that uses a &lt;strong&gt;C# unary operator&lt;/strong&gt;. This piece of code increments/decrements the counter variable initialized in the first part of the &lt;strong&gt;C# for statement&lt;/strong&gt; and is executed at the end of each iteration.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; C# unary operator provides increment (&lt;strong&gt;++&lt;/strong&gt;) and decrement (&lt;strong&gt;--&lt;/strong&gt;) operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Order
&lt;/h2&gt;

&lt;p&gt;The first part is the &lt;strong&gt;initialization expression&lt;/strong&gt;, which normally allows you to set up an iterator (also known as “loop variable”) to its starting value. Initialization is the first action performed by the loop only once, no matter how many times the loop statements are executed. After that is the &lt;strong&gt;termination expression&lt;/strong&gt;, this is some boolean condition to inform the loop when to stop. As long as this expression is true, the body of the loop will repeat. The loop’s body is surrounded by the braces immediately following the &lt;strong&gt;for statement&lt;/strong&gt;. Finally, at the end is the &lt;strong&gt;update expression&lt;/strong&gt; that increments/decrements an iterator variable. This statement takes effect at the bottom of the loop, just before the condition is checked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flowchart 
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Flowchart.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Flowchart.jpg" alt="Flowchart of for Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The semantic of C# for loop is slightly more complicated than &lt;a href="https://www.codebuns.com/csharp-basics/while-loop/" rel="noopener noreferrer"&gt;while&lt;/a&gt; and &lt;a href="https://www.codebuns.com/csharp-basics/c-do-while-loop/" rel="noopener noreferrer"&gt;do-while&lt;/a&gt; loops: &lt;strong&gt;Initialization&lt;/strong&gt; occurs only once before the loop begins, and then the &lt;strong&gt;condition&lt;/strong&gt; is tested, which evaluates to true or false. If the condition is true, the code inside the &lt;strong&gt;loop's body&lt;/strong&gt; executes. In the end, increments/decrements the &lt;strong&gt;iterator&lt;/strong&gt; each time through the loop after the body statement executes.&lt;/p&gt;

&lt;p&gt;The condition is rechecked. If the condition is still true, the whole process starts over again. This logic repeats until the condition becomes false. In that case, C# exits the &lt;strong&gt;for loop&lt;/strong&gt; and the next first statement after the loop continues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: C# for Loop
&lt;/h2&gt;

&lt;p&gt;Here is an example of a simple &lt;strong&gt;for loop&lt;/strong&gt; that displays numbers 1 through 5 in the console window:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Example.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Example.gif" alt="C# for Loop Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Curly braces are optional if the body of the loop contains only one 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="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;1&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;5&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A semicolon (&lt;code&gt;;&lt;/code&gt;) separates the three parts. The initialization part is &lt;code&gt;int i = 1&lt;/code&gt;; the test part is &lt;code&gt;i&amp;lt;=5&lt;/code&gt;, and the increment part is &lt;code&gt;i++&lt;/code&gt;, which uses an increment operator (&lt;code&gt;++&lt;/code&gt;). That means the value of &lt;code&gt;i&lt;/code&gt; will go from 1 to 5. Within the body of the loop is a single statement, which is the call to &lt;code&gt;Console.WriteLine&lt;/code&gt; method to print out the value of &lt;code&gt;i&lt;/code&gt; variable in the console window.&lt;/p&gt;

&lt;p&gt;This is what happens when above &lt;strong&gt;for loop&lt;/strong&gt; executes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The initialization expression &lt;code&gt;int i = 1&lt;/code&gt; is executed, which declares the integer variable &lt;code&gt;i&lt;/code&gt; and initializes it to 1.&lt;/li&gt;
&lt;li&gt;The expression &lt;code&gt;i &amp;lt;= 5&lt;/code&gt; is tested to return true or false. If the expression is true, continue with Step 3. Otherwise, the loop is finished.&lt;/li&gt;
&lt;li&gt;The statement &lt;code&gt;Console.WriteLine(i);&lt;/code&gt; is executed.&lt;/li&gt;
&lt;li&gt;The update expression &lt;code&gt;i++&lt;/code&gt; is executed, which increases the counter variable value &lt;code&gt;i&lt;/code&gt; by 1.&lt;/li&gt;
&lt;li&gt;Back to Step 2.&lt;/li&gt;
&lt;li&gt;Steps 2–4 are repeated as long as the condition is true.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run the above example, the output looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
4&lt;br&gt;
5&lt;/p&gt;

&lt;p&gt;You can alternatively decrement the counter variable. Look at the following code:&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;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;5&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;1&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="n"&gt;i&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;I have initialized &lt;code&gt;i&lt;/code&gt; variable with the value 5. The loop iterates as long as i is greater than or equal to 1. At the end of each iteration, &lt;code&gt;i&lt;/code&gt; is decremented by 1. It will display numbers going down from 5 to 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
5&lt;br&gt;
4&lt;br&gt;
3&lt;br&gt;
2&lt;br&gt;
1&lt;/p&gt;
&lt;h2&gt;
  
  
  Variations of  for  Loop
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;C# for loop&lt;/strong&gt; has several variations; some may seem awkward 😉. I want to show you alternatives; otherwise, rearranging the parts of a &lt;strong&gt;for statement&lt;/strong&gt; like this is discouraged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C# for loop with two variables:&lt;/strong&gt; You can split first and third parts into several statements using the *&lt;em&gt;comma operator *&lt;/em&gt;(&lt;code&gt;,&lt;/code&gt;). Here is an example in which I have used multiple initialization statements and multiple iterator statements all at the same time. There are two counters, counter &lt;code&gt;i++&lt;/code&gt; moves up from 1 and the counter &lt;code&gt;j--&lt;/code&gt; moves down from 100:&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;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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&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="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;3&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;j&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;$"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="s"&gt;, j-&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;j&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can perform multiple tests using compound 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="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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&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="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;98&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;j&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;$"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="s"&gt;, j-&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;j&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
i-1, j-100&lt;br&gt;
i-2, j-99&lt;br&gt;
i-3, j-98&lt;/p&gt;

&lt;p&gt;Another option is to leave out one or more parts from the &lt;strong&gt;for statement&lt;/strong&gt;. Let’s leave out the initialization part if an &lt;strong&gt;int&lt;/strong&gt; value has been declared before the loop.&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;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="k"&gt;for&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;3&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="n"&gt;i&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;You can move around the third part of the &lt;strong&gt;for statement&lt;/strong&gt;, which is &lt;code&gt;i++&lt;/code&gt;. Let’s move the last part in the loop’s body.&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;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;3&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The third part of the &lt;strong&gt;for loop&lt;/strong&gt; runs after testing the first two parts.&lt;/p&gt;

&lt;p&gt;Two semicolons are mandatory and must be used as placeholders to separate the three parts.&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;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="k"&gt;for&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;3&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The break Keyword With for Loop
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;What if you want to jump out of a loop in the middle, before a for loop condition becomes false?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FThe-break-Keyword-With-for-Loop.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FThe-break-Keyword-With-for-Loop.gif" alt="The break Keyword With for Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;break&lt;/strong&gt; keyword does that. &lt;a href="https://www.codebuns.com/csharp-basics/jump-statements/#break-statement" rel="noopener noreferrer"&gt;C# break operator&lt;/a&gt; causes the &lt;strong&gt;for loop&lt;/strong&gt; to stop immediately before it completes the current iteration and passes control to the first statement immediately after the loop. With a &lt;strong&gt;break statement,&lt;/strong&gt; you’re stopping only the block of code the break is in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Break.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Break.gif" alt="The break Keyword With for Loop Debugging Animation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the counter reaches 4, the condition (&lt;code&gt;i==4&lt;/code&gt;) tests to &lt;strong&gt;true&lt;/strong&gt;. The &lt;strong&gt;break&lt;/strong&gt; keyword doesn't just skip the rest of the iteration 4; it stops altogether and resumes execution immediately following the loop. Loop skipped the values 4 and 5.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
After break start here.&lt;/p&gt;
&lt;h2&gt;
  
  
  The continue Keyword With for Loop
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;What if you want to stop executing the current iteration of the loop and skip ahead to the next iteration?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FThe-continue-Keyword-With-for-Loop.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FThe-continue-Keyword-With-for-Loop.gif" alt="The continue Keyword With for Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can do this with the &lt;strong&gt;continue&lt;/strong&gt; keyword. &lt;a href=""&gt;C# continue operator&lt;/a&gt; stops the current iteration, without breaking out of the loop entirely, it just jumps ahead to the next iteration of the loop. The &lt;strong&gt;continue&lt;/strong&gt; statement passes control straight to the &lt;strong&gt;for statement&lt;/strong&gt; to start over. Let’s look at this more closely with an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Continue.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Continue.gif" alt="The continue Keyword With for Loop Debugging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code example, the loop prints out all the numbers except one. It skips 4 due to the &lt;strong&gt;continue&lt;/strong&gt; statement. When the loop hits condition (&lt;code&gt;i==4&lt;/code&gt;), it immediately skips &lt;code&gt;Console.WriteLine(i)&lt;/code&gt; and goes directly to the &lt;strong&gt;for&lt;/strong&gt; statement to check the condition. If the condition is still true, the loop then continues with the next cycle through the loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
5&lt;/p&gt;
&lt;h2&gt;
  
  
  Infinite for Loop
&lt;/h2&gt;

&lt;p&gt;Whenever you don’t give a condition to the &lt;strong&gt;for loop&lt;/strong&gt;, it automatically becomes an &lt;strong&gt;infinite for loop&lt;/strong&gt;, meaning the loop block executes endlessly. Use the double semicolon (&lt;code&gt;;;&lt;/code&gt;) in &lt;strong&gt;for&lt;/strong&gt; loop to make it run infinite times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FInfinite-for-Loop-in-C.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FInfinite-for-Loop-in-C.gif" alt="Infinite for Loop in C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All three parts of the &lt;strong&gt;for statement&lt;/strong&gt; are optional, except two semicolons &lt;code&gt;for(;;)&lt;/code&gt;. If the missing part is a condition, C# assumes no condition as a ‘true’ condition, thus creating an &lt;strong&gt;infinite or endless loop&lt;/strong&gt;. Let’s look at the following &lt;strong&gt;infinite for loop&lt;/strong&gt; example with the double semicolon.&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="p"&gt;;&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;"I am infinite for loop"&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;strong&gt;Note:&lt;/strong&gt; To stop an endless loop, press &lt;code&gt;CTRL + C&lt;/code&gt; on the keyboard. I mean, you press and hold down the &lt;code&gt;CTRL&lt;/code&gt; key, and while holding it down, press the &lt;code&gt;C&lt;/code&gt; key.&lt;/p&gt;

&lt;p&gt;You can use the following code to see how an &lt;strong&gt;infinite for loop&lt;/strong&gt; can be stopped with a &lt;strong&gt;break statement&lt;/strong&gt;. The counter increments while you are trapped in the &lt;strong&gt;endless loop&lt;/strong&gt;. Once the counter is greater than or equal to 4, you break out of the &lt;strong&gt;for loop&lt;/strong&gt; with the &lt;strong&gt;break keyword 👍&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FInfinite-for-Loop-in-C.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FInfinite-for-Loop-in-C.gif" alt="Debugging of Infinite for Loop in C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested for Loops
&lt;/h2&gt;

&lt;p&gt;A loop placed within another loop’s body is a nested loop. The outer loop must completely contain the inner loop. In the following image, the &lt;strong&gt;outer for loop&lt;/strong&gt; entirely contains the &lt;strong&gt;inner for loop&lt;/strong&gt; within its body. With each iteration of the outer loop triggers the inner loop which executes multiple times until the &lt;strong&gt;condition&lt;/strong&gt; becomes false.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FNested-for-Loop-in-C.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F09%2FNested-for-Loop-in-C.gif" alt="Nested for Loop in C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the following code example, there are two &lt;strong&gt;for loops&lt;/strong&gt;, one within the other. The &lt;strong&gt;outer for loop&lt;/strong&gt; will use &lt;code&gt;int i=1&lt;/code&gt; and the &lt;strong&gt;inner for loop&lt;/strong&gt; will use &lt;code&gt;int j=0&lt;/code&gt; as their initialization statements. To control the loop iterations, I have declared and initialized integer variable &lt;strong&gt;numberOfTimes&lt;/strong&gt;. If you set the &lt;strong&gt;numberOfTimes&lt;/strong&gt; to 2, then each &lt;strong&gt;for loop&lt;/strong&gt; will iterate 2 times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-Nested-for-Loop.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-Nested-for-Loop.gif" alt="Debugging of Nested for Loops in C# Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the following example, the &lt;a href="https://www.codebuns.com/csharp-basics/jump-statements/#break-statement" rel="noopener noreferrer"&gt;break statement&lt;/a&gt; exits &lt;strong&gt;inner for loop&lt;/strong&gt; and returns control to the &lt;strong&gt;outer for loop&lt;/strong&gt;, rather than breaking out of all the loops entirely. To exit both loops simultaneously, you must use two separate break keywords, one for each loop:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-Nested-for-Loop-with-Break.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-Nested-for-Loop-with-Break.gif" alt="Debugging of Nested for Loops in C# with Break Keyword"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference Between for Loop and while  Loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-vs-while-Syntax.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-vs-while-Syntax.jpg" alt="Difference Between for Loop and while Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To write a &lt;a href="https://www.codebuns.com/csharp-basics/while-loop/" rel="noopener noreferrer"&gt;while loop&lt;/a&gt;, you initialize an iterator variable, and as long as its condition is true, you continue to execute the body of the &lt;strong&gt;while loop&lt;/strong&gt;. To avoid an &lt;strong&gt;infinite loop&lt;/strong&gt;, the body of the &lt;strong&gt;while loop&lt;/strong&gt; must update the iterator variable. This loop requires more external variables to control.&lt;/p&gt;

&lt;p&gt;Unlike the &lt;strong&gt;while loop&lt;/strong&gt; that relies on an external variable, the &lt;strong&gt;for loop&lt;/strong&gt; contains all the loop elements in one convenient place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-vs-while-Example.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-vs-while-Example.jpg" alt="Example of for Loop vs while Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array with for Loop
&lt;/h2&gt;

&lt;p&gt;You can use a &lt;strong&gt;for loop&lt;/strong&gt; when you need to loop through an array until a specific condition is reached (for example, end of an array). Here’s an example that shows how you can iterate through the items of an array using &lt;strong&gt;C# for loop&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Array.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-Array.gif" alt="Array with for Loop in C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first statement declares a string array called &lt;code&gt;fruitsArray&lt;/code&gt; which contains a list of fruits. You use the Length property of the fruits array to determine how many items an array contains, which is needed for the loop iterations. To access the array items individually, you need to use the value of &lt;code&gt;i&lt;/code&gt; as an &lt;code&gt;indexer&lt;/code&gt; inside the square brackets like this &lt;code&gt;fruitsArray[i]&lt;/code&gt;. The value of &lt;code&gt;i&lt;/code&gt; will go from 0 to 3. Finally, as you loop over each item in the array, its value is printed to the console window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
Apple&lt;br&gt;
Banana&lt;br&gt;
Mango&lt;br&gt;
Orange&lt;/p&gt;

&lt;h2&gt;
  
  
  List With for Loop
&lt;/h2&gt;

&lt;p&gt;You can use a &lt;strong&gt;for loop&lt;/strong&gt; when you need to loop through a list until it reaches a termination condition (for example, end of a list). Following example shows how you can iterate all the items of a list using &lt;strong&gt;C# for loop&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-List.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2020%2F08%2FC-for-Loop-List.gif" alt="Debugging of List With for Loop in C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first statement declares a list named &lt;code&gt;fruitsList&lt;/code&gt; that contains 4 fruits. You need &lt;code&gt;Count&lt;/code&gt; property to find out the total number of fruits in the list, which is needed for the loop iterations. To access the list items individually, you need to use the value of &lt;code&gt;i&lt;/code&gt; as an indexer inside the square brackets like this &lt;code&gt;fruitsList[i]&lt;/code&gt;. The value of &lt;code&gt;i&lt;/code&gt; will go from 0 to 3. As you loop over each item in the list, its value is printed to the console window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
Apple&lt;br&gt;
Banana&lt;br&gt;
Mango&lt;br&gt;
Orange&lt;/p&gt;

&lt;p&gt;Please Share This Post With Love ❤️&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>tutorial</category>
      <category>dotnet</category>
      <category>basics</category>
    </item>
    <item>
      <title>C# Jagged Array (With STEP-BY-STEP Video)</title>
      <dc:creator>Codebuns</dc:creator>
      <pubDate>Sun, 27 Sep 2020 10:39:20 +0000</pubDate>
      <link>https://dev.to/codebuns/c-jagged-array-array-of-arrays-f7g</link>
      <guid>https://dev.to/codebuns/c-jagged-array-array-of-arrays-f7g</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AKG0WEVBxoVF2TuPEA_KJFw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F700%2F1%2AKG0WEVBxoVF2TuPEA_KJFw.jpeg" alt="C# Jagged Array(Array of Arrays"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two kinds of multi-dimensional arrays: &lt;a href="https://www.codebuns.com/csharp-intermediate/rectangular-array/" rel="noopener noreferrer"&gt;Rectangular&lt;/a&gt; and &lt;strong&gt;Jagged&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C# jagged array&lt;/strong&gt; is an &lt;strong&gt;array of arrays&lt;/strong&gt; whose elements are fixed, and each element can hold a separate array of different dimensions and sizes. Think of a table with the rows of unequal lengths, meaning. When you create a jagged array, you declare the number of fixed rows in the array. Then, each row will hold an array of different lengths.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2018%2F10%2Fjagged-array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2018%2F10%2Fjagged-array.png" title="C# jagged array" alt="C# for Absolute Beginners: the Basics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/S1CyNBGZ3eo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Once you’ve read this article, I’d recommend you to check out my free &lt;strong&gt;C# Quick Start Guide&lt;/strong&gt; &lt;a href="https://www.codebuns.com/tag/csharp/" rel="noopener noreferrer"&gt;Here&lt;/a&gt;, where I explained most of the topics in detail with &lt;a href="https://www.youtube.com/codebuns" rel="noopener noreferrer"&gt;YOUTUBE videos&lt;/a&gt;. Read &lt;strong&gt;updated version&lt;/strong&gt; of this article &lt;a href="https://www.codebuns.com/csharp-intermediate/jagged-array/" rel="noopener noreferrer"&gt;Here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Declaration and Instantiation of a Jagged Array
&lt;/h2&gt;

&lt;p&gt;Jagged arrays are declared by placing one pair of square brackets after another (&lt;strong&gt;[][]&lt;/strong&gt;) to represent the number of dimensions. Here is an example of declaring a jagged array:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
type[][] arrayName;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&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="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&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/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-Declaration.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-Declaration.png" title="C# Jagged Array Declaration" alt="C# Jagged Array Declaration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To initialize the jagged array, only the size that defines the number of rows in the first pair of brackets is set. The second brackets representing the number of elements inside the row are kept empty because every row has a different number of elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
arrayName = new type[rows][];&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&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;intArray&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-Initialization.png" title="C# Jagged Array Initialization" alt="C# Jagged Array Initialization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Declaration and initialization of an array at the same time on a single line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
type[][] arrayName = new type[rows][];&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&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="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-Declaration-Initialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-Declaration-Initialization.png" title="C# Jagged Array Declaration Initialization" alt="C# Jagged Array Declaration Initialization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create and Assign Values to a Jagged Array
&lt;/h2&gt;

&lt;p&gt;The following &lt;strong&gt;intArray&lt;/strong&gt; consists of &lt;strong&gt;three elements&lt;/strong&gt;, each of which is a &lt;a href="https://www.codebuns.com/csharp-intermediate/single-dimensional-array/" rel="noopener noreferrer"&gt;single-dimensional array&lt;/a&gt; of 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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To assign values to jagged array elements, you use &lt;strong&gt;initializers&lt;/strong&gt; to add the array elements with three separate single-dimensional arrays-one statements at a time, as shown 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;intArray&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="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;1&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;intArray&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;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="n"&gt;intArray&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;You can use the second method, which is quite convenient. With this method, values can be assigned all at once using a curly bracket {…}.&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;intArray&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="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;1&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you initialize the jagged array using curly-bracket syntax, then you can omit the size that defines the number of rows. The C# compiler determines the size from the number of elements assigned 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="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&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="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;1&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s a shorter form 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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt; &lt;span class="p"&gt;=&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;1&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You cannot omit the &lt;strong&gt;new&lt;/strong&gt; operator from the elements’ initialization.&lt;/p&gt;

&lt;p&gt;Another way with the &lt;strong&gt;var&lt;/strong&gt; keyword&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;intArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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;1&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="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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/yLUoFp" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Access Individual Array Elements
&lt;/h2&gt;

&lt;p&gt;You refer to each element of the array using its row and column index with two indexers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
arrayName[rowIndex][columnIndex]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&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;intArray&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;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;Access the whole row of data using the following syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
arrayName[row]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&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;intArray&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;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F4-Jagged-Array-Get-Whole-Row-By-Index.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F4-Jagged-Array-Get-Whole-Row-By-Index.png" title="C# Jagged Array Get Whole Row By Index" alt="C# Jagged Array Get Whole Row By Index"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1: Jagged Array with 3 Rows of Different Lengths
&lt;/h2&gt;

&lt;p&gt;This array consists of &lt;strong&gt;three rows&lt;/strong&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="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;intArray&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To assign values to jagged array elements, you can have arrays of different lengths for each element. In the following example, a jagged array is composed of three separate &lt;a href="https://www.codebuns.com/csharp-intermediate/single-dimensional-array/" rel="noopener noreferrer"&gt;single-dimensional&lt;/a&gt; arrays-one per row.&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;intArray&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="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;1&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;intArray&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;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&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="n"&gt;intArray&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;Alternatively, this is how you declare and initialize a jagged array with array initializer syntax, whose elements are arrays of integer values.&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;intArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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="m"&gt;4&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="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;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/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2Fjagged-array-with-3-rows-of-different-sizes.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2Fjagged-array-with-3-rows-of-different-sizes.png" title="C# jagged array with 3 rows of different sizes" alt="C# jagged array with 3 rows of different sizes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;int[3][]&lt;/code&gt; indicates jagged array has 3 rows and the number of columns is undefined.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;intArray[0] = new int[]&lt;/code&gt; indicates row 0 has 2 columns with the values &lt;code&gt;{1, 2}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;intArray[1] = new int[]&lt;/code&gt; indicates row 1 has 3 columns with the values &lt;code&gt;{3, 4, 5}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;intArray[2] = new int[]&lt;/code&gt; indicates row 2 has 4 columns with the values &lt;code&gt;{6, 7, 8, 9}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Index values for the above array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-Index-Values.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-Index-Values.png" title="C# Jagged Array Index Values" alt="C# Jagged Array Index Values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Index values of a Jagged Array with 3 Rows of Different Lengths&lt;br&gt;
You can access each element of the arrays, which are part of the jagged array, using their index. The index positions are used inside two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) when retrieving the value from 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="n"&gt;intArray&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="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// return 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement fetches a value 1 from the 1st column (&lt;code&gt;[0]&lt;/code&gt;) of the 1st row (&lt;code&gt;[0]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[0, 0]&lt;/code&gt; of the 1st 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="n"&gt;intArray&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;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// return 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement fetches a value 5 from the 3rd column (&lt;code&gt;[2]&lt;/code&gt;) of the 2nd row (&lt;code&gt;[1]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[1, 2]&lt;/code&gt; of the 2nd 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="n"&gt;intArray&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;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// return 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement fetches a value 7 from the 2nd column (&lt;code&gt;[1]&lt;/code&gt;) of the 3rd row (&lt;code&gt;[2]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[2, 1]&lt;/code&gt; of the 3rd array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-Get-Values-By-index.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-Get-Values-By-index.png" title="C# Jagged Array Get Values By index" alt="C# Jagged Array Get Values By index"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can access a value of an indexed array element, which is part of the jagged array, by using two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) and then assign a value to it with assignment operator (&lt;strong&gt;=&lt;/strong&gt;). Look at the following statements.&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;intArray&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="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="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;The above statement assigns a value 10 to the 1st column (&lt;code&gt;[0]&lt;/code&gt;) of the 1st row (&lt;code&gt;[0]&lt;/code&gt;), which updates the value of the element &lt;code&gt;[0, 0]&lt;/code&gt; of the 1st array from 1 to 10.&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;intArray&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;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="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;The above statement assigns a value 50 to the 3rd column (&lt;code&gt;[2]&lt;/code&gt;) of the 2nd row (&lt;code&gt;[1]&lt;/code&gt;), which updates the value of the element &lt;code&gt;[1, 2]&lt;/code&gt; of the 2nd array from 5 to 50.&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;intArray&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;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;70&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement assigns a value 70 to the 2nd column (&lt;code&gt;[1]&lt;/code&gt;) of the 3rd row (&lt;code&gt;[2]&lt;/code&gt;), which updates the value of the element &lt;code&gt;[2, 1]&lt;/code&gt; of the 3rd array from 7 to 70.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-Set-Values-By-index.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-Set-Values-By-index.png" title="C# Jagged Array Set Values By index" alt="C# Jagged Array Set Values By index"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/peH5Pb" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2: Jagged Array with 2 Rows of Different Lengths
&lt;/h2&gt;

&lt;p&gt;This array consists of &lt;strong&gt;two rows&lt;/strong&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;char&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;charArray&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;char&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;Let’s assign values to the elements of a jagged array. In the following example, a jagged array is composed of two separate single dimensional arrays of char values — one per row.&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;charArray&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="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&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="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="n"&gt;charArray&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;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&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 is to declare and initialize a jagged array with array initializer 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;char&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;charArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&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/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-with-2-rows-of-different-sizes.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F1-Jagged-Array-with-2-rows-of-different-sizes.png" title="C# Jagged Array with 2 rows of different sizes" alt="C# Jagged Array with 2 rows of different sizes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;char[2][]&lt;/code&gt; indicates jagged array has 2 rows and the number of columns is undefined.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;charArray[0] = new char[]&lt;/code&gt; indicates row 0 has 3 columns with the values &lt;code&gt;{ 'A', 'B', 'C' }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;charArray[1] = new char[]&lt;/code&gt; indicates row 1 has 2 columns with the values &lt;code&gt;{ 'D', 'E' }&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Index values for the above array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-With-2-Rows-Index-Values.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F2-Jagged-Array-With-2-Rows-Index-Values.png" title="C# Jagged Array With 2 Rows Index Values" alt="C# Jagged Array With 2 Rows Index Values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The index positions are used inside two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) when retrieving the value from 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="n"&gt;charArray&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="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// return C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement fetches a value C from the 3rd column (&lt;code&gt;[2]&lt;/code&gt;) of the 1st row (&lt;code&gt;[0]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[0, 2]&lt;/code&gt; of the 1st 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="n"&gt;charArray&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// return E&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement fetches a value E from the 2nd column (&lt;code&gt;[1]&lt;/code&gt;) of the 2nd row (&lt;code&gt;[1]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[1, 1]&lt;/code&gt; of the 2nd array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-With-2-Rows-Get-Index-Values.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F3-Jagged-Array-With-2-Rows-Get-Index-Values.png" title="C# Jagged Array With 2 Rows Get Index Values" alt="C# Jagged Array With 2 Rows Get Index Values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can access the value of an indexed array element by using two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) and then assign a value to it with an assignment operator (&lt;strong&gt;=&lt;/strong&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;charArray&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="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="sc"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement assigns a value ‘F’ to the 3rd column (&lt;code&gt;[2]&lt;/code&gt;) of the 1st row (&lt;code&gt;[0]&lt;/code&gt;), which updates the value of the element &lt;code&gt;[0, 2]&lt;/code&gt; of the 1st array from 'C' to 'F'.&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;charArray&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;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'G'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement assigns a value ‘G’ to the 2nd column (&lt;code&gt;[1]&lt;/code&gt;) of the 2nd row (&lt;code&gt;[1]&lt;/code&gt;), which updates the value of the element &lt;code&gt;[1, 1]&lt;/code&gt; of the 2nd array from 'E' to 'G'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F4-Jagged-Array-With-2-Rows-Set-Values-By-Index.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F4-Jagged-Array-With-2-Rows-Set-Values-By-Index.png" title="C# Jagged Array With 2 Rows Set Values By Index" alt="C# Jagged Array With 2 Rows Set Values By Index"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/ASUnF9" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array of Strings
&lt;/h2&gt;

&lt;p&gt;You can declare a jagged array of strings as&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;string&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="p"&gt;=&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Banana"&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Papaya"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cherry"&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;Another way with the &lt;strong&gt;var&lt;/strong&gt; keyword&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;fruits&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Banana"&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="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Orange"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Papaya"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Apricot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cherry"&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://dotnetfiddle.net/rBdEkM" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 3: Jagged Array with 2 Rows, Each of Which Contains Multi-Dimensional Array
&lt;/h2&gt;

&lt;p&gt;In the following example, a single-dimensional jagged array consists of &lt;strong&gt;two rows&lt;/strong&gt;, each containing two separate multi-dimensional array elements of different sizes-one per row.&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;char&lt;/span&gt;&lt;span class="p"&gt;[][,]&lt;/span&gt; &lt;span class="n"&gt;charArray&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;char&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;Let’s assign values to the elements of a jagged 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="n"&gt;charArray&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="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&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="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'F'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="n"&gt;charArray&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;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&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="sc"&gt;'G'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'H'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'J'&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;Alternatively, you declare and initialize a jagged array with an array initializer whose elements are multi-dimensional arrays.&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;char&lt;/span&gt;&lt;span class="p"&gt;[][,]&lt;/span&gt; &lt;span class="n"&gt;charArray&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;char&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="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&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="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'F'&lt;/span&gt; &lt;span class="p"&gt;}&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;char&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="sc"&gt;'G'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'H'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'J'&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/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F5-Jagged-Array-With-2-Rows-2-Rectangular-Array.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F5-Jagged-Array-With-2-Rows-2-Rectangular-Array.png" title="C# Jagged Array With 2 Rows 2 Rectangular Array" alt="C# Jagged Array With 2 Rows 2 Rectangular Array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The index position is used inside a pair of indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) when retrieving the value from the individual array element. The first bracket is used for the jagged array row, and the second bracket indicates the element within that row.&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;charArray&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="m"&gt;0&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="c1"&gt;// return C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jagged array 1st row (&lt;code&gt;[0]&lt;/code&gt;) indicated by the first bracket contains the element &lt;code&gt;[0, 2]&lt;/code&gt;. Therefore, the above statement fetches value C from the 3rd column (&lt;code&gt;[2]&lt;/code&gt;) of the 1st row (&lt;code&gt;[0]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[0, 2]&lt;/code&gt; of the 1st 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="n"&gt;charArray&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;span class="m"&gt;1&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;span class="c1"&gt;// return J&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jagged array 2nd row (&lt;code&gt;[1]&lt;/code&gt;) indicated by the first bracket contains the element &lt;code&gt;[1, 1]&lt;/code&gt;. Therefore, the above statement fetches a value J from the 2nd column (&lt;code&gt;[1]&lt;/code&gt;) of the 2nd row (&lt;code&gt;[1]&lt;/code&gt;), which displays the value of the element &lt;code&gt;[1, 1]&lt;/code&gt; of the 2nd array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F6-Jagged-Array-With-2-Rows-2-Rectangular-Array-Get-Value.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F6-Jagged-Array-With-2-Rows-2-Rectangular-Array-Get-Value.png" title="C# Jagged Array With 2 Rows 2 Rectangular Array Get Value" alt="C# Jagged Array With 2 Rows 2 Rectangular Array Get Value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can access the value of an indexed array element by using two indexers (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) and then assign a value to it with an assignment operator (&lt;strong&gt;=&lt;/strong&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;charArray&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="m"&gt;0&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="sc"&gt;'K'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jagged array 1st row (&lt;code&gt;[0]&lt;/code&gt;) indicated by the first bracket contains the element &lt;code&gt;[0, 2]&lt;/code&gt;. Therefore, above statement assigns a value K to the 3rd column of the 1st row, which updates the element &lt;code&gt;[0, 2]&lt;/code&gt; of the 1st array from C to K.&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;charArray&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;span class="m"&gt;1&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;span class="p"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'L'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jagged array 2nd row (&lt;code&gt;[0]&lt;/code&gt;) indicated by the first bracket contains the element &lt;code&gt;[1, 1]&lt;/code&gt;. Therefore, the above statement assigns a value L to the 2nd column of the 2nd row, which updates the element &lt;code&gt;[1, 1]&lt;/code&gt; of the 2nd array from J to L.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F7-Jagged-Array-With-2-Rows-2-Rectangular-Array-Update-Value.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codebuns.com%2Fwp-content%2Fuploads%2F2019%2F05%2F7-Jagged-Array-With-2-Rows-2-Rectangular-Array-Update-Value.png" title="C# Jagged Array With 2 Rows 2 Rectangular Array Update Value" alt="C# Jagged Array With 2 Rows 2 Rectangular Array Update Value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/dsD9IC" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Jagged Array Property and Method
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Length:&lt;/strong&gt; Gets the number of elements in all of the dimensions of an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GetLength(&lt;em&gt;dimension&lt;/em&gt;):&lt;/strong&gt; Gets the number of elements in the specified dimension of an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Processing Jagged Array Using Nested for Loops
&lt;/h2&gt;

&lt;p&gt;Never hardcode total number elements when you want to iterate through the array. Always use properties or methods such as &lt;strong&gt;Length&lt;/strong&gt; and &lt;strong&gt;GetLength()&lt;/strong&gt; to determine the total number of iterations needed. Let’s look at the following examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Jagged Array with 3 Rows of Different Lengths
&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;string&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&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;intArray&lt;/span&gt; &lt;span class="p"&gt;=&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;1&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="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="m"&gt;4&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="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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&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;span class="c1"&gt;// Use intArray.GetLength(0) or intArray.Length&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;row&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;row&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetLength&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;row&lt;/span&gt;&lt;span class="p"&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;column&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;column&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&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="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&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="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"\n"&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;display&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://dotnetfiddle.net/L1lZ5T" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following output displays the rows and every element within that row.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
1 2&lt;br&gt;
3 4 5&lt;br&gt;
6 7 8 9&lt;/p&gt;

&lt;p&gt;First, declared and initialized jagged &lt;strong&gt;intArray&lt;/strong&gt; variable. On the left side of the declaration, a pair of two square brackets (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) indicate the number of dimensions. The array is two-dimensional; that’s why you need to use two separate &lt;strong&gt;for loops&lt;/strong&gt; to iterate through each row and every element within that row.&lt;/p&gt;

&lt;p&gt;Outer &lt;strong&gt;for loop&lt;/strong&gt; is to get the number of rows by calling the &lt;strong&gt;GetLength&lt;/strong&gt; method and passing 0 for the dimension; instead, you can use the &lt;strong&gt;Length&lt;/strong&gt; property. On the other hand, nested for loop requires a different approach because each row of a jagged array contains a different number of columns. Therefore, instead of the &lt;strong&gt;GetLength&lt;/strong&gt; method to return the number of columns for that row, you use the array’s Length property.&lt;/p&gt;

&lt;p&gt;Finally, within the &lt;strong&gt;inner loop&lt;/strong&gt;, you use variable &lt;strong&gt;intArray&lt;/strong&gt; with the loop counter row for &lt;strong&gt;rowIndex&lt;/strong&gt; and column for &lt;strong&gt;columnIndex&lt;/strong&gt; inside two sets of square brackets (&lt;strong&gt;[ ][ ]&lt;/strong&gt;) to refer to each element in the array.&lt;/p&gt;

&lt;p&gt;So the outer loop iterates through every row, and the inner loop iterates through every element inside that row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Array with 2 Rows of Different Lengths
&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;string&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[][]&lt;/span&gt; &lt;span class="n"&gt;charArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&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="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]{&lt;/span&gt;&lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&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="n"&gt;charArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetLength&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;++)&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;j&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;j&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;charArray&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;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;charArray&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;j&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&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="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"\n"&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;display&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://dotnetfiddle.net/WbeIJR" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
A B C&lt;br&gt;
D E&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Loop Through Jagged Array Composed of Multi-Dimensional Arrays
&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;string&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[][,&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;charArray&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;char&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="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[,&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="sc"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'C'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sc"&gt;'D'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'F'&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;char&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="p"&gt;]{{&lt;/span&gt;&lt;span class="sc"&gt;'G'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'H'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sc"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'J'&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// First for loop is used to iterate through jagged array rows&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="n"&gt;charArray&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="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;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;$"Element(&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="s"&gt;): \n"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;// For each jagged array row, 2nd and 3rd for loop is used iterate through elements on that row&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;j&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;j&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;charArray&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="nf"&gt;GetLength&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;j&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// Column iteration&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;k&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;k&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;charArray&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="nf"&gt;GetLength&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;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
       &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;charArray&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;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&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="n"&gt;display&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"\n"&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="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;display&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://dotnetfiddle.net/mTWVu4" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loop counters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;i&lt;/strong&gt; is used for jagged array row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;j&lt;/strong&gt; (&lt;em&gt;row&lt;/em&gt;) and&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;k&lt;/strong&gt; (&lt;em&gt;column&lt;/em&gt;) is used for array element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
Element(0):&lt;br&gt;
A B C&lt;br&gt;
D E F&lt;br&gt;
Element(1):&lt;br&gt;
G H&lt;br&gt;
I J&lt;/p&gt;

&lt;h2&gt;
  
  
  Jagged Array in Short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Elements are fixed, and each element can hold a separate array of different lengths.&lt;/li&gt;
&lt;li&gt;Think of a &lt;strong&gt;table&lt;/strong&gt; that has rows of &lt;strong&gt;unequal lengths&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The declaration statement must specify the number of rows in the array. Then, declare the number of columns in each row before assigning values to the elements in that row.&lt;/li&gt;
&lt;li&gt;Also called an &lt;strong&gt;array of arrays&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  C# Reference | Microsoft Docs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays" rel="noopener noreferrer"&gt;Jagged Arrays&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please Share This Post With Love ❤️&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>intermediate</category>
    </item>
    <item>
      <title>C# for Absolute Beginners (With STEP-BY-STEP Videos)</title>
      <dc:creator>Codebuns</dc:creator>
      <pubDate>Wed, 30 Oct 2019 15:58:52 +0000</pubDate>
      <link>https://dev.to/codebuns/c-for-absolute-beginners-the-basics-3j64</link>
      <guid>https://dev.to/codebuns/c-for-absolute-beginners-the-basics-3j64</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1920%2F1%2AUt-3svKA6tkLHNXmgLOesw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1920%2F1%2AUt-3svKA6tkLHNXmgLOesw.jpeg" title="C# for Absolute Beginners: the Basics" alt="C# for Absolute Beginners: the Basics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Begin your journey with C#. The objective of this article and &lt;a href="https://www.youtube.com/codebuns" rel="noopener noreferrer"&gt;YOUTUBE videos&lt;/a&gt; is to familiarize you with the &lt;strong&gt;C# programming basics&lt;/strong&gt;. This article is for absolute beginners who want to learn &lt;strong&gt;C# fundamentals&lt;/strong&gt; by coding. &lt;strong&gt;Please share&lt;/strong&gt; if you like &lt;a href="https://www.codebuns.com/csharp-basics/c-programming-basics-for-absolute-beginners/" rel="noopener noreferrer"&gt;this&lt;/a&gt; article.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  C# Online Editors
&lt;/h2&gt;

&lt;p&gt;There are many C# online editors out there, you can use for coding but I have selected 2 for this article. I have used both the editors very extensively for preparing this step by step C# tutorial and it worked very well for me. By the way, all the editors are lightweight and free to use in the browser.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KAOSFgjKMRE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I don't know about you guys. But I want a few basic things in an online C# editor.&lt;/p&gt;

&lt;p&gt;First, it should have a &lt;strong&gt;Simple and Clean Interface&lt;/strong&gt;. I don't wanna see lots of ads popping up all over the page. Moreover, it must have an easy-to-use interface.&lt;/p&gt;

&lt;p&gt;Additionally, I want &lt;strong&gt;Code Completion&lt;/strong&gt; in the editor; I need auto-completion with IntelliSense support, which can provide smart completion based on variable types, function definitions, etc.&lt;/p&gt;

&lt;p&gt;Lastly, I want &lt;strong&gt;Syntax Highlighting&lt;/strong&gt; to improve code readability.&lt;/p&gt;

&lt;h3&gt;
  
  
  C# Online Editors
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://try.dot.net" rel="noopener noreferrer"&gt;try.dot.net&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dotnetfiddle.net" rel="noopener noreferrer"&gt;dotnetfiddle.net&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Variables in C#
&lt;/h2&gt;

&lt;p&gt;"Variable" is the name given to a computer memory location for storing data that can be reused throughout the program-that is, it is used to store, retrieve, and modify changeable data.&lt;/p&gt;

&lt;p&gt;A variable is always defined with a datatype, which means that it will hold the value of a specific type, such as &lt;strong&gt;string&lt;/strong&gt;, &lt;strong&gt;int&lt;/strong&gt;, &lt;strong&gt;float&lt;/strong&gt;, and so on. Always assign value to a variable before using it to avoid a compile-time error.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/RUgsDUIUWMU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  C# Variable Naming Conventions
&lt;/h3&gt;

&lt;p&gt;Rules regarding variable names that must be followed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;camelCase&lt;/strong&gt; for local variables, such as &lt;strong&gt;name&lt;/strong&gt;, &lt;strong&gt;firstName&lt;/strong&gt;, &lt;strong&gt;lastName&lt;/strong&gt;, and &lt;strong&gt;isMarried&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Can contain the letters a-z and A-Z, the numbers 0–9, and the underscore (_) character-other symbols are not allowed.&lt;/li&gt;
&lt;li&gt;No spaces and cannot start with a number.&lt;/li&gt;
&lt;li&gt;Cannot use C# language-keywords such as namespace, class, using, and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Declaring Variables in C#.
&lt;/h3&gt;

&lt;p&gt;To declare a variable, use the &lt;strong&gt;type&lt;/strong&gt; and then a &lt;strong&gt;name&lt;/strong&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;string&lt;/span&gt; &lt;span class="n"&gt;name&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;age&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://dotnetfiddle.net/LHQ645" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type&lt;/strong&gt; must be one of the valid data types, and &lt;strong&gt;variableName&lt;/strong&gt; is the identifier. This is an &lt;strong&gt;explicitly typed&lt;/strong&gt; local variable where the type is explicitly defined.&lt;/p&gt;

&lt;p&gt;You can declare multiple variables on one line in the form of a comma-separated "&lt;strong&gt;,&lt;/strong&gt;" list if they are of the same type. Use a semicolon (&lt;strong&gt;;&lt;/strong&gt;) if they are of different types.&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;string&lt;/span&gt; &lt;span class="n"&gt;name&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;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To initialize the variable with a value, you have to use the assignment operator "&lt;strong&gt;=&lt;/strong&gt;". The variable name is on the left side of the operator and on the right side is the value, 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;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pirzada"&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;=&lt;/span&gt; &lt;span class="m"&gt;35&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://dotnetfiddle.net/YKjiQY" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can declare the variable with a type, and assign it a value at the same time on the same line, which is more convenient:&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;string&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;"Pirzada"&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;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;35&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://dotnetfiddle.net/lADdDp" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  var Variable in C# (Type Inference)
&lt;/h3&gt;

&lt;p&gt;To declare &lt;strong&gt;implicitly typed&lt;/strong&gt; local variable, use the &lt;strong&gt;var&lt;/strong&gt; keyword:&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;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Pirzada"&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;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;35&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://dotnetfiddle.net/wLrWTG" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can declare a variable without giving an explicit or real type. The &lt;strong&gt;var&lt;/strong&gt; keyword in c# instructs the compiler to infer the type of a variable from the expression on the right side of the initialization statement. Actually, compiler determines and assigns the most appropriate type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;strong&gt;var&lt;/strong&gt; can only be declared and initialized in a single statement. Otherwise, the compiler doesn't have anything from which to infer the type.&lt;/p&gt;

&lt;h2&gt;
  
  
  C# Comments
&lt;/h2&gt;

&lt;p&gt;C# comments are only for you to read, not for the computer to execute. Use comments only to clarify code that's difficult to understand.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/g8wIRPXtWrM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Single-Line Comments
&lt;/h3&gt;

&lt;p&gt;You can make any line into a C# single-line comment by starting it with the two forward slash marks &lt;code&gt;//&lt;/code&gt;, indicating that the remainder of the line is a comment.&lt;/p&gt;

&lt;p&gt;Once the compiler reads the slashes, it ignores all characters until the end of the current 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;// This is a comment &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;"This is not a comment"&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 run these two lines, you'll get the following output: &lt;code&gt;This is not a comment&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/o9WKuE" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  End-Of-Line Comments
&lt;/h3&gt;

&lt;p&gt;You can put a comment at the end of a line, 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;int&lt;/span&gt; &lt;span class="n"&gt;variableName&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;span class="c1"&gt;//this is a comment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything before &lt;code&gt;//&lt;/code&gt; is a normal line of code, and everything after that is a comment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/Eps2xf" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-Line Comments (Delimited comments)
&lt;/h3&gt;

&lt;p&gt;C# multiline comment begins with an open comment mark &lt;code&gt;/*&lt;/code&gt; and ends with a closed comment mark &lt;code&gt;*/&lt;/code&gt;. All the text between the delimiters is ignored by the compiler. &lt;/p&gt;

&lt;p&gt;Here is an 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="cm"&gt;/* 
Comment Line 1 
Comment Line 2 
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/VNe9XO" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C# if else Statement
&lt;/h2&gt;

&lt;p&gt;C# if else statement is one of the most commonly used control flow statement. With &lt;strong&gt;if&lt;/strong&gt; statements, you can tell the computer to make a choice by evaluating a Boolean logical expression (true or false) called &lt;strong&gt;condition&lt;/strong&gt;. It allows you to tell the computer whether to run the code inside the block based on the condition or set of conditions.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/HmZ1ZvR10V8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  if Statement
&lt;/h3&gt;

&lt;p&gt;When you only need to execute code for a true condition, use an &lt;strong&gt;if&lt;/strong&gt; statement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if&lt;/strong&gt; Condition flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F178%2F0%2AxBiUckZJ9QrwcdAY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F178%2F0%2AxBiUckZJ9QrwcdAY.png" title="C#’s if condition flowchart" alt="C#’s if condition flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if&lt;/strong&gt; Condition Example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F850%2F0%2AaEJOra_a3Id7RGoT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F850%2F0%2AaEJOra_a3Id7RGoT.png" title="C# if condition example" alt="C# if condition example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/CBbHSG" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The value of the number variable is 8. So the expression &lt;code&gt;(number &amp;gt; 7)&lt;/code&gt; is evaluated to &lt;strong&gt;true&lt;/strong&gt; because 8 is greater than 7. Hence, the statements inside the body of the &lt;strong&gt;if&lt;/strong&gt; block is executed. If you change the value of the number variable from 8 to 6, then the expression will return &lt;strong&gt;false&lt;/strong&gt;, which will ignore the execution of the &lt;strong&gt;if&lt;/strong&gt; body.&lt;/p&gt;

&lt;h3&gt;
  
  
  else Statement
&lt;/h3&gt;

&lt;p&gt;You can also have an &lt;strong&gt;else&lt;/strong&gt; clause. Whenever the Boolean condition of the &lt;strong&gt;if&lt;/strong&gt; statement is &lt;strong&gt;false&lt;/strong&gt;, the &lt;strong&gt;else&lt;/strong&gt; clause executes as shown in the following code block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if else&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F264%2F0%2ADv09mEQSPKygyL74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F264%2F0%2ADv09mEQSPKygyL74.png" title="C#’s if-else flowchart" alt="C#’s if-else flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if else&lt;/strong&gt; Condition Example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F425%2F0%2AxH3b1BDeEEKUVtQU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F425%2F0%2AxH3b1BDeEEKUVtQU.png" title="C#’s if-else condition example" alt="C#’s if-else condition example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/a4HL5R" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The value of a number variable is 6. So the expression &lt;code&gt;(number &amp;gt; 7)&lt;/code&gt; is evaluated to &lt;strong&gt;false&lt;/strong&gt; because 6 is less than 7. Hence, the code inside &lt;strong&gt;else&lt;/strong&gt; block is executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  else if Statement
&lt;/h3&gt;

&lt;p&gt;C# &lt;strong&gt;if statement&lt;/strong&gt; can be extended by any number of &lt;strong&gt;else if&lt;/strong&gt; clauses. This statement follows directly after the &lt;strong&gt;if&lt;/strong&gt; statement to let you use multiple conditions for testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if else-if else&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F304%2F0%2AAHnwDqak5ezOZUEq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F304%2F0%2AAHnwDqak5ezOZUEq.png" title="C#’s if else-if else Condition Example" alt="C#’s if else-if else Condition Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Order of tests is important if you are using an &lt;strong&gt;if else-if&lt;/strong&gt; construct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if else-if else&lt;/strong&gt; Condition Example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F459%2F0%2Aq_bKTVUDnQtzec_8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F459%2F0%2Aq_bKTVUDnQtzec_8.png" title="C#’s if else-if else condition example" alt="C#’s if else-if else condition example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/QkdL5E" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the expression &lt;code&gt;(gender == "Female")&lt;/code&gt; is evaluated to &lt;strong&gt;true&lt;/strong&gt;, which is the condition of the &lt;strong&gt;else if&lt;/strong&gt; block and everything inside the pair of braces (&lt;strong&gt;{ }&lt;/strong&gt;) is carried out and the rest of &lt;strong&gt;if&lt;/strong&gt; statement will be skipped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each additional condition will only be evaluated if all previous conditions are &lt;strong&gt;false&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  C# Conditional Operator (if else shorthand)
&lt;/h2&gt;

&lt;p&gt;C# provides a conditional operator "?:", which is sometimes called C# ternary or question operator. This operator is a shorthand method of writing a simple single line &lt;strong&gt;if else&lt;/strong&gt; statement.&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;Ternary&lt;/strong&gt; operator&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F331%2F0%2AmHUMQjwnIT-0gxW6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F331%2F0%2AmHUMQjwnIT-0gxW6.png" title="C#’s ternary operator" alt="C#’s ternary operator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;Ternary operator&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F365%2F0%2AezDlfwUlLpDyuNU1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F365%2F0%2AezDlfwUlLpDyuNU1.png" title="C#’s ternary operator flowchart" alt="C#’s ternary operator flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# conditional operator uses three operands on one line. The first operand is a Boolean expression that evaluates to true or false. If the expression is true, the value of the second operand is returned; otherwise, the value of the third is returned.&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;bool&lt;/span&gt; &lt;span class="n"&gt;loggedIn&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;string&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loggedIn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"Online"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Offline"&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;$"User is &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;status&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="c1"&gt;// Print&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the result of the ternary operator is one of two strings, both of which may be assigned to &lt;code&gt;status&lt;/code&gt; variable. The choice of which string to assign depends on the value of the &lt;code&gt;loggedIn&lt;/code&gt; variable being true or false. In this case, a value of true results in the first string being assigned, and a value of false results in the second string being assigned.&lt;/p&gt;

&lt;p&gt;The above code is equivalent to 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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;loggedIn&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;string&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&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;loggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
   &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Online"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;else&lt;/span&gt; 
   &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Offline"&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;$"User is &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;status&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="c1"&gt;// Print&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/CDvend" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C# switch Statement
&lt;/h2&gt;

&lt;p&gt;The C# switch statement evaluates a single expression against a list of multiple possible cases. Every switch case is related to the single expression and must end with the &lt;strong&gt;break&lt;/strong&gt; or &lt;strong&gt;goto case&lt;/strong&gt; or an empty block. The break statement passes control out of the switch. You can omit a &lt;strong&gt;break&lt;/strong&gt; statement if two cases lead to the same action.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/31tBUGr5oy4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;switch&lt;/strong&gt; statement starts with the &lt;strong&gt;switch&lt;/strong&gt; keyword followed by a &lt;strong&gt;switch&lt;/strong&gt; expression in parentheses. After the &lt;strong&gt;switch&lt;/strong&gt; expression, you include multiple case labels that represent the possible values of the &lt;strong&gt;switch&lt;/strong&gt; expression. When the &lt;strong&gt;switch&lt;/strong&gt; expression finds the matching value specified by a case label, the statements inside that case are executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F686%2F0%2AnifOFXObO5pBtsM1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F686%2F0%2AnifOFXObO5pBtsM1.png" title="C# switch statement example" alt="C# switch statement example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;switch Statement&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F395%2F0%2AGN1h08jLuzO-8pyY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F395%2F0%2AGN1h08jLuzO-8pyY.png" title="C# switch statement flowchart" alt="C# switch statement flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;strong&gt;default&lt;/strong&gt; case is optional and is executed if no other case applies. It's also a common practice to code the &lt;strong&gt;default&lt;/strong&gt; label last for easy reading.&lt;/p&gt;

&lt;p&gt;In the following example, the value of a number variable is 4. None of the cases matches with the value provided by the switch expression. In this case, the default block is executed which prints out "Unknown number!"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F492%2F0%2AKIZlL5WVfeLYGu_t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F492%2F0%2AKIZlL5WVfeLYGu_t.png" title="C#’s switch default" alt="C#’s switch default"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/3Xfb4G" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Iteration Statements
&lt;/h2&gt;

&lt;p&gt;Iteration statement (loop) is a block of code that will repeat itself over and over again until a given condition is true or some terminating condition is reached.&lt;/p&gt;

&lt;p&gt;There are four looping structures in C# that enable you to execute a block of code multiple times until a certain condition is met.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for loop&lt;/li&gt;
&lt;li&gt;while loop&lt;/li&gt;
&lt;li&gt;do…while loop&lt;/li&gt;
&lt;li&gt;foreach loop&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  for Loop
&lt;/h3&gt;

&lt;p&gt;The C# for loop executes a block of code repeatedly until the condition is &lt;strong&gt;false&lt;/strong&gt;. This type of loop is useful when the number of loop iterations is fixed, and the counter variable that determines how many times the loop is going to be executed is needed in the loop.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qoHLHVcduYo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;for loop&lt;/strong&gt; structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F383%2F0%2AaaTSkIikLRehlulD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F383%2F0%2AaaTSkIikLRehlulD.png" title="C#’s for loop structure" alt="C#’s for loop structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;for loop&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F290%2F0%2ACVmvaHSx9iVexvC8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F290%2F0%2ACVmvaHSx9iVexvC8.png" title="C#’s for loop structure" alt="C#’s for loop structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The C# &lt;strong&gt;for&lt;/strong&gt; loop starts with the &lt;strong&gt;for&lt;/strong&gt; keyword followed by three expressions separated by semicolons within parentheses.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first parameter declares and initializes a counter variable once at the start.&lt;/li&gt;
&lt;li&gt;The second parameter is a condition and is evaluated before each new iteration of the loop.&lt;/li&gt;
&lt;li&gt;The third parameter is an iterator, which prepares the loop for the next iteration.&lt;/li&gt;
&lt;li&gt;Statements are the block of code that you want to execute multiple times in a loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  for Loop Example
&lt;/h4&gt;

&lt;p&gt;The following example demonstrates usage of C# &lt;strong&gt;for&lt;/strong&gt; loop. It writes out all the integers from 0 to 4.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F284%2F0%2Aq8_N6bf8piv03Udr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F284%2F0%2Aq8_N6bf8piv03Udr.jpg" title="C#’s for loop example" alt="C#’s for loop example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/9z21Mo" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code consists of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the loop is first encountered, C# initializes the &lt;strong&gt;counter&lt;/strong&gt; variable &lt;code&gt;i&lt;/code&gt; with the &lt;strong&gt;int&lt;/strong&gt; type and assigns an initial value of 0 to it &lt;code&gt;(i=0)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A Boolean condition &lt;code&gt;(i &amp;lt; 4)&lt;/code&gt; will repeatedly execute until the counter is less than 4.&lt;/li&gt;
&lt;li&gt;An expression for updating the counter (&lt;code&gt;i++&lt;/code&gt; meaning &lt;strong&gt;i=i+1&lt;/strong&gt;) adds 1 at the end of each repetition of the loop, thus increasing the counter variable by 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  while Loop
&lt;/h3&gt;

&lt;p&gt;The C# while loop repeatedly executes a block of code inside the loop while the given logical expression is &lt;strong&gt;true&lt;/strong&gt;; otherwise, it breaks. This type of loop is useful when the number of loop iterations does not need to be known before the loop begins, and a counter variable is not needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Bear in mind that somewhere in the loop, the expression should be changed to false to avoid an inﬁnite or endless loop.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/etEgbRhl3Ao"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;while loop&lt;/strong&gt; structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F287%2F0%2AaR72wqd8aaVZwOAP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F287%2F0%2AaR72wqd8aaVZwOAP.png" title="C#’s while structure" alt="C#’s while structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;while loop&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F206%2F0%2AfCBGGLw8DaGkFkci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F206%2F0%2AfCBGGLw8DaGkFkci.png" title="C#’s while loop flowchart" alt="C#’s while loop flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The C# &lt;strong&gt;while&lt;/strong&gt; loop starts with the &lt;strong&gt;while&lt;/strong&gt; keyword followed by a single expression within parentheses.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loop takes only one expression.&lt;/li&gt;
&lt;li&gt;The condition is only evaluated at the beginning of each iteration.&lt;/li&gt;
&lt;li&gt;The condition (Boolean expression) is evaluated before each new iteration of the loop. This must be true in order for the next iteration to be performed. The iterations end when the condition is false.&lt;/li&gt;
&lt;li&gt;Set a Boolean flag to false somewhere in the loop to avoid an inﬁnite or endless loop.&lt;/li&gt;
&lt;li&gt;Loop ends with a semicolon ";".&lt;/li&gt;
&lt;li&gt;Statements are the block of code that you want to execute multiple times in a loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  while Loop Example
&lt;/h4&gt;

&lt;p&gt;The code below shows an example of how a C# &lt;strong&gt;while&lt;/strong&gt; loop works. It writes out all the integers from 1 to 4.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F410%2F0%2AdVRrSO2P74coSKNm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F410%2F0%2AdVRrSO2P74coSKNm.png" title="C#’s while loop example" alt="C#’s while loop example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/jXP3OO" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code consists of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declared and initialized a &lt;strong&gt;counter&lt;/strong&gt; variable with the &lt;strong&gt;int&lt;/strong&gt; type and assigned an initial value of 0 to it, &lt;code&gt;counter=0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Boolean expression is evaluated, and if it is &lt;strong&gt;true&lt;/strong&gt;, then the sequence of operations in the body of the loop is executed.&lt;/li&gt;
&lt;li&gt;A Boolean condition &lt;code&gt;(counter &amp;lt; 4)&lt;/code&gt; will repeatedly execute until the counter is less than 4.&lt;/li&gt;
&lt;li&gt;An expression for updating the counter (&lt;code&gt;counter++&lt;/code&gt; is a syntactically-shorter version of &lt;strong&gt;counter=counter+1&lt;/strong&gt;) adds 1 at the end of each repetition, thus increasing the counter variable by 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  do…while Loop
&lt;/h3&gt;

&lt;p&gt;The C# do-while loop is a variant of the &lt;strong&gt;while&lt;/strong&gt; loop except for one main difference: it will always execute the loop body at least once in the beginning irrespective of the condition being &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt;. The condition is evaluated at the end of the first execution of the block when the &lt;strong&gt;while&lt;/strong&gt; statement is encountered.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Bear in mind that somewhere in the loop, the expression should be changed to &lt;strong&gt;false&lt;/strong&gt; to avoid an inﬁnite or endless loop.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/B6FudGf7fAA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;do-while loop&lt;/strong&gt; structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F362%2F0%2AuAeZI4FP4vtQ7Kk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F362%2F0%2AuAeZI4FP4vtQ7Kk4.png" title="C#’s do-while loop structure" alt="C#’s do-while loop structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;do-while loop&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F214%2F0%2AdxmMmqmTSFSeTrYZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F214%2F0%2AdxmMmqmTSFSeTrYZ.png" title="C#’s do-while loop flow chart" alt="C#’s do-while loop flow chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;do-while&lt;/strong&gt; loop starts with the &lt;strong&gt;do&lt;/strong&gt; keyword and the loop body. At the bottom of the block is &lt;strong&gt;while&lt;/strong&gt; keyword followed by single expression within parentheses ().&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always executes the loop body at least once, even if the condition fails the ﬁrst time.&lt;/li&gt;
&lt;li&gt;The condition (Boolean expression) is evaluated after each new iteration of the loop. This must be &lt;strong&gt;true&lt;/strong&gt; in order for the next iteration to be performed. The iterations end when the condition is &lt;strong&gt;false&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Set a Boolean flag to false somewhere in the loop to avoid an inﬁnite or endless loop.&lt;/li&gt;
&lt;li&gt;Loop ends with a semicolon ";".&lt;/li&gt;
&lt;li&gt;Statements are the block of code that you want to execute multiple times in a loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  do-while Loop Example
&lt;/h4&gt;

&lt;p&gt;The code below shows an example of how a &lt;strong&gt;do-while&lt;/strong&gt; loop works. It writes out all the integers from 1 to 4.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F486%2F0%2AyUC3uJIqR4DMNONU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F486%2F0%2AyUC3uJIqR4DMNONU.png" title="C#’s do-while loop example" alt="C#’s do-while loop example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/jczQsR" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code consists of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declared and initialized a Counter variable with the &lt;strong&gt;int&lt;/strong&gt; type and assigned an initial value of 0 to it, &lt;code&gt;counter=0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;First time in the beginning, the loop body executes at least once irrespective of the condition being true or false.&lt;/li&gt;
&lt;li&gt;The Boolean expression is evaluated at the bottom of the code block when encountered.&lt;/li&gt;
&lt;li&gt;The Boolean condition &lt;code&gt;(counter &amp;lt; 4)&lt;/code&gt; will repeatedly execute until the counter is less than 4.&lt;/li&gt;
&lt;li&gt;An expression for updating the counter (&lt;code&gt;counter++&lt;/code&gt; is a syntactically-shorter version of &lt;strong&gt;counter=counter+1&lt;/strong&gt;) adds 1 at the end of each repetition, thus increasing the counter variable by 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  foreach Loop
&lt;/h3&gt;

&lt;p&gt;The C# &lt;strong&gt;foreach&lt;/strong&gt; loop provides simple syntax to cycle through all the elements in an array or collection unless you explicitly end the loop with the &lt;strong&gt;break&lt;/strong&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/k_SmGu7GYYU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;foreach loop&lt;/strong&gt; structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F361%2F0%2AXWw6ASbdhKSpS8MW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F361%2F0%2AXWw6ASbdhKSpS8MW.png" title="C#’s foreach loop structure" alt="C#’s foreach loop structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C# &lt;strong&gt;foreach loop&lt;/strong&gt; flowchart&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F397%2F0%2AtDAMeHoOlZiSNUBB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F397%2F0%2AtDAMeHoOlZiSNUBB.png" title="C#’s foreach loop structure" alt="C#’s foreach loop structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The C# &lt;strong&gt;foreach&lt;/strong&gt; loop starts with the &lt;strong&gt;foreach&lt;/strong&gt; keyword followed by parentheses. See the above images.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;type&lt;/strong&gt; is used to declare the data-type of the variable. You can use the &lt;strong&gt;var&lt;/strong&gt; keyword, which instructs the compiler to infer the type of the item from the type of the collection.&lt;/li&gt;
&lt;li&gt;The item is the looping variable used inside the loop body into which the next element is automatically acquired from the collection on each iteration. The variable (&lt;strong&gt;the iterator&lt;/strong&gt;) data-type must be the same as the items in the array or list.&lt;/li&gt;
&lt;li&gt;The collection is the array or list representing a number of values over which you want to iterate.&lt;/li&gt;
&lt;li&gt;The statements are the block of code that executes for each iteration of the loop multiple times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The compiler only allows reading from, not writing to, the collection during the execution of a &lt;strong&gt;foreach&lt;/strong&gt; loop.&lt;/p&gt;

&lt;h4&gt;
  
  
  foreach Loop Example
&lt;/h4&gt;

&lt;p&gt;If you have more than one statement within the loop, you must enclose the statements in the opening and closing braces. You can use braces with a single statement as well.&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;integerValues&lt;/span&gt; &lt;span class="p"&gt;=&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;span class="m"&gt;2&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="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;integerValues&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;System&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="k"&gt;value&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://dotnetfiddle.net/E0q3WO" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, you have an int variable &lt;strong&gt;value&lt;/strong&gt; that is used for looping. Each time the loop runs, an element in the &lt;code&gt;integerValues&lt;/code&gt; array is assigned to the variable &lt;strong&gt;value&lt;/strong&gt;. For example, the first time the loop runs, the number 1 is assigned to &lt;strong&gt;value&lt;/strong&gt;. It then executes the code within the block that makes up the &lt;strong&gt;foreach&lt;/strong&gt; loop body. The line &lt;code&gt;System.Console.Write(value);&lt;/code&gt; simply prints out the number 1 on the console. In the second iteration, number 2 is assigned and printed out. This continues until all the elements in the array have been printed. This means that the code inside the C# &lt;strong&gt;foreach&lt;/strong&gt; loop is called as many times as there are elements in the &lt;code&gt;integerValues&lt;/code&gt; array. So, if &lt;code&gt;integerValues&lt;/code&gt; contains 4 elements, the code inside the loop block will be executed 4 times.&lt;/p&gt;

&lt;h2&gt;
  
  
  String Concatenation
&lt;/h2&gt;

&lt;p&gt;A common use of string concatenation (kon-kat-en-ay-shun) is to inject variable values into the string using the string formatting syntax. C# provides the string operator &lt;code&gt;+&lt;/code&gt; (plus symbol) or the &lt;strong&gt;String.Format&lt;/strong&gt; method to format and organize various strings. Both ways can be used to join (concatenate) two or more strings together to generate a new string.&lt;/p&gt;

&lt;h3&gt;
  
  
  C# String.Concat( ) and "+"
&lt;/h3&gt;

&lt;p&gt;The variable &lt;strong&gt;result&lt;/strong&gt; in the following example would contain the value &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; after concatenating all the strings.&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;a&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&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="c1"&gt;//+ operator concatenates two strings &lt;/span&gt;
&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Addition of "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;a&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;b&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are numeric. Operands that are not of type string will be automatically converted to its string representation by calling the virtual &lt;code&gt;ToString&lt;/code&gt; method on that operand.&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;//+ symbol actually translates to the String.Concat() method &lt;/span&gt;
&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Addition of "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&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;b&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&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;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Concat&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;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Addition of "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&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;b&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The C# compiler converts the &lt;code&gt;+&lt;/code&gt; operator into the static &lt;strong&gt;String.Concat()&lt;/strong&gt; method at compile time; there is no real benefit in choosing one method over the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  C# String Interpolation (New Way)
&lt;/h3&gt;

&lt;p&gt;C# 6.0 introduced string interpolation which provides another option on how to insert variable values into a string. Now you can insert one or more expressions directly in the string literal just by adding the &lt;code&gt;$&lt;/code&gt; prefix to output the result in a nicely formatted manner. Interpolated strings are more readable and less error-prone than any other previous methods of formatting strings.&lt;/p&gt;

&lt;h4&gt;
  
  
  C# String Interpolation Format
&lt;/h4&gt;

&lt;p&gt;The following example uses string interpolation to create the string &lt;strong&gt;result&lt;/strong&gt; using the &lt;code&gt;$&lt;/code&gt; prefix. A string with a &lt;code&gt;$&lt;/code&gt; sign can use curly braces &lt;code&gt;{...}&lt;/code&gt; around the name of a variable to output the value assigned to it at that position in the string. Without the leading &lt;code&gt;$&lt;/code&gt; symbol, the string &lt;code&gt;{...}&lt;/code&gt; would be treated as a string literal.&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;a&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&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="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"Addition of &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&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;b&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;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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="c1"&gt;// string interpolation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/XJRscp" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Conversion
&lt;/h2&gt;

&lt;p&gt;Conversion of one type to another is called type conversion. There are two conversion mechanisms supported by C#:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implicit Type Conversion&lt;/li&gt;
&lt;li&gt;Explicit Type Conversion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implicit Type Conversion (Hidden)
&lt;/h3&gt;

&lt;p&gt;If one type is converted into another type automatically by the &lt;strong&gt;CLR&lt;/strong&gt;, it's called implicit type conversion. No special casting syntax is required, and no data is lost during implicit conversion.&lt;/p&gt;

&lt;p&gt;C# performs implicit data type conversion when types are compatible, and the compiler knows the conversion is safe. For example, the following code declares an &lt;strong&gt;int&lt;/strong&gt; variable and set its value equal to 100. Because an &lt;strong&gt;int&lt;/strong&gt; can always fit in a &lt;strong&gt;double&lt;/strong&gt;, C# knows this is safe and doesn't complain.&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;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;786&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&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="c1"&gt;// Implicit casting from int to double &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;57&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// automatic type conversion &lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;l&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="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'0'&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;d&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/bJSZJz" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Explicit Type Conversion (Cast)
&lt;/h3&gt;

&lt;p&gt;Explicit conversion is required when the data cannot convert from one simple-type to another automatically by the compiler or when conversion may change the value by producing an incorrect result due to the possible loss of data (&lt;strong&gt;narrowing conversions&lt;/strong&gt;). To prevent a compilation error, the compiler requires you to use a cast operator to perform the type conversion.&lt;/p&gt;

&lt;p&gt;Let's look at different ways of explicit type conversion in C#.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Casting&lt;/li&gt;
&lt;li&gt;Converting&lt;/li&gt;
&lt;li&gt;Parsing&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Casting
&lt;/h4&gt;

&lt;p&gt;The parentheses (&lt;strong&gt;( )&lt;/strong&gt;) operator is used to explicitly cast one type to another by forcing the compiler to make the conversion. Casting works only between compatible data types, where &lt;strong&gt;CLR&lt;/strong&gt; knows how to convert from one type to the other.&lt;/p&gt;

&lt;p&gt;To perform a cast, put the target data type inside parentheses in front of the value or variable to be converted. If the cast is successful, it returns a value in that type; otherwise, it throws an exception. Take a look at the following example.&lt;/p&gt;

&lt;p&gt;Some of the conversions that cannot be made implicitly:&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;long&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2222&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// use ( ) operator to convert a type explicitly &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="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;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Explicit casting from long to int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/fU8DBp" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Converting
&lt;/h4&gt;

&lt;p&gt;You can use the &lt;strong&gt;System.Convert&lt;/strong&gt; class to perform conversions between base types where implicit or explicit casting isn't possible. This class contains a complete set of static methods for both up level or down level casting and throws an &lt;strong&gt;InvalidCastException&lt;/strong&gt; for failed conversions while performing a cast from one type to another.&lt;/p&gt;

&lt;p&gt;The string supplied must be a valid representation of a number, and that number must be one that won't cause an overflow. The following code uses the Convert class &lt;strong&gt;ToInt32&lt;/strong&gt; method to change the string into an int:&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;string&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"786"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Conversion from string to int &lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Convert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToInt32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Return 786&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/tMxL7y" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Parsing
&lt;/h4&gt;

&lt;p&gt;Parsing is used to convert the string type into a primitive value type by using built-in parsers, which are included in all the data structures of the .NET framework. You can use the &lt;strong&gt;Parse&lt;/strong&gt; or &lt;strong&gt;TryParse&lt;/strong&gt; methods to convert a string representation of a number to an integer, such as the &lt;strong&gt;System.Int32&lt;/strong&gt; type.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;TryParse&lt;/strong&gt; method which takes two parameters: the first is the string to be parsed, and the second is an int variable with the &lt;strong&gt;out&lt;/strong&gt; keyword to be filled with the value returned if the conversion succeeds.&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;string&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"786"&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;parsedValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;parsedValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Return True &lt;/span&gt;
&lt;span class="c1"&gt;// With Code block &lt;/span&gt;
&lt;span class="k"&gt;if&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="nf"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;parsedValue&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/MRajIF" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Parse&lt;/strong&gt; method takes a string as a parameter and returns an int if the string contains an integer 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;string&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"786"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="n"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Return 786&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotnetfiddle.net/8l30qq" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  C# Constant, Readonly
&lt;/h2&gt;

&lt;p&gt;C# supports two types of constants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compile-time constants:&lt;/strong&gt; The field value of the constant cannot be changed after the initial assignment, and it must, therefore, be assigned a value as it is declared.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run-time constants:&lt;/strong&gt; The value of the constant can be assigned during run-time; once it's assigned a value, it can't be changed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A constant can be any of the built-in numeric types, bool, char, string, or an enum type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constant (Compile-time Constants)
&lt;/h3&gt;

&lt;p&gt;For compile-time constants, you can use the &lt;strong&gt;const&lt;/strong&gt; keyword before the data type to declare a constant field or a constant local to a method, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F518%2F0%2AZLdaFgDSZ2E50ExW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F518%2F0%2AZLdaFgDSZ2E50ExW.png" title="C# Compile-Time Constant" alt="C# Compile-Time Constant"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/XG0gQV" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;const&lt;/strong&gt; modifier creates a compile-time constant, so the compiler will replace all usage of the constant with its value. Therefore, the value must be assigned at the time of declaration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Readonly (Run-time Constants)
&lt;/h3&gt;

&lt;p&gt;For run-time constants, use the &lt;strong&gt;readonly&lt;/strong&gt; keyword. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F606%2F0%2ArbkZqSiqXjC4YnlM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F606%2F0%2ArbkZqSiqXjC4YnlM.png" title="C#’s Run-Time Constant" alt="C#’s Run-Time Constant"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dotnetfiddle.net/UqgiWs" rel="noopener noreferrer"&gt;Run Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This modifier may only be applied to fields and can only be initialized either at the declaration or in a constructor but nowhere else.&lt;/p&gt;

&lt;p&gt;Please Share This Post With Love ❤️&lt;/p&gt;

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