Params Keyword With ienumerable in C# 6.0

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

  1. static int sum(params int[] values)  
  2. {  
  3. }  
Now I will make a simple example that makes a method and then explain that example with the params keyword.
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int result = addition(12, 22, 33);  
  10.             Console.WriteLine("the sum of the num1 num2 and num3 is--"+result);  
  11.             Console.ReadLine();  
  12.         }  
  13.         public static int addition(int num1,int num2,int num3)  
  14.         {  
  15.             return num1 + num2 + num3;  
  16.              
  17.         }  
  18.     }  
  19. }  
Output



Now, I make this example using the params keyword in C#.
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int result = addition(1,2,3,4,5,6,7,8,9);  
  10.             Console.WriteLine("the sum of 0 to 1 is--" + result);  
  11.             Console.ReadLine();  
  12.         }  
  13.         public static int addition(params int[] listnumbers)//here use of param keyword  
  14.         {  
  15.             int sum = 0;  
  16.             foreach(int i in listnumbers)// this is  foreach loop  
  17.             {  
  18.                 sum = sum + i;  
  19.             }  
  20.             return sum;  
  21.         }  
  22.     }  
  23. }  
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 interface

ienumerable<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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication3  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             List<string> names = new List<string>();// this is use of generic  
  14.             names.Add("Rizwan");  
  15.             names.Add("shridhar");  
  16.             names.Add("Abhishek");  
  17.             names.Add("Nimit");  
  18.             names.Add("Gaurav");  
  19.             names.Add("Atul");  
  20.             IEnumerable<string> ienum = (IEnumerable<string>)names;//use of ienumerable  
  21.             foreach (string i in ienum)  
  22.             {  
  23.                 Console.WriteLine(i);  
  24.             }  
  25.             Console.Read();  
  26.         }  
  27.     }  
  28. }  
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:
  1. public void method(params int[] values)   
Now C# 6.0 provides us the new technique to write this like:
  1. public void method(params IEnumerable<T>values)  
Now I will provide an example of params with ienumerable.

Example 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace ConsoleApplication4  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             List<string> a = new List<string>();// this is use of generic  
  11.             a.Add("Rizwan");  
  12.             a.Add("shridhar");  
  13.             a.Add("Abhishek");  
  14.             IEnumerable<string> ienum = (IEnumerable<string>)a;  
  15.             foreach (string i in ienum)  
  16.             {  
  17.                 Console.WriteLine(i);  
  18.             }  
  19.             Console.Read();  
  20.   
  21.         }  
  22.   
  23.         public static class uses<T>  
  24.         {  
  25.   
  26.             public static IEnumerable<string> Chain(params IEnumerable<string>[] arguments)  
  27.             {  
  28.                 foreach (IEnumerable<string> a in arguments)  
  29.                 {  
  30.                     foreach (string value in a)  
  31.                     {  
  32.                         yield return value;//yeild is a keyword  
  33.                         Console.Read();  
  34.                     }  
  35.                 }  
  36.             }  
  37.         }  
  38.   
  39.     }  
  40. }  

 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. 

Up Next
    Ebook Download
    View all
    Learn
    View all