Tuesday, 20 January 2015

.NET Tutorial for Beginners

What is .NET Framework  ?

My goal with this post is to start from beginning and try to give some basic idea on .NET and this is not a comprehensive session  :)

          .NET Framework is is a software framework developed by Microsoft that runs primarily on Microsoft Windows. It includes a large class library known as Framework Class Library (FCL) and provides language interoperability (each language can use code written in other languages) across several programming languages. Programs written for .NET Framework execute in a software environment (as contrasted to hardware environment), known as Common Language Runtime (CLR), an application virtual machine that provides services such as security, memory management, and exception handling. FCL and CLR together constitute .NET Framework.

          .Net was originally known as NGWS (Next Generation Windows Services) which was said to be an Internet based platform of Next Generation Windows Services.

.Net Framework Versions

# Common Language Runtime Engine(CLR)

The Common Language Runtime is the foundation (heart) of .Net Framework. All .Net programs execute under the super vision of CLR.

Managed Code : Code that targets the CLR is known as managed code.
Unmanaged Code : Code that does not targets the CLR.

If you are using some third party software example VB6 or VC++ component they are Unmanaged code as .Net runtime (CLR) does not have control over the source code execution of the language. For example, VC++ has MSCRT40.DLL, VB6 has MSVBVM60.DLL,and Java has Java Virtual Machine etc. Similarly, .NET has CLR.

* Run time is an agent that manages codes at execution time and code management is the fundamental principle of run time

# Responsibilities of CLR

• Garbage Collection: - CLR automatically manages memory thus eliminating memory leaks. When objects are not referred, GC automatically releases those memories thus providing efficient memory management.

• Code Access Security: - CAS grants rights to program depending on the security configuration of the machine. Example the program has rights to edit or create a new file but the security configuration of machine does not allow the program to delete a file. CAS will take care that the code runs under the environment of machines security configuration.

• Code Verification: - This ensures proper code execution and type safety while the code runs. It prevents the source code to perform illegal operation such as accessing invalid memory locations etc.

• IL (Intermediate language)-to-native translators and optimizer’s:- CLR uses JIT, compiles the IL code to machine code, and then executes. CLR also determines depending on platform what is optimized way of running the IL code.

# Microsoft Intermediate Language – MSIL

MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or Common Intermediate Language (CIL). During the compile time , the compiler convert the source code into Microsoft Intermediate Language (MSIL) .Microsoft Intermediate Language (MSIL) is a CPU-independent set of instructions that can be efficiently converted to the native code. During the runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System.
When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE) file . Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations

C# Code > C# Compiler > IL > .NET Runtime > JIT Compiler > Machinecode > Execution

.NET Assembly

Microsoft .Net Assembly is a logical unit of code, that contains code which the Common Language Runtime (CLR) executes. It is the smallest unit of deployment of a .net application and it can be a .dll or an exe . Assembly is really a collection of types and resource information that are built to work together and form a logical unit of functionality. It include both executable application files that you can run directly from Windows without the need for any other programs (.exe files), and libraries (.dll files) for use by other applications

Private and Public Assembly

A private Assembly is used only by a single application, and usually it is stored in that application's install directory. A shared Assembly is one that can be referenced by more than one application. If multiple applications need to access an Assembly, we should add the Assembly to the Global Assembly Cache (GAC). There is also a third and least known type of an assembly: Satellite Assembly . A Satellite Assembly contains only static objects like images and other non-executable files required by the application

# .NET Framework class library
The .NET Framework class library is a library consisting of namespaces, classes, interfaces, and datatypes included in the .NET Framework. This library is organized into namespaces that contain functionally related groups of classes. These namespaces are divided among different categories, such as data access, common types, debugging, file access, network communication, security, Windows applications, Web applications, Web services, XML data etc.

1. Base Class Library

The Base Class Library is a simple runtime library for modern programming languages. It serves as the Standard for the runtime library for the language C# as well as one of the CLI Standard Libraries. It provides types to represent the built-in data types of the CLI, simple file access, custom attributes, security attributes, string manipulation, formatting, streams, collections, among other things. It defines types in the following namespaces.

