Archive

Archive for the ‘.Net’ Category

Understanding the .Net Framework


The main feature of .Net platform is its runtime system which is known as Common Language Runtime (CLR).CLR is the engine that handles the parsing and execution of all .NET application instruction. The CLR is the foundation upon which the entire .NET platform is built.

Code Management and Execution- Code management within the CLR covers most of the basic activities of loading and executing .NET programs. The key task of code management is converting the Intermediate Language (IL) files created by the command line compilers into native code for the target machine. Command line compilers do not create actual executable code; they create a form of compressed code called IL. This IL then converted to native code to run.

There are some important advantages of using IL. This IL makes code operating system independent. You can compile your code once and then ship it to many different platforms or devices for execution. That’s because the actual creation of the executable code is handled by the CLR on the host device itself.

Step-1: Create the source code. You can use any text editor to crate .NET source code files.

Step-2 & 3: A command line compiler accepts the source code as input and produces a file (.DLL or .EXE) that contains parsed code and additional metadata that describes the compiled component.

net1

Step-4 & 5: The parsed code and metadata is delivered to the Just-in-time (JIT) compiler that converts the parsed code into executable code for that operating system and processor. For ASP.NET applications, this last step occurs the first time a user request a page from the Web application.

Note:- It is important that the JIT-ed file is stored on disk for future use. Repeated requests to a component will be compiled to native code only the first time. All subsequent requests are routed directly to the native compiled file. This means that all ASP.NET applications executed at “native-code speed” every time. It also means that this conversion to native code happens only once in the lifetime of the component. The only times this changes is when the source code is changed; at that point, the entire cycle mentioned above starts again.

Categories: .Net, Framework Tags: ,

Understanding CLR components

April 7, 2009 Leave a comment

CLR consists of various components that provide the run-time environment and run-time services. These components include– Class Loader,MSIL to native code compiler,Code manager,Garbage Collector,Security Engine,Type Checker,Thread Support,Exception Manager,Debug Engine,COM Marshaler,Base Class Library Support.

For a program to run within the CLR and benefit from the managed execution environment,you need to write the source code of the program in a CLS-compliant language.The compilers of CLS-compliant languages compile the source code and generate an intermediate code,called MSIL Code and Metadata. Before you execute the MSIL code,you need to compile it into CPU-specific instructions. To execute the code,the runtime requires information about the code that is in the metadata. The MSIL code and metadata are located in a “Portable Executable File”.

  • When you execute a portable executable file,the  Class Loader loads the MSIL code and the metadata from the portable executable file into run-time memory.Before the code is executed,it is passed to the native code compiler for compilation.
  • The IL to native code compiler contains just-in-time(JIT) compiler for different CPU architectures and compiles the IL code into a native instruction set.
  •  After the MSIL code and the metadata are loaded into memory,the Code Manager calls entry-point method,which is the Main,WinMain or D11Main method. During the execution of the entry-point method,the code manager places the objects in memory and controls the execution of the program.
  • The Garbage Collector performs periodic checks on the managed heap to identify the objects that are no longer required and removes them from the memory.
  • During program execution,the Type Checker ensures that all objects and values,and the references to those objects and values,have a valid type.
  • The Security Engine of the common language runtime(CLR) enforces restrictions on the code and controls the access to system resources such as hard disk,network connections,and other system resources.
  • The .NET Framework allows a single process to be divided into one or more subprocesses called application domains. Each application domain can contain one or more threads. The run-time monitors all the threads that have executed code within its process.
  • COM Marshaler performs marshaling of data when data passes between managed and unmanaged execution environments. Marshaling manages the different representations of data across different execution environments. It performs the necessary conversions in data formats between managed and unmanaged code.
  • During the execution process, a program depends upon the functionality provided by the .NET Framework class library .The CLR provides Base Class Library Support to the programes that are running within the managed execution process.
  • The CLR also provides a common Debug Engine which enables you to debug an applicationwritten in any language supported by the .NET framework. The common Debug engine supports debugging applications running on local and remote machines.
Categories: .Net, Framework

Code practice – String usage in .NET

April 6, 2009 3 comments

String in .Net, is an Immutable object (Object cannot be modifed). Hence memory allocated for a string remains even if you alter/modify the string through code, forcing the CLR to create new space for the modified string.

//Code Illustration in C#.Net

string str = ""; //memory created for str and value "" is stored
str = "String is Immutable" //new memory space is allocated for str
                            //and value is stored
str += str  + " in .NET" //Again, new memory allocation

This means more string operation you do with, more memory is occupied, though its with the same string variable. Better approach would be to use StringBuilder to do all appends and get the final output as StringBuilder class provides an efficient way to append. And its mutable.

Also string str = “” and string str = String.Empty is not the same in terms of memory allocation. the first creats an object in the memory and the latter does not. Hence prefer the latter

Avoid ToLower() and ToUpper() in code (Remember strings are immutable)

–Writing code is easy, writing good code is not.  Learn, Implement, Share–

.NET Framework: Introduction


Microsoft’s .NET platform is an entirely new way to build applications including web applications. ASP.Net was built on top of the .NET platform and takes full advantage of the power and flexibility of the .NET  framework.

Advantage of .NET platform

There are a number of powerful features and services within the .NET platform; however there are four in particular that have a great impact on ASP.NET applications. Those are-

  1. OS-Neutral Environment- One of the most important aspects of the .NET platform is that it is essentially operating system neutral. The design of the .NET run time library makes it possible to build both desktop and web solution without ever making direct calls to the underlying operating system at all. This means that ASP.NET developers need to know almost nothing of the windows operating system in order to built and deploy their solutions.
  2. Device Independent Microsoft has designed the .Net platform’s output to work successfully on a number of different devices.  For example, the ASP.NET run times contain a set of user interface controls (Web Form controls) that automatically query the calling browser or device or device and then emit the proper markup language that will work for the calling device. This means that web application developers no longer are required to write complicated branching code that will send one set of files to Netscape browsers and another set of files to Microsoft Browsers. Just one markup page is needed to support multiple browsers.This device independence takes a big leap forward in the ability to create a single source code library that can run successfully on almost any device.
  3. Wide Language Support- Microsoft has designed the .NET run times to support multiple source code languages. This allows programmers to choose the language they are most comfortable with when creating their solutions. The .NET run times support three Microsoft languages that can be used to build ASP.NET applications: C#, Visual Basic .NET and Jscript .NET. C# is the language Microsoft stuff used to build much of the .NET class libraries themselves. It is an extension of the C++ language that has many similarities with java.There are three more languages that are work for .NET has already been released in some beta form. Those are COBOL .NET, PERL .NET and Eiffel for .NET. There are more than 15 .NET languages announced.
  4. Internet-Based Component Services- Microsoft has built into .NET the ability to treat almost any internet location as a component. The key technology used to accomplish this is XML based SOAP (Simple Object Access Protocol) Services. SOAP offers a standardized message format for passing information between Web servers over the Internet. Microsoft has used this message system to help create a standardized way to send component and type library information between two machines. This is, in effect, a kind of remote procedure calling (RPC) system that works over standard HTTP connections. By utilizing the open standards of SOAP, Microsoft has built into the .NET run time a very simple way to expose components for use across the Internet.

Categories: .Net, Framework Tags: ,