Intro to the .NET Framework

  1. Background
    1. Since version 1.0, a C-based Windows API has allowed programs to interact with the Windows OS
    2. 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
    3. A Hello World program (written in C using the Win32 API) is over a hundred lines of code!
    4. Most GUI programmers today don't write in C; they use a .NET language that encapsulates calls to the Windows API
      1. Microsoft started developing the .NET Framework in 2000 and released version 1.0 in 2002
      2. Current version is 4.6, released in July 2015
      3. Initially targeted Windows OS, but Mono Project (sponsored by Novell) has supported Linux, Unix, FreeBSD, Mac OS X, Solaris
      4. Microsoft recently created the open source CoreCLR which targets Windows, Linux, Mac OS X, FreeBSD
      5. Primary languages: C#, Visual Basic .NET, Managed C++, and F#
      6. Third-party languages: Ada, COBOL, LISP, Perl, Python, Ruby, and many more
  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, Mono, and Portable.NET
    2. Programs written in a CLI (or .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 .NET programs (Microsoft's implementation of CLI)
      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 Framework is architected much like Java
      Comparison between .NET Framework and Java
  3. C# console application
    1. 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#!");
              }
          }
      }
      
    2. Common practice is to save code in a file named after the enclosed class name: Hello.cs
    3. Compiled into the HelloCSharp assembly HelloCSharp.exe which can be executed by the CLR
    4. 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
      
    5. Because CIL can be reverse engineered, many developers use obfuscators to make reverse engineering very difficult if not impossible