<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Daniel's Blog]]></title><description><![CDATA[Hi, I'm Daniel. I am a Microsoft Azure Cloud Engineer passionate about the cloud. I'm currently learning C# so you can expect content about this topic from me i]]></description><link>https://blog.danielfv.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1683117759461/BN2Wyi-HH.png</url><title>Daniel&apos;s Blog</title><link>https://blog.danielfv.com</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 12:29:17 GMT</lastBuildDate><atom:link href="https://blog.danielfv.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Variables and data types in C#]]></title><description><![CDATA[In C#, there are built-in types (e.g., integers, floating-point numbers, characters, and Booleans) and user-defined (custom) types (e.g., classes, structs, and enums).
Data types specify the kind of data a variable can hold or the kind of values an o...]]></description><link>https://blog.danielfv.com/csharp-variables-and-data-types</link><guid isPermaLink="true">https://blog.danielfv.com/csharp-variables-and-data-types</guid><category><![CDATA[C#]]></category><category><![CDATA[data types]]></category><category><![CDATA[intro to programming]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[programming languages]]></category><dc:creator><![CDATA[Daniel Fajardo Valenti]]></dc:creator><pubDate>Mon, 08 May 2023 00:14:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683503903409/fa09a568-892e-4409-965e-440d0016b15d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In C#, there are built-in types (e.g., integers, floating-point numbers, characters, and Booleans) and user-defined (custom) types (e.g., classes, structs, and enums).</p>
<p>Data types specify the kind of data a variable can hold or the kind of values an object can represent. Types are important because they determine the operations that can be performed on the data, the way data is stored in memory, and how it is passed between methods.</p>
<p>A data type defines the kind of values a variable can hold and the operations that can be performed on those values. In other words, a data type is a blueprint, while a variable is a storage location that follows that blueprint.</p>
<h2 id="heading-relationship-between-data-types-and-variables">Relationship between data types and variables</h2>
<p>When we declare a variable, we must specify its data type.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683200468063/81c9a43f-f05e-4d7c-8d6d-a783aa887c3d.png" alt /></p>
<p>This informs the compiler about the kind of data the variable can store and the operations that can be applied to it. In this case, integer values and you can perform operations like addition, subtraction, multiplication, and division on it.</p>
<p>The data type of a variable determines how much memory is allocated for that variable and how the data is interpreted when it's read or modified.</p>
<h2 id="heading-data-types">Data types</h2>
<p>In C# and other programming languages, a variable of a <strong>value type</strong> contains an instance of the type. This differs from a variable of a <strong>reference type</strong>, which contains a reference to an instance of the type.</p>
<p>There are <strong>built-in types</strong> included in C# that we can use and serve as the basis for creating more complex <strong>custom types</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683504047892/891fb268-204d-4294-9be1-ab641a552bf6.png" alt class="image--center mx-auto" /></p>
<ol>
<li><strong>Value Type:</strong></li>
</ol>
<ul>
<li><p>Value types directly contain their data, and memory is allocated on the stack.</p>
</li>
<li><p>When a value type variable is assigned to another variable, a copy of the value is created. Therefore, changes to one variable do not affect the other.</p>
</li>
<li><p>Value types are usually more efficient in terms of memory usage and access speed.</p>
</li>
</ul>
<ol>
<li><strong>Reference Type:</strong></li>
</ol>
<ul>
<li><p>Reference types store a reference (memory address) to the memory location where the data is stored, rather than the data itself. Memory for reference types is allocated on the heap.</p>
</li>
<li><p>When a reference type variable is assigned to another variable, both variables refer to the same memory location. Therefore, changes made to one variable also affect the other.</p>
</li>
</ul>
<p>Here's an example:</p>
<pre><code class="lang-csharp"><span class="hljs-comment">// *** Value type ***</span>
<span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;
<span class="hljs-keyword">int</span> b = a; <span class="hljs-comment">// We assign the *value* of a to b</span>
b += <span class="hljs-number">5</span>; <span class="hljs-comment">// We increment the b variable only</span>

Console.WriteLine(<span class="hljs-string">$"a: <span class="hljs-subst">{a}</span>, b: <span class="hljs-subst">{b}</span>"</span>); 
<span class="hljs-comment">// Output: a: 10, b: 15 </span>

<span class="hljs-comment">// ***  Reference type ***</span>
<span class="hljs-comment">// we create a new object of the class StringBuilder</span>
StringBuilder sb1 = <span class="hljs-keyword">new</span> StringBuilder(<span class="hljs-string">"Hello"</span>);
StringBuilder sb2 = sb1; <span class="hljs-comment">// we assign sb1 to sb2</span>
sb2.Append(<span class="hljs-string">", World!"</span>); <span class="hljs-comment">// we add text to the original string</span>

Console.WriteLine(<span class="hljs-string">$"sb1: <span class="hljs-subst">{sb1}</span>, sb2: <span class="hljs-subst">{sb2}</span>"</span>); 
<span class="hljs-comment">// Output: sb1: Hello, World!, sb2: Hello, World!</span>
</code></pre>
<p>In this example, when we assign <code>a</code> to <code>b</code>, a new copy of the value is created. Thus, modifying <code>b</code> does not affect <code>a</code>. However, when we assign <code>sb1</code> to <code>sb2</code>, both variables reference the same memory location, so changes made to one variable are visible to the other.</p>
<h2 id="heading-summary-of-data-types">Summary of Data types</h2>
<p>Here's a summary of the different kind of value types and reference types available in C#.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683504092940/eb19f7ef-1f5c-422b-ab27-430a19638949.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, C# offers various data types that help manage how data is stored, accessed, and manipulated. Understanding the differences between value types and reference types, as well as built-in and custom types, is essential for efficient programming in C#.</p>
<h2 id="heading-reference">Reference</h2>
<ul>
<li><p><a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/standard/class-library-overview">.NET class library overview | Microsoft Learn</a></p>
</li>
<li><p><a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/">The C# type system | Microsoft Learn</a></p>
</li>
<li><p><a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types">Integral numeric types - C# reference | Microsoft Learn</a></p>
</li>
<li><p><a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types">Floating-point numeric types - C# reference | Microsoft Learn</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>