#23: What is JIT?

Just In Time Compiler: A Compiler responsible for Converting IL Code to Native CPU Instructions.

Note: JIT Compiles the IL Code only once,when you execute the program For the first time.The next time when the program is executed ,the JIT does not compile,but instead the call goes directly to memory(since there was no change in IL Code,and the instructions has already been generated during the First time compilation,there is no need for JIT to compile the same old IL Code). 

NGEN.EXE: Tool Available for converting IL Code to Native CPU Instructions.It increases Performance of Applications.It does not utilize JIT. 

#22:Managed Modules-CLR

Every Portable Executable File( produced After Compiling,is Managed Module(Executed by CLR).

Managed Module=IL Code+Metatdata.

Managed Module has the Following Information For CLR/OS.

1.PE32 or PE32+header: Indicates whether the Executable is 32 bit or 63bit. PE32 For 32bits and PE32+ For 64bit.

2.CLR Header:Contains CLR Version and other CLR related Information like MethodDefinition etc..

3.MetaData :MetaData acts like table which gives information about underlying data types ,method name,method parameters used in the program.

4.ILCode: CPU-Independent Machine Language Designed by Microsoft. They are common across CPU.

Note:Collection of Managed Module is called Assemblies.

A Insight Into Executable:

Executable Explorer

#21 What is CLR?

CLR:Common Language RunTime.

1.A Runtime Environment Provided by Microsoft,that is usable by different Programming Language(C#,VB,F# etc..).

2.They take Care of Memory Management,Exception Handling,Assembly Loading,Threads and so on..that  your Application Needs.

3.Any Code that you write with specific Compiler for Specific Language that targets CLR Ends up as Managed Module.(PE-32 -64 bit/ PE-32+ for 32 bit Exe).Managed Module Executables are intrun executed by CLR. 

 

#18:For vs ForEach Difference when looping through collection

You cannot  Add /Remove(Assign Values/Delete ) while you iterate Collection  with ForEach.

int[] a=new int[4]{1,2,3,4};
 foreach (int i in a)
 {
 i = 7;// Error-Read Only Not Possible
 }

Reason: IEnumerator has a method defined only with get,not with set.However you can add set method,but not preferred because it has its own side effects.


public object Current

{
get { throw new NotImplementedException(); } //Only get method,For Each can only read.
}

#15:ForEach Over Collection of Object is Valid only If it Implements IEnumerable

ForEach is Applicable only if Class  implements IEnumerable(IEnumerator Method).

 public class C
 {
 public int i { get; set; }

 public void sampleMethod()
 {
 C objc = new C();
 //error Class C is Not Enumerable
 foreach (var VARIABLE in objc)
 {

 }
 }
 }

Correct Usage implement IEnumerable in Class(IEumerator GetEnumerator):

public class C:IEnumerable
 {
 public int i { get; set; }

 public void sampleMethod()
 {
 C objc = new C();

 foreach (var VARIABLE in objc)
 {

 }
 }
 public IEnumerator GetEnumerator()
 {
 throw new NotImplementedException();
 }
 }

#14 Don’t Use StringBuilder EveryWhere

Even though StringBuilder has its Advantage.They have to used Properly.

var FName = Console.ReadLine();
 var LName = Console.ReadLine();
 /*
 * Less Time Consuming and Fast.
 */
 String Name = "FirstName" + FName + "LastName" + LName;
 /*
 * Bad Code,since operation is time consuming though its efficient.
 */</p><p>StringBuilder builder=new StringBuilder();
 builder.Append("FirstName");
 builder.Append(FName);
 builder.Append("LastName");
 builder.Append(LName);</p><p>

1. StringBuilder are ideally suited when you iterate over the string operation for vast number of times.

2.String Concat are better way if you know the literals that have to be concatenated and if they are compile time constant

#13 StringBuilder Usage

Strings are Immutable. StringBuilder is special case where the buffer size increases(doubles up every time when the length is in shortage),instead of creating new strings every time when an concatenation operation takes place on string.

</pre>
String a = "";
for (int i = 1; i < 100; i++)
{
a = a + "" + "Test"; //Improper Usage it would create 100 objects of strings
}
StringBuilder builder=new StringBuilder();
for (int i = 0; i < 100; i++)
{
//Proper Usage only buffer size increase
builder = builder.Append(a).Append(" ").Append("Test");
}