Introduction
I think we are all happy to know that Microsoft has introduced a new Visual Studio known as Visual Studio 2015 in which we will use C# 6.0. There are many new features included in C# 6.0. We know that C# is a very powerful Object Oriented Programming language so in the C# 6.0 there are also some new features that make it more powerful and reliable for users. it hs also dropped some features because these features are not so useful for the users so instead of those features C# 6.0 has introduced some new features. The dropped features are like a primary constructor, inline declaration and so on. In C# 6.0 we have some extra features that are very very useful. Features like Auto-property Enhancements, Using Static, Exception Filters, Name of Expressions, Null-conditional Operators, Index Initializes, Await in catch/finally, params Ienumerable and many more.
So here I will discuss one of the features that are very useful and very interesting known as the params Ienumerable, but before learning and discussing the params ienumerable first it is necessary to understand the Params and the ienumerable so let's discuss the params.
Params
Params is a keyword in the C# that plays a very vital role in programming. Using the params keyword we can pass the value of arguments to the function. Here we can pass a number of comma-separated arguments. When we compile our code containing the params keyword all the arguments passed in the method are automatically converted into a temporary array. Then this array is used for the method for receiving the parameters.
Syntax of Params
- static int sum(params int[] values)
- {
- }
Now I will make a simple example that makes a method and then explain that example with the params keyword.
- using System;
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int result = addition(12, 22, 33);
- Console.WriteLine("the sum of the num1 num2 and num3 is--"+result);
- Console.ReadLine();
- }
- public static int addition(int num1,int num2,int num3)
- {
- return num1 + num2 + num3;
-
- }
- }
- }
Output
Now, I make this example using the params keyword in C#.
- using System;
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int result = addition(1,2,3,4,5,6,7,8,9);
- Console.WriteLine("the sum of 0 to 1 is--" + result);
- Console.ReadLine();
- }
- public static int addition(params int[] listnumbers)
- {
- int sum = 0;
- foreach(int i in listnumbers)
- {
- sum = sum + i;
- }
- return sum;
- }
- }
- }
Output
There are the following drawbacks of the params keyword:
- Here we can use only that value for the function or method that are comma separated.
- We cannot use the additional keyword in a method if we use the params keyword in it.
- We can use only a single params keyword in the method declaration.
Now i will discuss ienumerable in C# with you.
The ienumerable interfaceienumerable<T> works like an interface. Here we can enumerate any type of instances that we want to enumerate. It is a generic interface generated using the the query expression. The query expression is the ienumerable<T>, here T will be an int, float or maybe anything. The ienumerable interface is found in the System.Collection namespace and has a single method in it.
I think you will understand it with a simple example so i will provide an example of ienumerable. Let's see it.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> names = new List<string>();
- names.Add("Rizwan");
- names.Add("shridhar");
- names.Add("Abhishek");
- names.Add("Nimit");
- names.Add("Gaurav");
- names.Add("Atul");
- IEnumerable<string> ienum = (IEnumerable<string>)names;
- foreach (string i in ienum)
- {
- Console.WriteLine(i);
- }
- Console.Read();
- }
- }
- }
Output
That is an overview of params and ienumerable. I think now you can easily work with params and ienumerable. So we will proceed further to discuss the latest features of C# 6.0, that is the
params Ienumerable
.
As I said above, you cannot use another keyword in a single method when you use the params keyword in a method. So before the new C# 6.0 you could not do this but now C# 6.0 provides the ability to use ienumerable with the params keyword.
Now it is not necessary to declare the params method as an array so there are some changes in it. Params ienumerable provides the ability to use ienumerable<T> instead of T[ ], when we use a params method.
Before we use it like:
- public void method(params int[] values)
Now C# 6.0 provides us the new technique to write this like:
- public void method(params IEnumerable<T>values)
Now I will provide an example of params with ienumerable.
Example
- using System;
- using System.Collections.Generic;
-
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> a = new List<string>();
- a.Add("Rizwan");
- a.Add("shridhar");
- a.Add("Abhishek");
- IEnumerable<string> ienum = (IEnumerable<string>)a;
- foreach (string i in ienum)
- {
- Console.WriteLine(i);
- }
- Console.Read();
-
- }
-
- public static class uses<T>
- {
-
- public static IEnumerable<string> Chain(params IEnumerable<string>[] arguments)
- {
- foreach (IEnumerable<string> a in arguments)
- {
- foreach (string value in a)
- {
- yield return value;
- Console.Read();
- }
- }
- }
- }
-
- }
- }
Output
Summary
So in this article I provided you information about the Params keyword with ienumerable. Before C# 6.0 we could not use ienumerable with the params keyword but now we can use it. We write ienumerable<T> instead of int[ ] with params. So we can say it reduces our code and the main advantage of using params with ienumerable is that we can provide the parameters depending on our needs, if we did not use params with ienumerable then every time we would need to provide the parameters depending on the method definition. So to overcome that problem we use it. I think this a good feature provided by C# 6.0.