#31:Invoking a Task.

You can create a Task,in two ways.

1.New Instance.

2.Factory.

static void Main(string[] args)
 {
 //Task with New Option
 Task T=new Task(LoopSample);
 T.Start();
 //StartNew with Factory
 Task t1 = Task.Factory.StartNew(LoopSample);
 Console.WriteLine("Completed Task");
 }
private static void LoopSample()
 {
 for (int i = 0; i<10000; i++)
 {
 Console.WriteLine(i);
 }
 }

#30 What is Task?

Task : Task is CLR ThreadPool Component,which manages to execute Operations in parallel.Unlike Thread,they do not create individual thread in windows,instead they utilize the CLR Threads from ThreadPool.

Usage:

They are mainly used when you want to perform Asynchronous Operation.and you have a time consuming block of code which blocks your UI Thread.   

#29: Linq Breaking Deferred Execution.

If you need your Linq Query to be executed Immediately after construction,we can use Conversation Operators like ToList(),ToArray().They force the query to execute immediately

var item = new List<int>();
item.Add(2);
IEnumerable<int> result = item.Select(x =>; x*5).ToList();
//Query is Executed Immedialtely and result will be 10.
//No deferred Execution,because we used ToList(),Forcing query to execute immediately
item.Add(3);
foreach (var VARIABLE in result)
{
 Console.WriteLine(VARIABLE);
}

#28:Deferred Execution-Linq

Most of the Query Operators Execute only when they are enumerated(IEnumerator Type-Move Next),but not when constructed.

var item = new List<int>();
 item.Add(2);
IEnumerable<int> result = item.Select(x => x*5);
//Query is Constructed
item.Add(3);
//But still the item will be added and output would be 10,15 instead of 10.
 //This is called deferred execution.Query executes only on enumerating(for-each) but not on construction.
foreach (var VARIABLE in result)
{
 Console.WriteLine(VARIABLE);
}

Note: Not All Query Operators involves Deferred Execution.For instance First Operator should Execute the Query when constructed and return the First Element.

 

#27:Linq Query Operators

The Query Operators takes the Following delegate Generic Types

1.Tsource-Input sequence.

2.TResult-Output Sequence.

3.TKey-Key used for order,sorting,grouping.

string[] names = { "Rangesh", "Tom", "Jon"};
var outerQuery = names.OrderBy(n2 => n2.Length).Select(n2 => n2.Length).First();

In the above sample,the OrderBy has the Generic delegate type where Source= names and Key=Length

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

#26:Summary of CLR,CTS,CLS,IL

CLR Summary

CLR: A Runtime Environment where all your code get executed as machine instructions.All management of resources,memory,exceptions are taken care by CLR.

CTS: All Types which CLR Supports is exposed by CTS.

CLS:A minimal Support of CTS,which every compiler has to implement it,inorder to Utilize CLR.Intention is to have common feature accross languages,so that they can communicate with each other.  

 

 

 

#25:What is CLS ?

Common Language Specification: A Specification,which every language must definitely support and should be kept same across different languages.The intention is that you must be able to communicate between different languages.

Example: For Instance  class in VB,should be able to inherit from Class in C#.Here Inheritance is Common between two languages.Thus CLS is a way of defining minimal support across various languages.

#24 What is CTS ?

Common Type System:A set of standard that defines how data has to be mapped into memory.For instance data can be value type or memory type,data type can be int,float,long,decimal. Languages that Utilizes CLR,should expose the data types as defined by CTS.

Example:Let’s assume you want writing language called xyz.and you utilize CLR feature,and your data type has to map to either Value Type or Memory Type.You cannot define a new type called valuememory type and this will not be supported by CLR as it’s not defined in CTS.

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