Skip to main content

Basics

Just some notes on basic C#, as I have recently been learning .NET

Microsoft - Tour of C#

Architecture

You can download .NET as an SDK or a runtime. The SDK is for developing applications, while the runtime is for executing applications that have been developed with the .NET SDK. The SDK includes runtimes required to run .NET applications.

More information on contents of the SDK and .NET runtime

Common Language Runtime (CLR) is a virtual machine that executes applications and generates / compiles code using a Just In Time (JIT) compiler. The JIT compiler translates Intermediate Language (IL) from compiled C# into machine code that the processor understands. C# is just used as an example here, and is not the only .NET language that compiles to IL. The JIT compiler has a feature called Tiered Compilation which enables the recompilation of individual methods at run time, which in turn supports quick compilation of large applications.

The CLR is responsible for Automatic Memory Management through the process of Garbage Collection

It is possible to use unmanaged resources within a .NET application. For example, a FileHandle attached to a FileStream must be explicitly released by the caller. The FileStream itself is however a managed object. Unamanged objects implement the IDisposable interface. IDisposable objects call the Dispose() method which releases any contained unmanaged resources.

More information on cleaning up unmanaged resources

More .NET Terminology

Garbage Collection

Input / Output

void TestInput()
{
  string formattingString = "Captured {0} input: {1}\n";

  Console.Write("\nInput a character, then press enter: ");
  int ascii = Console.Read();
  char ch = Convert.ToChar(ascii);
  Console.Write(formattingString, "character", ch);
  Console.ReadLine(); // Discard any left over input
    
  Console.Write("\nPress a key: ");
  ConsoleKeyInfo key = Console.ReadKey();
  Console.Write("\n" + formattingString, "key", key.KeyChar);

  Console.Write("\nEnter a line: ");
  string? line = Console.ReadLine();
  Console.Write(formattingString, "line", line);
}

Value Types

Microsoft - Reference Types

Microsoft - Value Types

Microsoft - Pointer Types

Interfaces

Microsoft - Interface

void PrintEnum(IEnumerable<int> obj)
{
  Console.WriteLine();
  foreach (var i in obj)
  {
    Console.Write("{0}, ", i);
  }
}

Generics

TODO: Differences from C++ templates

Generics are supported by class, struct, interface, and delegate types.

Nullable

Microsoft - Nullable

Boxing

Class

Supports single inheritance, where a class may inherit from a single base class and extend or define functionality.

Classes may inherit from multiple interfaces, but may only inherit from a single base class.

Struct

Supports multiple inheritance, where N interfaces can inherit from each other to create a single interface

LINQ

async / await

RPC / WCF

Microsoft - RPC Types