#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. 

 

#20:Abstract Method

1.An Abstract method can be enclosed only in Abstract Class.

class test
 {
 //Error Abstract Method in Non-Abstract Class
 public abstract void hello();
}

2. Abstract Method cannot Declare Body.

abstract class test
{
//Error Abstract Method Cannot have declaration of Body
public abstract void hello()
{
}
// Correct Usage:only contract allowed.
public abstract void hello();
}

3.Abstract Method Cannot be Virtual

 //Abstract method cannot be virtual
  public abstract virtual void h();

4.You must override the Abstract Method in Class you derive.

5.Abstract Class can contain Non-Abstract Methods.

#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.
}

#17 Yield KeyWord

The Yield Keyword Indicates that it is an Iterator. They are used with classes which implements  IEnumerable.

Yield Keyword indicates that you need not write an Explicit Class defining the GetEnumerator Method .In turn the compiler writes it for you.

public IEnumerator GetEnumerator()
 {
 for (int i = 0; i <objarray.Length; i++)
 {
 yield return objarray[(index+i) % objarray.Length];
 }
 }

#16 Enumerating Over Custom Class

To Enumerate over Custom Class,The Class must implement IEnumerable and must have method defined for GetEnumerator.

public class EmployeeArray:IEnumerable
 {
 public int index;
 public Employee[] objarray;
public EmployeeArray(Employee[] Array ,int startindex )
 {
 objarray = Array;
 index = startindex;
 }
public IEnumerator GetEnumerator()
 {
 for (int i = 0; i <;objarray.Length; i++)
 {
 yield return objarray[(index+i) % objarray.Length];
 }
 }

#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");
}