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 this feature.
Simple foreach loop
foreach (string i in listStrings)
{
// Your code here
}
Parallel foreach
Parallel.ForEach(listStrings, text =>
{
// Your code goes here
});
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.
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
dynamic Person = new ExpandoObject();
Person.ID = 1001;
Person.Name = "Princy";
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
void PrintName(string a, string b, string c = "princy")
{
Console.WriteLine(a, b, c);
}
We can now call the function PrinctName() by passing either two or three parameters as in the following.
PrintName("Princy", "Gupta", "Jain");
PrintName("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.
void PrintName(string A, string B)
{
}
Function call
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.
Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "princy", true);
var tupleobj = Tuple.Create(1, "Princy", true);
In order to access the data inside the tuple, use the following.
string name = tupleobj.Item2;
int age = tupleobj.Item1;
bool obj = tupleobj.Item3;

Avanish YadavPosted Dec 31, 2015, 12:50 AM
Nice
Avanish YadavPosted Dec 31, 2015, 12:49 AM
Nice
mohan dassPosted Aug 18, 2015, 6:47 AM
Thank u.
Praveen Raveendran PillaiPosted May 22, 2014, 5:50 AM
Nice article... All these features are available from .net 4 framework.
Anbu AzhaganPosted May 20, 2014, 10:45 AM
Nice
Pradeep HegdePosted Apr 15, 2014, 1:54 AM
nice info..
Shweta LodhaPosted Mar 23, 2014, 6:56 PM
I think you should title this post as 'New features in .Net 4.0' rather than 4.5 and 5.
Akhil MittalPosted Mar 21, 2014, 5:10 AM
What in this article is the feature of .Net Framework 5.0???
Graeme PariotPosted Mar 20, 2014, 8:54 PM
Princy, few of the features provided by you are not part of 4.5 or 5.0 Features like Named and Optional parameters, Tuple, etc these features were available in .Net 4 too. Please modify your post
Shweta LodhaPosted Mar 20, 2014, 12:06 PM
Welcome..You are also here ;)
Sam HobbsPosted Mar 20, 2014, 11:55 AM
Congratulations on your first article.
Mahesh ChandPosted Mar 20, 2014, 7:50 AM
Welcome to C# Corner, Princy.
Sarfaraz AlamPosted Mar 20, 2014, 6:11 AM
Very nice
Jeetendra GundPosted Mar 20, 2014, 4:15 AM
Nice Article....Welcome Princy