Sunday, October 9, 2011

- Introducing C# and the .NET Platform


.Net can be thought as run-time environment and comprehensive base class library.
Here is some advantages we have in .NET:
  • Interoperability with existing code
  • Support for numerous programming Languages
  • A common run time engine shared by all .NET-aware languages
  • Complete and total language integration
  • A comprehensive base class library
  • No more COM plumbing
  • A simplified deployment mode
CLR (Common Language Runtime) is the run-time layer shared by all .NET-aware languages and platforms, which locateload and manage .NET types. CLR also responsible for memory managementapplication hostingthread handling and performing various security checkersThe basic of the CLR is physically represented by a library name mscoree.dll (Common Object Runtime Execution Engine) which load automatically when an assembly is referenced for use and load the required assembly to the memory.
Runtime engine responsible for resolving the location of an assembly and finding the requested type within the binary by reading the contained metadata. CLR then lays out the type in memory, compiles the associated CIL into platform-specific instructions.
CTS (Common Type System) fully describes all possible data type and programming constants supports by runtime. 
On the other hand CLS (Common Language Specification) is subset of common type and programming construct that all .NET programming languages can agreed on. CLS rules apply only to those parts of a type that are exposed outside the defining assembly. the only aspects of a type that must conform to the CLS are the member definitions themselves(i.e., naming conventions, parameters, and return types). the implementation logic for a member may use any number of non-CLS techniques, as the outside world won't know the difference. You can instruct C# compiler to check your code for CLS compliance  using following attribute:
[assembly: System.CLSCompliant (true)]  

Managed Code: code targeting the .NET run-time. Binary Unit contains manged code called assembly.
Unmanaged Code: code that can not directly be hosted by .NET run-time
Here is a cool link which contains additional .NET-Aware Programming Languages: dotnetlanguages
CIL (Common Intermediate Language):
An assembly contains CIL code which is similar to Java bytecode and it will compiled to platform specific instructions when it's necessary (when a block referenced for use by the .NET runtime). CIL also contains Metadata which describes in vivid detail the characteristic of every type (class, structure, enumeration) as well as member of each type (e.g properties, methods, events) living within the binary. IntelliSence feature, Object-browsing utilities, and some other features are using Metadata
Manifest  external assemblies required by current assembly described using metadata which called manifest. manifest contains information such as version of the assembly, culture information, copyright information and so forth.
Once you compile any .NET code using .NET compiler, you end up with *.exe assembly which contains manifest, CIL instructions, and metadata describing each aspect of program classes. 
compiler & mscoee.dll & source code
ildasm.exe (Intermediate Language Disassembler): 
.NET Framework 4.0 SDK provides Intermediate Language Disassembler utility (ildasm.exe) in order to see any .NET assembly and investigate the contents, including the associated manifest, CIL code, and type metadata
can be accessed through Start/All Programs/ Microsoft Visual Studio 2010/  Visual Studio Tools/ Visual Studio Command Prompt (2010)/ and type ildasm or find it at (C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin) .  Alternatively search your computer for idasm.exe file. Idasm.exe may open *.exe or *.dll with ildasm and see all the details explain above (picture below). by double click on MANIFEST and other  methods you may see more details. Press Ctrl+M to see the metadata

idasm.exe
Each .NET-aware compiler produces nearly identical CIL instructions. Therefore all languages are able to interact within a well-defined binary arena.
Code must be Compiled on the fly before used because assemblies contains CIL instructionrather than Platform Specific Instruction.  
-Just In Time- JIT Compiler (Jitter) : compiles CIL into meaningful CPU instructions.
Type: is a general term to refer to a member from the set (e.g. class, interface, etc) 
Class Type: cornerstone of OOP, may compose of members (e.g constructors, properties, etc)
Class Characteristic:  
Sealed: Can not function as base class to other classes
Implement any Interface: Collection of abstract members that provide a contract between the object and object user. CTS allows a class to implement any number of interfaces.
Abstract vs Concrete: Abstract classes can not be instantiated directly they are intended to define common behavior for derived types 
Visibility: Controls if the class may be used by external assemblies or only from within the defining assemble.
Structure Type: UDTs (User-Defined Types) lightweight class type having value base semantics. sited for modeling geometric and mathematical data C# keyword struct.
Enumeration Types:  handy programming construct that allow group name value pairs C# keyword enum. 32-bit Integer is the default storage to hold each item.
Delegate Type: .NET equivalent of a type-safe, C-style function pointer. However it is a class derives from System.MulticastDelegate, rather than a simple pointer to a raw memory address. C# keyword delegate.
Delegates help to provide a way for one entity to forward a call to another entity and provide a function for the .NET event architecture.
The CTS defines following adornment which may associated with members:
  • visibility (e.g public, protected, private)
  • abstract (to enforce a polymorphic behavior on derived types)
  • virtual (to define a canned, but overridable implementation)
  • static (bound at the class level)
  • instance (bound at the object level)
Libraries: gives developers a well-defined set of existing code to leverage in their applications
NameSpace: is a grouping of semantically related types contained in any assembly
With help of Visual Studio 2010 Object Browser utility (View/ Object Browser ) you can observe assemblies referenced by current project the namespace within a particular assembly, the type within a given namespace, and member of specified type (bellow snapshot) 
Object Browser


A vast majority of .NET Framework assemblies are located under C:\Windows\Assembly and it called GAC (Global Assembly Cache) 

Deploying the .NET Runtime:
Microsoft provides dotNetFx40_Full_x86.exe(32-bit)dotNetFx40_Full_x86_x64.exe (64-bit) (77MB)setup package in order to install .NET base classes to a machine that does not have .NET installed. You may download the file form Microsoft website for free:
http://www.microsoft.com/downloads 
Note: Vista and Widows 7 are include the necessary .NET runtime.
In case your user needs to download the file using low-speed internet connection Microsoft also provides dotNetFx40_Client_x86.exe (32-bit) dotNetFx40_Client_x86_x64.exe (64-bit) (34MB) client profile which is a subset of the .NET base classes libraries and the necessary runtime infrastructure.


.NET assemblies can be developed and executed on non-Microsoft operating systems with help of CLI (common Language Infrastructure) and VSE (Virtual execution System) the .NET runtime engine. Refer to this website for more information about CLI and C# language specifications http://www.ecma-international.org/
Mono project is an open source distribution of CLI : http://www.mono-project.com/Main_Page
Portable.Net is another open source distribution of CLI : http://www.dotgnu.org/

Windows 8 Application:
Creating an running windows 8 application is only possible on Windows 8 not earlier version of windows.
WinRT ( Windows Runtime ): An entirely new runtime layer. Despite the fact it do some similar things as CLR do (e.g garbage collection and support multiple programming language) in doesn't have anything to do with CLR.

Reference: Pro C# 2010 and the .NET 4 Platform by Andrew Troelsen & Pro C# 5.0 and the .NET 4.5 Framework by Andrew Troelsen.



    No comments:

    Post a Comment