Intro to .NET

  1. Background
    1. Progression of Windows programming through time
      Evolution of Windows Programming
      Platform Windows API (WinAPI) .NET Framework and Windows Forms Windows Presentation Foundation (WPF) Windows Universal Platform (UWP)
      Year Introduced 1985 2002 2006 2012
      Programming Languages C, Visual Basic C#, VB.NET, J#, Managed C++ C#, VB.NET, F#, C++/CLI C#, VB.NET, F#, C++/CX, JavaScript/HTML
      Platforms Windows 1 → NT Windows 98 → XP Windows Vista → 7 Windows 8 → 10
    2. Since version 1.0, a C-based Windows API has allowed programs to interact with the Windows OS
      1. The API has largely been tied to hardware architecture, and great care has gone into making it backwards-compatible
        1. Win16 - API for the first version of Windows (running on DOS) that was 16-bit
        2. Win32 - for 32-bit Windows
        3. Win32s - subset of the Win32 API for the Windows 3.1x family
        4. Win64 - for 64-bit platforms
      2. Example: Hello World program (written in C using the Win32 API) is over a hundred lines of code!
    3. Today, programmers typically use .NET to create Windows applications
      1. .NET is a free and open-source framework for Windows, Linux, and Mac OS
      2. .NET started as .NET Framework in 2002 for building Windows apps and web applications on Windows OS
      3. An open-source .NET Framework port called Mono Project was created (with little Microsoft support) for Linux, Unix, FreeBSD, Mac OS, Solaris
      4. Microsoft created the open-source version of .NET Framework called .NET Core in 2016, which targeted Windows, Linux, and Mac OS
      5. .NET primary languages: C#, Visual Basic .NET, Managed C++, and F#
      6. Third-party languages: Ada, COBOL, LISP, Perl, Python, Ruby, and many more
    4. Three primary ways to create Windows apps
      1. Windows Forms
      2. Windows Presentation Foundation (WPF)
      3. Universal Windows Platform (UWP)
  2. Common Language Infrastructure (CLI)
    1. CLI is an open standard created by Microsoft that describes the executable code and runtime environment that are implemented by Microsoft's .NET Framework, .NET Core, Mono, and Portable.NET
    2. Programs written in a .NET language are compiled into Common Intermediate Language (CIL), which is sometimes called Microsoft Intermediate Language (MSIL)
      1. CIL is machine-independent and does not need to be re-compiled for different platforms
      2. Depending on the application, the CIL is compiled into an executable (.exe), a dynamically linked library (.dll), or some other exension
      3. These packaged files are called assemblies
    3. The Common Language Runtime (CLR) is the virtual machine that executes CIL code
      1. The CLR uses a just-in-time compiler (JIT compiler) to transform the CIL into machine code for the platform it is running on
      2. CLR handles memory management (garbage collection), type safety, and exception handling
    4. .NET is architected much like Java
      Comparison between .NET Framework and Java
  3. C# background
    1. C# (pronounced "C-Sharp") was created in 2000 by Microsoft for the .NET platform
    2. Anders Hejlsberg led the development team at Microsoft
    3. C# is said to be a combination of the best things from C++ and Java
    4. According to the Aug 2023 TIOBE index, C# is ranked #5
    5. C# is a used in Unity to make games, Xamarin to make cross-platform mobile apps, and Visual Studio to write GUI programs
  4. C# console application
    1. Start Visual Studio 2022 and create a project:
      1. Click Create a new project
      2. Choose Console App (.NET Core) and click Next
      3. Project name: HelloCSharp
      4. Click Create
    2. This C# program outputs "Hello, C#!" to the console 10 times
      using System;
      
      namespace HelloCSharp
      {
          class Hello
          {
              static void Main(string[] args)
              {
      			for (int i = 0; i < 10; i++)
      			{
      				Console.WriteLine("Hello, C#!");
      			}
              }
          }
      }
      
    3. Syntax
      1. using directive is like import in Java, used to simplify code by allowing types in a namespace to be referenced without preceding namespace
      2. namespace keyword used to declare a scope of related objects, similar to package in Java
      3. class keyword used to declare a class
      4. All executable programs have a static void Main() method, which is the first method executed
    4. Best practice is to save code in a file named after the enclosed class name: Hello.cs
    5. Compiled into the HelloCSharp assembly HelloCSharp.exe which can be executed by the CLR
    6. Can view the CIL in HelloCSharp.exe by running the ildasm (IL Disassembler) tool from the Visual Studio Command Prompt

      ildasm screenshot
      				
      .method private hidebysig static void  Main(string[] args) cil managed
      {
        .entrypoint
        // Code size       30 (0x1e)
        .maxstack  2
        .locals init ([0] int32 i,
                 [1] bool CS$4$0000)
        IL_0000:  nop
        IL_0001:  ldc.i4.0
        IL_0002:  stloc.0
        IL_0003:  br.s       IL_0014
        IL_0005:  ldstr      "Hello, C#!"
        IL_000a:  call       void [mscorlib]System.Console::WriteLine(string)
        IL_000f:  nop
        IL_0010:  ldloc.0
        IL_0011:  ldc.i4.1
        IL_0012:  add
        IL_0013:  stloc.0
        IL_0014:  ldloc.0
        IL_0015:  ldc.i4.s   10
        IL_0017:  clt
        IL_0019:  stloc.1
        IL_001a:  ldloc.1
        IL_001b:  brtrue.s   IL_0005
        IL_001d:  ret
      } // end of method Hello::Main
      
    7. Because CIL can be reverse engineered, many developers use obfuscators to make reverse engineering very difficult if not impossible