System: Defines the Object class which all reference type objects derive from (including value-type objects) and the Value Type class which all value type objects derive from. It also defines the base data types like integers, floating point numbers, character, strings, Boolean, enumeration and more. Support for the environment and platform and a command-line interface is provided along with base classes for exceptions and attributes. It defines arrays and delegates, mathematical functions and many other types.

System.Collections : Defines many common container types used in programming, such as dictionaries, hash tables, lists, queues and stacks
System.Collections.Generic : Defines generic types of the container types in the System.Collections namespace
System.Diagnostics : Defines types that provide the ability to diagnose applications. It includes event logging, performance counters, tracing and interaction with system processes.

System.Globalization : Defines types that define culture-related information, including language, country/region, calendars in use, format patterns for dates, currency and numbers and sort order for strings.

System.IO : Defines type that enable reading from and writing to different streams, such as files or other data streams. Also provides a connection to the file system.

System.Security : Defines types that represent the security system and permissions.

System.Security.Permissions : Defines types that control access to operations and resources based on policy.

System.Text : Defines types that support various character encodings, regular expressions and a more efficient mechanism for manipulating strings.

System.Threading : Defines types that enable multithreaded programming.

2. Runtime Infrastructure Library

The Runtime Infrastructure Library provides the services needed by a compiler to target the CLI and the facilities needed to dynamically load types from a stream in a specified file format.It defines types in the following namespaces

System.Reflection : Defines types that provide a managed view of loaded types, methods and fields, and that can dynamically create and invoke types. These types are relevant to the program runtime

System.Runtime.CompilerServices : Defines types that provide functionality for compiler writers who use managed code to specify attributes in metadata that affect the run-time behavior of the Virtual Execution System.

System.Runtime.InteropServices Defines types that support Platform Invocation Services (P/Invoke).

3. Network Library

The Network Library provides simple networking services including direct access to network ports as well as HTTP support.It defines types in the following namespaces

System.Collections.Specialized : Defines specialized and strongly-typed collections; for example, a linked list dictionary, a bit vector, and collections that contain only strings.

System.Net :  Defines types that provide a simple programming interface for a number of network protocols.

System.Net.Sockets : Defines type that provide a managed implementation of the Windows Sockets (Winsock) interface for developers who need to tightly control access to the network

4. Reflection Library
The Reflection Library provides the ability to examine the structure of types, create instances of types and invoke methods on types, all based on a description of the type.It defines types in the following namespaces:

System.Globalization : Defines type that provide information about a specific culture (called a locale for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used and formatting for dates and sort strings.

System.Reflection : Defines types that provide a managed view of loaded types, methods and fields, and that can dynamically create and invoke types.

System.Security.Permissions : Defines types that control access to operations and resources based on policy. These types are relevant to the reflection.

5. XML Library

The XML Library provides a simple "pull-style" parser for XML. It is designed for resource-constrained devices, yet provides a simple user model.It defines types in the following namespace

System.Xml : Defines types for processing XML

6 . Extended Array Library

The Extended Array Library provides support for non-vector arrays. That is, arrays that have more than one dimension or arrays that have non-zero lower bounds.The Extended Array Library doesn't add any extra types, but it does extend the array-handling mechanism.

7 . Extended Numerics Library

The Extended Numerics Library provides support for floating-point (System.Single, System.Double) and extended-precision (System.Decimal) data types. Like the Base Class Library, this library is directly referenced by the C# standard.

8 . Parallel Library

The Parallel Library provides easy parallelism for non-expert programmers, so that multithreaded CPUs can be exploited

9 . Vararg Library

The Vararg Library provides support for dealing with variable-length argument lists
#  Important Namespaces Chart

C# - Data Types  & Operators 

The data types directly contain data. Some examples are int, char, float, which stores numbers, alphabets, floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

C# Integral Types Value :

Floating, Double & Decimal : Precision and Range

The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class.
// Example to find the Size Of  Data Types
using System;
namespace Intlongvalue
    class Program
        static void Main(string[] args)
            Console.WriteLine("Size of int: {0} ::::: Size of Long : {1}", sizeof(int), sizeof(long));

C# - Operators
     •Arithmetic Operators, Relational Operator and Logical Operators

Arithmetic Operators

Relational Operators

Logical Operators