<?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>Forem: Gokul</title>
    <description>The latest articles on Forem by Gokul (@gokulnathan).</description>
    <link>https://forem.com/gokulnathan</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%2F972718%2F665aac87-ca0b-47b6-8f9a-3544846bb2b3.jpg</url>
      <title>Forem: Gokul</title>
      <link>https://forem.com/gokulnathan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gokulnathan"/>
    <language>en</language>
    <item>
      <title>How .NET Really Works: From Build to CLR Execution Explained Simply</title>
      <dc:creator>Gokul</dc:creator>
      <pubDate>Sun, 22 Jun 2025 16:04:30 +0000</pubDate>
      <link>https://forem.com/gokulnathan/how-net-really-works-from-build-to-clr-execution-explained-simply-p01</link>
      <guid>https://forem.com/gokulnathan/how-net-really-works-from-build-to-clr-execution-explained-simply-p01</guid>
      <description>&lt;p&gt;I used to wonder why we use a “slow and bloated” framework when languages like C produce tiny binaries and run so efficiently. I never really knew what happened under the hood of every .NET app I created, and each new release hides even more details behind abstraction.&lt;/p&gt;

&lt;p&gt;I promised myself that, to love my job, I must first appreciate the tech I work with. This article is my attempt to understand how .NET and its runtime work. Hope you enjoy it! 😊&lt;/p&gt;

&lt;p&gt;We all know C# is a compiled language so there is are &lt;strong&gt;2&lt;/strong&gt; phases in it &lt;strong&gt;Build Phase&lt;/strong&gt; and &lt;strong&gt;Run Phase&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Let’s start with what happens during the &lt;strong&gt;build&lt;/strong&gt; phase. &lt;/p&gt;
&lt;h2&gt;
  
  
  Calm before the storm - the so-called Build phase 🤣
&lt;/h2&gt;

&lt;p&gt;Internally, &lt;strong&gt;MSBuild&lt;/strong&gt; is invoked when we enter &lt;code&gt;dotnet build&lt;/code&gt; in CMD:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;SDK selection&lt;/strong&gt;
MSBuild checks the SDK specified in &lt;em&gt;myapp.csproj&lt;/em&gt;, for example:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   &amp;lt;Project Sdk="Microsoft.NET.Sdk.Web" /&amp;gt;
   &amp;lt;!-- or --&amp;gt;
   &amp;lt;Project Sdk="Microsoft.NET.Sdk" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;SDKs live under:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   C:\Program Files\dotnet\sdk\&amp;lt;version&amp;gt;\Sdks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Load &lt;code&gt;.props&lt;/code&gt; files&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
