Saturday, November 26, 2011

.NET Basics Part 1

I have had a hard time trying to digest the concept of .NET for sometime. Why do we need .NET? We know that its possible to do everything with just the C Programming language. Or may be even C++. Then why .NET? Or why even Java? VB, C#, WPF, WCF, WWF, .NET 1.1/2.0/3.0/3.5/4.0/4.5......???? You go to the interviews and they ask you SQL, PL-SQL, Threading, JQuery, SOA... disgusting isn't it???

Actually, the problem is not with the technology. Its the problem with my inability to comprehend the beauty of the capabilities of the Microsoft .NET Technology. I have put all my efforts in getting certified and clearing the interviews, rather than to understand the concepts and grow as a developer. Hmmm... Then what do i do?? Write a blog? yeah. I think its a good idea. Why???

Why because its important for me to start. Ok. From where do i start. --> Basics.

What was the intention behind developing the .NET technology?

1. Provide Cross Platform independence
2. Language Interoperability enhancing the interoperability of applications.
3. To make significant improvements in Code Reuse, Resource management, multi-language development, security, deployment and administration.

-----------------------------------------------------

What is .NET?

Microsoft .NET is a technological platform using which we can develop applications on the series of Microsoft Windows Operating System. Its a collection of technologies that help in creating and running robust, reliable, secure, scalable and distributed applications.

(Hold on. Only Windows OS?? What about other OS platforms?

Well, Microsoft only supports .NET on windows platform officially. Mono is another platform similar to .NET, built on the specifications given by Microsoft which can run on other OS Platforms. Mono is being built by a group of open source developers.

Ok. So what does that mean? I wont be able to run .NET on other OS? .NET is not truely platform independent?

Well as of now YES. Its not a truely platform independent software framework like Java which can run on any OS.
Oh wait a minute? Isn't this insane? What if my clients say that we need a technology that works on any platform? Like Java. I am dead.

Actually more than 80% of the companies across the world today use the windows operating system because it is more reliable than any other OS. So relax. Situation is not that worse. .NET works best with Windows OS than any other technology. Also Microsoft is coming up with a lot of new technologies for enterprise development that work seamlessly with .NET than any other framework.)

-----------------------------------------------------

.NET consists of the following components:

1. .NET IDE: Visual Studio 2003/2005/2008/2010 Integrated Development Environment (IDE).
2. .NET Framework: ??

What is an Visual Studio IDE?

Integrated Development Environment. Using this tool we can design, develop and deploy Console, Windows and Web Based applications. We can even develop WebServices as well. Below is the list of templates that are available in each of the IDE's.

Visual Studio 2003 IDE : Released in 2003. Supports .NET Framework 1.0/1.1
Console | Windows | Web Applications | .NET Web Services

Visual Studio 2005 IDE: Released in 2005. Supports .NET Framework 2.0
Console | Windows | Web Applications | AJAX enabled Web Applications | .NET Web Services

Visual Studio 2008 IDE : Released in 2008. Supports .NET Framework 2.0/3.0/3.5
Console | Windows | Web Applications | AJAX enabled Web Applications | .NET Web Services
| WPF, WCF and WWF applications |

Visual Studio 2010 IDE : Released in 2010. Supports .NET Framework 2.0/3.0/3.5/4.0
Console | Windows | Web Applications | AJAX enabled Web Applications | .NET Web Services
| WPF, WCF and WWF applications | Silverlight | MVC Architecture


Ok. Now what is a .NET Framework?

It's the microsoft implementation of the Common Language Infrastructure (CLI).
(CLI is a standard and a set of specifications for development and execution environments where languages and libraries work together.)
.NET Framework is a consistent object-oriented programming framework which helps us to design, develop, deploy applications which are developed using different programming languages like C#. VB, VC++, J# etc.

What are the features or advantages of .NET Framework?

