.Net Framework related questions and answers

1.What are the responsibilities of CLR?

Phrase 3(Compile MSIL to Native Code):

Before run the code we need to compile MSIL code into Native code which is done by CLR.  Native code 

is generated for the target machine architecture.

This can be performed in two ways

1) JIT (just-in-time) compiler

2) Ngen.exe (Native Image Generator)

1. JIT (just-in-time) compiler:

JIT compilation converts the MSIL into Native code on demand at application runtime because some 

code might not called at runtime.

The loader creates and attaches a stub to each method.

When method is called first time , The stub passes control to JIT which converts MSIL to native code and 

modified stub point directly to the generated Native code. Subsequent method calls Native code stub 

directly.

2. Ngen.exe (Native Image Generator):

Because JIT compilation performed at run time depend method call its hurts the performance.

Another Important thing is code generated by the JIT compiler is bound to the process that triggered the 

compilation. It cannot be shared across multiple processes.

Due to above CLR supports ahead-of-time compilation mode.

This ahead-of-time compilation mode uses the Ngen.exe.

Ngen.exe perform conversion before running the application. Its convert entire assembly generated 

code in the Native Image Cache as a file on disk its available to all process. So its definitely increases the 

performance.

 Phrase 4(Running the native code):

CLR provide the infrastructure to run the code and services are used while running the code.

Services which are used are garbage collection, security, interoperability with unmanaged code, cross-

language debugging support, and enhanced deployment and versioning support.

2.What is JIT (just-in-time) compiler?

Which is used in Compile MSIL to Native Code.

JIT compilation converts the MSIL into Native code on demand at application runtime because some 

code might not called at runtime.

The loader creates and attaches a stub to each method.

When method is called first time , The stub passes control to JIT which converts MSIL to native code and 

modified stub point directly to the generated Native code. Subsequent method calls Native code stub 

directly.

3.What is Ngen.exe (Native Image Generator) compilation?

Which is used in Compile MSIL to Native Code.

CLR supports ahead-of-time compilation mode.

This ahead-of-time compilation mode uses the Ngen.exe.

Ngen.exe perform conversion before running the application. Its convert entire assembly generated 

code in the Native Image Cache as a file on disk its available to all process. So its definitely increases the 

performance.

4.What is difference between JIT compilation and Ngen.exe compilation?

JIT compilation performed at run time depend method call its hurts the performance.

Another Important thing is code generated by the JIT compiler is bound to the process that triggered the 

compilation. It cannot be shared across multiple processes.

CLR supports ahead-of-time compilation mode.

This ahead-of-time compilation mode uses the Ngen.exe.

Its convert entire assembly generated code in the Native Image Cache as a file on disk its available to all 

process. So its definitely increases the performance.

5.What is MSIL in NET?

From the our C# source code taken by the CSC (c# compiler) which will generate MSIL.

MSIL(Microsoft intermediate language) :

Its contain CPU independent set of instructions.

Its contain instructions from initializing to exit.

Public static void Main()

        {

            int a =1, b=3;

            Console.WriteLine(a + b);

            Console.ReadLine();

        }

For above we can see the below MSIL code which is contain instructions from initializing to exit.

.method public hidebysig static void  Main() cil managed

{

  .entrypoint

  // Code size       21 (0x15)

  .maxstack  2

  .locals init ([0] int32 a,

           [1] int32 b)

  IL_0000:  nop

  IL_0001:  ldc.i4.1

  IL_0002:  stloc.0

  IL_0003:  ldc.i4.3

  IL_0004:  stloc.1

  IL_0005:  ldloc.0

  IL_0006:  ldloc.1

  IL_0007:  add

  IL_0008:  call       void [mscorlib]System.Console::WriteLine(int32)

  IL_000d:  nop

  IL_000e:  call       string [mscorlib]System.Console::ReadLine()

  IL_0013:  pop

  IL_0014:  ret

} // end of method MyClassForDelegates::Main

7.Threading in C#?

Threading enables to perform more than one operation at a time.

Using System. Threading   namespace is used for threads 

How can we use C# keywords as identifiers?

Using @ as a prefix to the keyword.

For example we want to if keyword as identifier you can use  like @if 

Int if (not valid)

Int @if (valid)

8.What is the use of unsafe keyword?

OR

Write a sample program which illustrate the unsafe keyword?

Its used represent the unsafe context.

public class Unsafeclass

    {

       unsafe static void mul( int* a)

        {

            *a = *a * *a;

        }

        unsafe static void Main()

        {

            int b = 2;

            mul(&b);

            Console.Write(b);

            Console.ReadLine();

        }

    }

o/p : 4

Whenever we are working with unsafe we need to mention unsafe key word everywhere otherwise it 

shows error. 

We need to compile solution also in unsafe mode.

9.What is Explicit keyword in C#?

OR

Write a sample program which illustrate the Explicit keyword?

Which is used to define user define cast operator.

If a conversion operation can cause exceptions or lose information, you should mark it explicit.

 class Meters

    {

        public Meters(float temp)

        {

            inmeters = temp / 100;

        }

        public static explicit operator Meters(float c)

        {

            Meters m = new Meters(c);

            return m;

        }

        public float InMeters

        {

            get { return inmeters; }

        }

        private float inmeters;

    }

    class MainClass

    {

        static void Main()

        {

            float a = 300;

            Meters m = (Meters)a;

            Console.Write(" In Meters the value is :");

            Console.WriteLine(m.InMeters);

            Console.ReadLine();

        }

    }  

Output:

In Meters the value is : 3

10.What is the use of fixed keyword?

OR

Write a program to illustrate fixed statement?

Fixed statement is used for prevent the garbage collector from collecting the movable objects.

Fixed state always used in unsafe context.

  public class fixedkeyword

    {

        static unsafe void Main()

        {

            string str = "fixedkeyword program";

            fixed (char* p = str) 

            {

                //you can write logic

            }

        }

    }

11.What is stackalloc keyword?

The stackalloc key word is used to allocate block of memory on the stack.

It is used in unsafe code context

int* lengths = stackalloc int[100];

12.What is null-coalescing operator (??) in C#?

OR

Write a program to illustrate fixed statement?

This operator take two arguments first its check left side operand value if its not null it will return the 

value else its return right side operand.

public static void Main()

        {

            int? a = null;

            int x = 7;

            int z = a ?? x;

            Console.WriteLine(z);

            Console.ReadKey();

        }

o/p : 7

If both are null its won’t show any value but here resultant operand should be nullable. 

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment