New Features in .NET 4.5 and 5.0

This is my attempt at explaining some new features in .NET 4.5 and 5.0.
 
I am explaining the following features:
  • Parallel foreach
  • BigInteger
  • Expando Objects
  • Named and Optional Parameters
  • Tuple
1. Parallel.ForEach
 
Parallel.ForEach is a feature introduced by the Task Parallel Library (TPL). This feature helps you run your loop in parallel. You need to have a multi-processor to use of this feature.
 
Simple foreach loop
 
foreach (string i in listStrings)
{
 ..........
}
 
Parallel foreach
 
Parallel.Foreach(listStrings, text=>
{
……………………..
});
 
2. BigInteger
 
BigInteger is added as a new feature in the System.Numerics DLL. It is an immutable type that represents a very large integer whose value has no upper or lower bounds.
  1. BigInteger obj = new BigInteger("123456789123456789123456789"); 
3. ExpandoObject
 
The ExpandoObject is part of the Dynamic Language Runtime (DLR). One can add and remove members from this object at run time.
 
Create a dynamic instance.
  1. dynamic Person = new ExpandoObject();  
  2. Person.ID = 1001;  
  3. Person.Name = "Princy";  
  4. Person.LastName = “Gupta”;  
4. Named and Optional Parameters
 
Optional Parameters
 
A developer can now have some optional parameters by providing default values for them. PFB how to create optional parameters
  1. void PrintName(string a, string b, string c = “princy”)  
  2. {  
  3.      Console.Writeline(a, b, c)  
We can now call the function PrinctName() by passing either two or three parameters as in the following:
  1. PrintName(“Princy”,”Gupta”,”Jain”);  
  2. PrinctName(“Princy”,”Gupta”); 
Output
 
PrincyGuptaJain
PrincyGuptaprincy
 
Note: An optional parameter can only be at the end of the parameter list.
 
Named Parameters
 
With this new feature the developer can pass values to parameters by referring to parameters by names.
  1. void PrintName(string A, string B)  
  2. {  
Function call
  1. PrintName (B: “Gupta”, A: “Princy”); 
With this feature we don't need to pass parameters in the same order to a function.
 
5. Tuple
 
A Tuple provides us the ability to store various types in a single object.
 
The following shows how to create a tuple.
  1. Tuple<intstringbool> tuple = new Tuple<intstringbool>(1,"princy"true);  
  2. Var tupleobj = Tuple.Create(1, "Princy"true);  
In order to access the data inside the tuple, use the following:
  1. string name = tupleobj.Item2;  
  2. int age = tupleobj.Item1;  
  3. bool obj = tupleobj.Item3; 


Similar Articles