Archive for the ‘C# 4.0’ Category

Join Two Array – Zipping

February 18, 2013 Leave a comment

Last week I had a requirement where I need to Join two array,there was no relation that I could use to  join LINQ.

Int32[] iArrayOne = {12,34,45,89};

String[] sArrayTwo= { “Value1”,”value2”,”value3”,”Value4”}

Later which I posted a question in SO,in fact it was not possible directly in LINQ.Eric has written a Post that solves my requirement. Yes the operation is termed as Zipping. Until .NET 4.0 we need to use the below snippet to achieve it and starting 4.0 we will have that function as part of the .NET.

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>
(this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TSecond, TResult> resultSelector)
if (first == null) throw new ArgumentNullException("first");
if (second == null) throw new ArgumentNullException("second");
if (resultSelector == null) throw new ArgumentNullException("resultSelector");
return ZipIterator(first, second, resultSelector);

private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>
(IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TSecond, TResult> resultSelector)
using (IEnumerator<TFirst> e1 = first.GetEnumerator())
using (IEnumerator<TSecond> e2 = second.GetEnumerator())
while (e1.MoveNext() && e2.MoveNext())
yield return resultSelector(e1.Current, e2.Current);
Zipping operation is defined as   var qry = from row in iArrayOne.Zip(sArrayTwo, (temp1, temp2) => 
new { ArrayObject1 = temp1, ArrayObject2 = temp2 })
Tags : ,
Categories: .NET 4.0, C# 4.0

Optional Parameter, Named arguments & Default values C# 4.0

May 6, 2009 1 comment

One of the cool feature (list here) we always wanted is ability to provide optional parameters, default values for the methods. Microsoft has addressed this in C# 4.0 and definitely it will make to RTM as well. As there are many articles out in the web regarding this I will not reiterate it but give you a base understanding on this and let you to read the article for core understanding.

Following things are valid in C# 4.0

public Int32 DoAdd(Int32 iNumberOne,Int32 iNumberTwo=0,Int32 iNumberThree=0)


            return iNumberOne + iNumberTwo+iNumberThree;


            //value is passed to the First parameter


            //value is passed to the first & second Parameter


            //Value is pased to the first,second & third parameter

            DoAdd(1, 2,3);

            //value is passed to the first and third parameter

            //this is where we can see one of the use of NamedParameters

            //calling this method this DoAdd(1,2) will men completly different

            //in this sence. Hence the Named Paramert helps here

            DoAdd(1, iNumberThree: 2);

            //When you use the Named Parameter order of the Parameter doent


            DoAdd(iNumberThree: 2, iNumberOne: 2);



Points to Remember:

  • Optional Parameter should be declared just simply placing the default value with a = operator
  • Optional Parameter must appear after all required parameters.
  • ref or out parameter cannot have a default value.
  • Named parameters gives us the more code readability
  • Helps in the optional parameter methods

Named Parameters:

  • Named parameters gives us the more code readability
  • Helps in the optional parameter methods
  • Order of parameter, helps us to change the order of the Parameters while calling method and still have the ablity to map which value goes to which argument 


        public Int32 DoMath(Int32 iNumberOne,Boolean SqureTheNumber)


            if (SqureTheNumber)

                return iNumberOne * iNumberOne;


                return iNumberOne + 1;


            //This doent give much information about the call

            DoMath(4, false);


            //This is more clear

            DoMath(4, SquareTheNumber:false);






Categories: .NET 4.0, C# 4.0