Monday, May 21, 2007

CLR, CTS, CLS, JIT, MSIL, Managed Code

What is CLR, CTS, CLS?
The .NET Framework provides a runtime environment called the Common Language Runtime or CLR (similar to the Java Virtual Machine or JVM in Java), which handles the execution of code and provides useful services for the implementation of the program. CLR takes care of code management at program execution and provides various beneficial services such as memory management, thread management, security management, code verification, compilation, and other system services. The managed code that targets CLR benefits from useful features such as cross-language integration, cross-language exception handling, versioning, enhanced security, deployment support, and debugging. Common Type System (CTS) describes how types are declared, used and managed in the runtime and facilitates cross-language integration, type safety, and high performance code execution. The CLS is simply a specification that defines the rules to support language integration in such a way that programs written in any language, yet can interoperate with one another, taking full advantage of inheritance, polymorphism, exceptions, and other features. These rules and the specification are documented in the ECMA proposed standard document, "Partition I Architecture", http://msdn.microsoft.com/net/ecma/

Is .NET a runtime service or a development platform?Ans: It's both and actually a lot more. Microsoft .NET includes a new way of delivering software and services to businesses and consumers. A part of Microsoft.NET is the .NET Frameworks. The .NET frameworks SDK consists of two parts: the .NET common language runtime and the .NET class library. In addition, the SDK also includes command-line compilers for C#, C++, JScript, and VB. You use these compilers to build applications and components. These components require the runtime to execute so this is a development platform. What is MSIL, IL?When compiling to managed code, the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code. 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. Microsoft intermediate language (MSIL) is a language used as the output of a number of compilers and as the input to a just-in-time (JIT) compiler. The common language runtime includes a JIT compiler for converting MSIL to native code.

Can I write IL programs directly?
Yes. Peter Drayton posted this simple example to the DOTNET mailing list:
.assembly MyAssembly {}
.class MyApp {
.method static void Main() {
.entrypoint
ldstr "Hello, IL!" c
all void System.Console::WriteLine(class System.Object)
ret
}
}
Just put this into a file called hello.il, and then run ilasm hello.il. An exe assembly will be generated.

Can I do things in IL that I can't do in C#?
Yes. A couple of simple examples are that you can throw exceptions that are not derived from System.Exception, and you can have non-zero-based arrays.

What is JIT (just in time)? how it works?
Before Microsoft intermediate language (MSIL) can be executed, it must be converted by a .NET Framework just-in-time (JIT) compiler to native code, which is CPU-specific code that runs on the same computer architecture as the JIT compiler.
Rather than using time and memory to convert all the MSIL in a portable executable (PE) file to native code, it converts the MSIL as it is needed during execution and stores the resulting native code so that it is accessible for subsequent calls.
The runtime supplies another mode of compilation called install-time code generation. The install-time code generation mode converts MSIL to native code just as the regular JIT compiler does, but it converts larger units of code at a time, storing the resulting native code for use when the assembly is subsequently loaded and executed.
As part of compiling MSIL to native code, code must pass a verification process unless an administrator has established a security policy that allows code to bypass verification. Verification examines MSIL and metadata to find out whether the code can be determined to be type safe, which means that it is known to access only the memory locations it is authorized to access.

Define CLR (Common Language Runtime)?
Central to the .NET Framework is its runtime execution environment, known as the Common Language Runtime (CLR) or the .NET runtime.
Example: A Jagged Array can be used is to create a table in which the lengths of the rows are not same. This Array is declared using square brackets ( [ ] ) to indicate each dimension.The following code demonstrates the creation of a two-dimensional jagged array.

What is managed code (IL)?
Code running under the control of the CLR is often termed as managed code.The C# code will be compiled into IL before it is executed (i.e. the C# compiler only compiles to managed code)

Explain the compilation process by the CLR?
Any source code that is developed needs to be compiled. Compilation occurs in two steps:
1. Compilation of source code to IL
2. Compilation of the IL to platform-specific code by the CLR

What are the Advantages of Managed Code?
1. Platform Independence
2. Performance Improvement: the IL is always Just-In-Time compiled (known as JIT compilation). Instead of compiling the entire application in one go (which could lead to a slow start-up time), the JIT compiler simply compiles each portion of code as it is called (just-in time). When code has been compiled once, the resultant native executable is stored until the application exists, so that it does not need to be recompiled the next time that portion of code is run.
3. Language Interoperability: The use of IL not only enables platform independence; it also facilitates language interoperability. You can compile to IL from one language, and this compiled code should then be interoperable with code that has been compiled to IL from another language.

What are the important features of the IL?
1. Object orientation and use of interfaces
2. Strong distinction between value and reference types
3. Strong data typing
4. Error handling through the use of exceptions
5. Use of attributes.

What is MSIL, and why should my developers need an appreciation of it at all?Microsoft Intermeidate lanaguage. which is the out put for all the .net supported languages after comiplation will produce. Appreciation for cross language support.

What is Common Language Specification (CLS)?
The CLS works with the CTS to ensure language interoperability. The CLS is a set of minimum standards that all compilers targeting .NET must support.

No comments: