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 C# 6.0, there are also some new features that make it more powerful and reliable for users. it has 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 in C#
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)
{
// Method body goes here
}
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 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 numbers from 1 to 9 is: " + result);
Console.ReadLine();
}
public static int Addition(params int[] listNumbers)
{
int sum = 0;
foreach (int i in listNumbers)
{
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 is 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 the enumerable 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 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;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
List<string> names = new List<string>(); // This is the use of generics
names.Add("Rizwan");
names.Add("Shridhar");
names.Add("Abhishek");
names.Add("Nimit");
names.Add("Gaurav");
names.Add("Atul");
IEnumerable<string> ienum = (IEnumerable<string>)names; // Use of IEnumerable
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)
{
// Your method implementation here
}
Now, C# 6.0 provides us with a new technique to write this like.
public void method(params IEnumerable<T> values)
{
// Your method implementation here
}
Now, I will provide an example of params with innumerable.
Example
using System;
using System.Collections.Generic;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
List<string> a = new List<string>(); // This is the use of generics
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; // Yield is a keyword
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 innumerable, 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 is a good feature provided by C# 6.0.