These set environment variables, global properties, and defaults (output path, configuration, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Gathers all project items which includes&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*.cs&lt;/code&gt; source files
&lt;/li&gt;
&lt;li&gt;Referenced assemblies (e.g., &lt;code&gt;System.Net.Http.dll&lt;/code&gt;)
these are the using libraries which use in our c# code, each using statement corresponds to a &lt;code&gt;.dll&lt;/code&gt; file which is placed in &lt;code&gt;C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.36&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;NuGet packages (e.g., StackExchange.Redis)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Load &lt;code&gt;.targets&lt;/code&gt; files&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
These define what actual build steps to run. these can include predefined sequece of steps like  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Restore&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PrepareResources&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Compile&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GenerateAssemblyInfo&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CopyFilesToOutputDirectory&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Build&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;During above sequence we have Compile step&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
During compile step MSBUILD will use roslyn compiler to compile .cs file into IL ( .dll files)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compiler will enforce CLS rules &lt;/li&gt;
&lt;li&gt;compiler also maps all language specific types to CTS-defined types ( e.g., int -&amp;gt; System.Int32 )&lt;/li&gt;
&lt;li&gt;Both CLS ( Common language specification ) and CTS ( Common Type System ) plays vital role in produced IL( Intermediate Language ) code.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
MSBuild drops the generated &lt;code&gt;.dll&lt;/code&gt;, optional &lt;code&gt;.exe&lt;/code&gt;, &lt;code&gt;*.pdb&lt;/code&gt;, &lt;code&gt;*.deps.json&lt;/code&gt;, etc. into:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.\bin\Debug\net8.0\
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  The Actual strom - The Run Phase 🍃
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Does two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;dotnet build&lt;/code&gt; ( will be skipped if already done and no changes are detected ) &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dotnet myapp.dll&lt;/code&gt; ( actual execution of your application starts here )&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;p&gt;So far, we have compiled our code into &lt;code&gt;.dll&lt;/code&gt; and &lt;code&gt;.exe&lt;/code&gt; files along with other dependencies.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These files are also called IL (Intermediate Language) code.
&lt;/li&gt;
&lt;li&gt;They cannot be directly executed on Windows, Linux, or any OS. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Because they are not machine code.  &lt;/p&gt;

&lt;p&gt;You may ask:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;What is the purpose of this extra step? Why not directly convert the source code into machine code?&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;There are several reasons why this was implemented:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cross-platform&lt;/strong&gt;: IL runs anywhere the CLR exists.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime optimisation&lt;/strong&gt;: JIT tailors code to the actual CPU.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security &amp;amp; verification&lt;/strong&gt;: CLR checks IL for type safety and memory violations.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language interoperability&lt;/strong&gt;: C#, F#, VB.NET, etc., all compile to the same IL.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Enable verbose host tracing
&lt;/h3&gt;

&lt;p&gt;Before we proceed further its better if we enable below flag for detailed CMD traces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set COREHOST_TRACE=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution starts when &lt;code&gt;dotnet myapp.dll&lt;/code&gt; is called :&lt;br&gt;&lt;br&gt;
As part of execution below command is executed internally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;--additionalprobingpath&lt;/span&gt; C:&lt;span class="se"&gt;\U&lt;/span&gt;sers&lt;span class="se"&gt;\Y&lt;/span&gt;ourName&lt;span class="se"&gt;\.&lt;/span&gt;nuget&lt;span class="se"&gt;\p&lt;/span&gt;ackages C:&lt;span class="se"&gt;\U&lt;/span&gt;sers&lt;span class="se"&gt;\Y&lt;/span&gt;ourName&lt;span class="se"&gt;\D&lt;/span&gt;esktop&lt;span class="se"&gt;\m&lt;/span&gt;yapp&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="se"&gt;\D&lt;/span&gt;ebug&lt;span class="se"&gt;\n&lt;/span&gt;et8.0&lt;span class="se"&gt;\m&lt;/span&gt;yapp.dll
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  so what it does ?
&lt;/h4&gt;

&lt;p&gt;It kicks of an Execution Sequence called CoreHost chain :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vxwvsykloarxw371hb6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vxwvsykloarxw371hb6.png" alt="What happens when we do dotnet run" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;dotnet.exe&lt;/strong&gt; ( located in "C:\Program Files\dotnet\dotnet.exe" ) is called.

&lt;ul&gt;
&lt;li&gt;This acts as the entry point of CoreHost chain.&lt;/li&gt;
&lt;li&gt;From here directly calls hostfxr.dll file&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hostfxr.dll&lt;/strong&gt; is called!

&lt;ul&gt;
&lt;li&gt;resolved runtime sdk&lt;/li&gt;
&lt;li&gt;Uses &lt;code&gt;--additionalprobingpath&lt;/code&gt; to locate NuGet packages&lt;/li&gt;
&lt;li&gt;Chooses appropriate framework&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;hostpolicy.dll&lt;/strong&gt; is called :

&lt;ul&gt;
&lt;li&gt;loads dependecy based on &lt;code&gt;.deps.json&lt;/code&gt; file (found along with myapp.dll bost build ).&lt;/li&gt;
&lt;li&gt;Resolves and loads all runtime + app assemblies.
basically loads below things 

&lt;ul&gt;
&lt;li&gt;our app dll (myapp.dll)&lt;/li&gt;
&lt;li&gt;Reference library if any ( Newtonsoft.Json.dll )&lt;/li&gt;
&lt;li&gt;.Net runtime assemblies like ( System.Console.dll )&lt;/li&gt;
&lt;li&gt;Other Dependency files mentioned in *.deps.json file.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Finally once all these are done, coreclr.dll is called.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;coreclr.dll&lt;/strong&gt; :

&lt;ul&gt;
&lt;li&gt;The fricking start of the clr itself.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;lets talk about what clr is and why it is important.&lt;/p&gt;

&lt;p&gt;As discussed before, the CLR (Common Language Runtime) acts as a runtime platform or container environment capable of processing and executing IL code. It also provides several essential runtime services:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory management&lt;/strong&gt; via Garbage Collection&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conversion of IL to machine code&lt;/strong&gt; using the JIT compiler, which optimizes based on the runtime machine&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type safety verification&lt;/strong&gt; through the CTS (Common Type System)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once all these steps are completed the app will be up and running 🥳.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4evg6bh1cfgok0s1219a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4evg6bh1cfgok0s1219a.png" alt="Simple portrayal for execution" width="700" height="737"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>C# Inheritance: Virtual, Override, New, and Hidden Dangers</title>
      <dc:creator>Gokul</dc:creator>
      <pubDate>Mon, 12 May 2025 11:39:34 +0000</pubDate>
      <link>https://forem.com/gokulnathan/c-inheritance-virtual-override-new-and-hidden-dangers-mal</link>
      <guid>https://forem.com/gokulnathan/c-inheritance-virtual-override-new-and-hidden-dangers-mal</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxru7otgkcvczsfs09918.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxru7otgkcvczsfs09918.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;br&gt;
C# inheritance is quite easy to implement but sometimes complex to understand.&lt;/p&gt;

&lt;p&gt;Sure, most developers know what &lt;code&gt;virtual&lt;/code&gt;, &lt;code&gt;override&lt;/code&gt;, and &lt;code&gt;new&lt;/code&gt; do. But what if we throw in a couple of casts and a few extra levels of inheritance? Now things aren’t so straightforward.&lt;/p&gt;

&lt;p&gt;This article assumes you already know what inheritance is.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Initial Setup&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="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&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;"Animal makes noise"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&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;"Animal moved"&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="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&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;"Dog barks"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&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;"Dog runs"&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="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SuperDog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&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;"Super Dog howls"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&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;"Super Dog sprints"&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="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&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;"Duck quacks"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&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;"Duck Duck go!"&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;strong&gt;❓ Question 1&lt;/strong&gt;: What is the output?&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;Animal&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SuperDog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  

&lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SuperDog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;dk&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Duck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;dk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;dk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&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;❓ Question 2&lt;/strong&gt;: What does &lt;code&gt;virtual new&lt;/code&gt; mean?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does it still hide the base implementation?&lt;/li&gt;
&lt;li&gt;Does it still declare the method for further override?&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;❓ Question 3&lt;/strong&gt;: Guess the output!&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;Dog&lt;/code&gt; class like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&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;"Dog barks"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&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;"Dog runs"&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;Now what do you think this will print?&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;Dog&lt;/span&gt; &lt;span class="n"&gt;d3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;d3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;span class="n"&gt;d3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&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;Constructors and Virtual Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Constructors are meant to initialize the class state before it can be used.&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Base&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;"Base constructor"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Init&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;"Init Called: Base class"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Base logic (maybe logging or setup)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Derived&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Base&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_logMessages&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;_message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Initialization starts"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Derived&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_logMessages&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;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Init&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;_message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;_logMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Init Called: Derived class"&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;strong&gt;❓ Question 4&lt;/strong&gt;: Answer the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Does the above code run properly?&lt;/li&gt;
&lt;li&gt;What does the output look like?&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  ✅ Solutions:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Question 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;Animal&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Animal makes noise&lt;/span&gt;
&lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Animal moved&lt;/span&gt;

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Dog barks&lt;/span&gt;
&lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Animal moved&lt;/span&gt;

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SuperDog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Super Dog howls&lt;/span&gt;
&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Animal moved&lt;/span&gt;

&lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;sd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SuperDog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Super Dog howls&lt;/span&gt;
&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Super Dog sprints&lt;/span&gt;

&lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;dk&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Duck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;dk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Animal makes noise&lt;/span&gt;
&lt;span class="n"&gt;dk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Duck Duck go!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Question 2&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;virtual new&lt;/code&gt; declares a method that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hides the current base class implementation, and&lt;/li&gt;
&lt;li&gt;Allows the method to be overridden in future derived classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So yes, it hides the base method for the current class &lt;strong&gt;and&lt;/strong&gt; opens it for further overriding.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Question 3&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;If a method exists in the base class and is accessible (not &lt;code&gt;private&lt;/code&gt;), &lt;code&gt;base.MethodName()&lt;/code&gt; will work inside the derived class—whether it's overridden or hidden using &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animal makes noise
Dog barks
Animal moved
Dog runs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Question 4&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does the above code run normally?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Hell no!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assume we create an object 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;Derived&lt;/span&gt; &lt;span class="n"&gt;dd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Derived&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;What really happens&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CLR starts memory allocation for the object.&lt;/li&gt;
&lt;li&gt;Field &lt;code&gt;_message&lt;/code&gt; is declared and initialized.&lt;/li&gt;
&lt;li&gt;Field &lt;code&gt;_logMessages&lt;/code&gt; is declared, &lt;strong&gt;but not yet initialized&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The base class constructor is invoked (derived constructor hasn't run yet).&lt;/li&gt;
&lt;li&gt;Inside the base constructor, &lt;code&gt;Init()&lt;/code&gt; is called.&lt;/li&gt;
&lt;li&gt;Because it's virtual, it dispatches to &lt;code&gt;Derived.Init()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;Derived.Init()&lt;/code&gt;, &lt;code&gt;_logMessages.Add(...)&lt;/code&gt; is called—&lt;strong&gt;but &lt;code&gt;_logMessages&lt;/code&gt; is null!&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;💥 &lt;strong&gt;NullReferenceException&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Output before crash:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Base constructor
Initialization starts
Unhandled Exception: System.NullReferenceException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  💬 Final Thought:
&lt;/h3&gt;

&lt;p&gt;While this scenario may not be common in enterprise apps, it can easily slip in unnoticed—especially when refactoring or extending base functionality. Most exceptions are just mistakes we didn't anticipate.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you like this article please leave a comment, i am new to blogging but love programming, i am trying to expand my circle and would now to interact with others, Thanks&lt;/p&gt;
&lt;/blockquote&gt;

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