Thursday, September 12, 2013

What is C# “Func” does?



Do you think are you really using .NET 3.5 feature?


It’s time to kill the old methodologies and writing plenty of lines of code. See the one of nice feature of 
.NET 3.5 

Take a simple function that eats input parameter as integer and returns output parameter as string

Old Methodology:


function string GetString(int intparam)
{
return “inputparameter”+ intparam;
}
String outputvalue= GetString(24);


New Methodology :


Func<int,string> GetString = i=>“inputparameter”+ i;
String outputvalue= GetString(24); 

         Here first parameter ( int ) act as input parameter type and second parameter ( string ) act as output parameter type


  1.  Func<int,bool,string> ==> here int and bool acts as input parameter type and string act as output parameter type
  2. Func<string> ==> here string acts as output parameter.There is no input parameter here.
  •      Func<int,string>=(i)=>string.Format("value{0}",i);
  •      Func<int,bool,string>=(i,b)=>string.Format("value{0}and {1}",i,b) ;
  •      Func<string>=()=>"test message"; Here there is no input parameter .It returns only string value

    Happy Coding :)