Cross Platform (among Windows OS's) : Create application in one windows OS and run it on any Windows OS.

Multiple language support and Language Interoperability: Develop application in one .NET language( say C# ) and that app can interact with another application developed in another .NET language( say VB ). This helps to bring developers of different programming languages together.

Consistent Object-Oriented Programming Model across all .NET languages:

Code Reuse:

Code Specialization:

Automatic Resource Management: Resources such as Files, Memory, Network and Database connections are handled automatically.

Easy of Deployment: Applications are deployed in the form of assemblies. Here no registry entries are required. It is possible to have different versions of an assembly existing at any point of time.

Develop Secure applications:

Administration Tools Available:

-------------------------------------------------------------


The .NET framework evolution can be seen as below.



-----------------------------------------------------

.NET Framework consists of primarily two parts:
1. Framework Class Library (FCL) : which encompasses the Base Class Library (BCL)
2. Common Language Runtime (CLR):

What is Framework Class Library (FCL)?

The FCL is a collection of thousands of reusable classes (within hundreds of namespaces), interfaces and value types. The Base Class Library is a part of FCL and provides the most fundamental functionality, which includes classes in namespaces System, System.CodeDom, System.Collections, System.Diagnostics, System.Globalization, System.IO, System.Resources and System.Text.

(Why do we need a Class Library? What does FCL have that BCL doesn't?
A Class Library is an Object-Oriented collection of namepspaces->Classes->functions and controls which provide most of the common functionality that can used in the programming code like String management, Data Collection, Database interaction, File Handling, Threads, Delegates, Memory Management etc. This will reduce the burden on the programmer to perform common tasks and help him concentrate on the business functionality thus fastening the process of application development.)


What is Common Language Runtime (CLR)?

CLR is an execution environment in which all applications developed using .NET are executed. It performs major tasks such as code verification, compilation and execution, memory management, garbage collection, type safety, exception handling, debugging, security and many more.

Lets dig into the salient features of CLR in detail:

Automatic memory and resource management: (Garbage Collection)
The CLR allocates memory to the applications from the managed heap memory. But if sufficient memory is not available Garbage Collector (GC) is automatically invoked. This GC deallocates memory from those objects which are no longer in use or no longer are being referenced by other objects. CLR also resolves the issues of memory leaks and invalid memory references.

CLR imposes Code Access Security (CAS): CAS is the CLR's security system that enforces security policies by preventing unauthorized access to protected resources and operations. CAS implements strict type and code verification infrastructure called the Common Type System (CTS).

Managed Code:

In the .NET platform, applications are not compiled directly into the machine language. Because, the machine language generated in one OS, may not run in another OS. The application might have to be recompiled in the new OS to generate the machine specific machine language.

Instead the CLR compiles the code into an intermediate language called the Microsoft Intermediate Language (MSIL) or simply knows as the Intermediate Language (IL). This IL is machine independent.

Along with the IL, metadata is also generated by the CLR which describes the attributes, classes, methods etc present in the code being compiled. The IL + Metadata generated during compilation is what forms the Assembly.

These assemblies can be packaged into a product and sold to the customers. The customers will run the application product on their system. Customers need to install the .NET Framework onto their system before executing the application product.
Now when the customer tries to execute the application on his/her system, the Just in Time Compiler (JIT) translates the IL into the native machine code, which is CPU specific.

This IL that runs on the CLR, is also known as the Managed Code. IL is the managed code developed on the .NET Framework. CLR provides GC, exception handling and type safety for the managed code.


Cross Platform Independence:

The IL generated using .NET framework can run on any OS platform that has the .NET framework installed. Hence providing cross platform support.

Language Interoperability:

.NET applications developed using any .NET language are all compiled into the IL which the .NET Framework understands. Hence different applications developed in various .NET languages can work together.

Common Language Specification (CLS):

CLR has a set of specifications or rules that any programming language must comply with if it is targetting the .NET platform. Only then such a language is termed as CLS compliant or .NET compliant. These set of specifications or rules are called CLS.


Common Type System (CTS):

CTS defines how the data types are declared, used and managed in .NET.

-----------------------------------------------


Additional information on the concepts of .NET Framework:

Assembly:

The IL and the metadata generated during code compilation is called as the Assembly. The generated assembly can be in the form of a .EXE or a .DLL.

.EXE or Executable file:


Dynamic Link Library:



Assembly is made up of 4 items

1. IL
2. Metadata
3. Manifest contains the information related to the assembly, such as

i.. Assembly name
ii. Assembly version number: Major, Minor, Revision and Build number. CLR uses it to enforce security policy.
iii. Culture: Communication Languages that the assembly supports.
iv. Strong name Information: Public key from the publisher.
v. List of files that form the assembly: Hash of each file and the filename.
vi. Type Reference Information: Refer a type to the file which contains the declaration and implementation.
vii. Info on Referenced assemblies: List of assemblies that are being statically referenced by an assembly.
    Contains the dependent assembly's name, metadata and public key (if the assembly being referenced is Strong Named).

4. Resource Files: Audio/Video/Image files

So now the assembly includes IL + metadata + manifest + Resource files.

Static Assembly:

Stored in the Portable Executable PE format on a disk.

Dynamic Assembly:

Runs directly from the memory without being saved on the disk before execution. After execution, if required, it can be saved on the disk.

Private Assembly:

If a single application uses an assembly its called as private assembly. This type of assembly is not registered on Global Assembly Cache GAC.

Shared Assembly:

An application that is registered on the GAC so that multiple applications running on the same system can utilize the assembly.

Global Assembly Cache GAC:

It is a computer or machine level central repository for registering the assemblies so that different applications on the same machine can utilize it. An assembly can be registered on GAC using,

1. Gacutil.exe utility:
2. Windows Explorer that is assigned to drag assemblies into cache.

Strong Naming of an Assembly:

Strong name consists of the assembly's identity. It can be assigned using the sn.exe utility.

1. Assembly name
2. Version Number
3. Culture
4. Public Key
5. Digital Signature

Benefits of a strong name to an assembly.

Globally Unique.
Ensures that the assembly came from the original publisher.
Facilitates integrity check: We can make sure that the assembly has not been tampered since its last build.
Enables side by side execution of different versions of the same assembly.

------------------------------------------------------