Dot Net For All

Important IEnumerable methods for Fast programming

In this article I will discuss some of the important functions which have been provided by the .NET framework for IEnumerable types. These functions comes very handy to work in fast paced development.Lets see what are these functions are. I will use C# as the language for the examples.

Get the numbers in a Range for IEnumerable

We can get the numbers in a range using the Range function of the Enumerable Type. Please check the figure below.

IEnumerable Range Method

In the above code I want to get all the numbers from 1 to 5. I have used Enumerable.Range(1, 5) to achieve the same

Operating on values from two arrays

Zip is used to operate on the values from two arrays. Have a look at the following code to understand it better

            string[] arr1 = new string[] { "vikram", "Raj", "Prasad" };
            int[] arr2 = new int[] { 33, 31, 32 };

            var modified = arr1.Zip(arr2, (string arg1, int arg2) => {
                return string.Format("Name: {0} and Age: {1}", arg1, arg2.ToString());
            });

            foreach (var item in modified)
            {
                Debug.WriteLine(item);
            }

In the above code I am concatenating the items from two arrays and returning the results. I am using a lambda method two work on the elements of the two arrays. I could have used the Func delegate in place of lambda. The result of the above Zip functions is as shown below.

Zip Method for IEnumerable

This function can come as rescue if we want to work on items of two arrays at same time to get some king of result.

Concatenating Two arrays

We can use the concat method  to join the elements of two IEnumerable types. Please check the code below.

            var arr1 = new int[] { 1, 2, 3, 4, 5 };
            var arr2 = new int[] { 5, 6, 7, 8, 9, 10 };

            var modified = arr1.Concat(arr2);

The output of the above code would be a IEnumerable type containing elements from both arrays. The output for the above code would be

{1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10}

As we can see from the above result, 5 is there two times in the array.

Getting the Union of two Collections

Using Concat operation we will get the same item multiple times in the resulting array. But if you want to get each item only once we can use the Join operation. Please check the code below.

            var arr1 = Enumerable.Range(1, 5);
            var arr2 = Enumerable.Range(5, 5);

            var modified = arr1.Union(arr2);

The result of the above code will be

{1, 2, 3, 4, 5, 6, 7, 8, 9 ,10}

The above result will have all the elements present exactly once. Though number 5 is present in each of the collections but still it is present only once.

Getting the Common Items

To get the common items from both of the arrays we should use Intersect method of the IEnumerable type. Please check the code below

            var arr1 = Enumerable.Range(1, 5);
            var arr2 = Enumerable.Range(5, 5);

            var modified = arr1.Intersect(arr2);

The result of the above code is number 5. This is the common item present in both the arrays.

Getting the Items only present in first Collection

To get the items present only in the first collection we have to use the Except method. The C# code example is below

            var arr1 = Enumerable.Range(1, 5);
            var arr2 = Enumerable.Range(5, 5);

            var modified = arr1.Except(arr2);

The result of the above code would be

{1, 2, 3, 4}

The above are the items which are only present in the first collection. The 5 is also not present in the result as it is present in the second collection along with the first.

Conclusion:

In this article I have discussed some of the important functions of C# IEnumerable class which can be very useful while programming. The functions were Range, Zip, Concat, Union, Intersect and Except.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview