Sunday, September 21, 2014

C# 5.0 and .NET Framework some basic key points summary - Part 1

Several months ago, I started to prepare for the .NET 70-483 exam. To make sure that I remember all the things I learn, I started extracting the most important points, doing practical examples in C#, and searching for improvements and more explanation on the internet, especially StackOverflow. For several weeks it was OK, but after a while, I started to feel that the preparation speed is very slow like that, and I dropped the idea. However, I managed to write a very nice summary of some important features of C#. This post will contain this information, because I thought it is worth sharing. My main sources of inspiration were Andrew Troelsen's book on Pro C# 5.0 and .NET Framework 4.5 which I bought exactly for this purpose, and StackOverflow. You will find the references at the end of the article. If I omitted something, it was unintentional. Note also that this article is divided in three parts, and I do not claim having covered all the aspects! The references are at the end of part 3!

.NET building blocks:
  1. CLR - Common Language Runtime. It is responsible for the location, loading, and managing of the .NET objects. It also takes care of memory management, security checks, thread management and application hosting
  2. CTS – Common Type System. This block describes all the possible data types, programming structures supported and the interaction between these. It describes also the how they are represented in the .NET metadata information. Documents how types must be defined in order to be hosted by CLR.
  3. CLS – Common Language Specification. Defines all the data types and programming constructs that are supported across all .NET languages. If you want to reuse you library with another .NET language, it must comply with CLS. It is a subset of the CTS type.
Base class libraries provide access to core features of the .NET platform, as graphic rendering, database access, File I/O, Security, threading, Web API etc.

Some C# core features:
  1. No pointers (But you can if you need)
  2. Automatic memory management
  3. Attribute-based programming through decorations (like Obsolete)
  4. Overloading of the operators
.NET 2.0 introduced (not exhaustive):
  1. Partial keyword
  2. anonymous functions
  3. generics
.NET 3.5 introduced (not exhaustive):
  1. LINQ (strongly typed queries)
  2. building shape with anonymous types
  3. Extensions
  4. lambda operator
  5. initialization at creation time
.NET 4.0 introduced (not exhaustive):
  1. optional parameters
  2. dynamic lookup of members through dynamic keyword
.NET 4.5 introduced (not exhaustive):
  1. await and async keywords
  2. PLINQ
.NET binaries contain platform agnostic IL(Intermediate Language) code and type metadata.
Binary blobs are called assemblies.

Assemblies contain CIL that is compiled to platform specific code only when required by .NET Runtime.

Assemblies contain Metadata that describes the types inside the assembly.

The manifest represents the metadata about the assembly itself, which describes the version of the assemble, the culture and the various libraries it depends upon (externally referenced assemblies).

The C# compiler is csc.exe

Different .NET languages usually generate nearly identical CIL code.

Benefits of CIL: different .NET languages interaction, platform-agnostic code
CIL is compiled at request by JIT, that does the compilation based on the platform, optimizing the resultant code, and after that stored in memory for later use. You can pre-JIT using ngen.exe

.NET Type Metadata: Used for Reflection, Intellisense, Debugging, WCF, late binding, serialization. It contains descriptions about the types in the assembly(class, struct, enum), and their properties, methods and events.

Assembly Manifest: metadata about the assembly itself, describing the version, all externally required assemblies, copyright information etc. Like type metadata, it is generated by the compiler.

Type is the general term to describe the member of the following set: class, interface, struct, enumeration, delegate.

  • if sealed, then cannot be inherited (are final)
  • interfaces : a class can implement any number. It defines the interaction contract between objects
  • abstract: used to define common behavior, cannot be instantiated.
  • visibility: public, internal – for outer classes, private and protected for inner classes.
  • represent a set of abstract member definitions, which may be supported by a class or structure.
  • by convention begin with I
  • are used to request a functionality in a polymorphic way
  • best suited to model geometric and mathematical data
  • created with struct keyword
  • can be thought as a lightweight class
  • are sealed by default, cannot be inherited or inherit
  • handy construct that allow you to group name/value pairs
  • abstraction from concrete values
  • usually are 32 bit integer, but can be altered
  • derive from System.Enum
  • equivalent of a type-safe C-style function pointer
  • is a class
  • derives from System.MulticastDelegate
  • foundation of the .NET event architecture
CTS Intrinsic types:
  • the naming can vary by language
  • ultimately resolve to the same type defined in mscorlib.dll
CLS rule: It applies to those parts of a type that are exposed outside the defining assembly.

The CLR – the collection of services that are required in order to run a given compiled unit of code. 

The core is in mscoree.dll (common runtime execution engine). When an assembly is referenced, mscoree is automatically loaded and in turn loads the required one.

mscorlib.dll – core assembly that contains types that encapsulate a wide variety of common programming tasks.

namespace – grouping of the semantically related types. An assembly can contain any number of namespaces.

Topmost root namespaces in .NET base class library: System, Microsoft (which is the namespace to access functionality related to Windows operating system)

Accessing a namespace is done with the using keyword. You can avoid this, by specifying the fully qualified name of the type within the namespace.

A vast majority of the .NET Framework assemblies are located under a specific directory termed the global assembly cache (GAC).

Ildasm.exe is used to analyze assemblies, and specifically analyze the manifest, CIL code, type metadata.

Windows 8 applications are represented as tiles on the metro screen. Metro applications can be created for the desktop, or WinRT, which is not the same as .NET CLR, but is similar.

WinRT applications are created with a whole new set of namespaces, that start with Windows.*

The Developer command prompt – allows access to the .NET development tools without modifying the user's computer's PATH environment variable to allow access from a standard console.

C# source files are compiled using csc.exe.
csc /t:exe /r:System.Windows.Forms.dll /out:MyApp.exe MyBox.c
s TestApp.cs

C# compiler, the target, additional external library, output name, first class, second one

To group multiple compile instructions, we can use response files (*.rsp).

Default response file is csc.rsp, in the install location of the version of the Framework.

Anatomy of a simple C# Program:
  1. In C# it is not possible to create global functions or global points of data, everything must be contained within a type definition.
  2. C# is case sensitive
  3. Every executable application must contain a Main() method, that can be of different signature. If multiple classes contain a Main() method, then you must inform the compiler which is the main one :)
  4. Main() signature can vary
  5. By convention, returning 0 from main assumes that the program executed successfully
  6. In Windows, an application's return value is contained in the environment variable %ERRORLEVEL% which can be queried after execution
  7. void return means return 0;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.