Func<> delegate expression

This expression can be given as Func< inDataType T , outDataType Tresult >
Where inDataType = data type of the input &
outDataType = data type of the output

eg.: Func i.e. input data type = string & output data type = int

we can initiate Delegate Func in different ways ...some them are ........

1. We can directly give some function to Func.

class Program
{
static void Main(string[] args)
{
Func strString = Upperconversion;

string name = "this is func example";

Console.WriteLine(strString(name));

Console.ReadLine();
}

public static string Upperconversion(string s)
{
return s.ToUpper();
}
}


Here Function Upperconversion is directly given Func.

2.We can directly give delegate insted of any function to the Func.

static void Main(string[] args)
{
Func strString = delegate(string s)
{ return s.ToUpper();};

string name = "this is func example";

Console.WriteLine(strString(name));

Console.ReadLine();
}

3.We can use lamda expression for Func.

static void Main(string[] args)
{
Func strString = s => s.ToUpper();

string name = "this is func example";

Console.WriteLine(strString(name));

Console.ReadLine();
}


example that you can understand.

string[] words = { "Hindi", "Marathi", "English", "Gujarati" };

Func selector = str => str.Length;

IEnumerable aWords = words.Select(selector);

foreach (int word in aWords)
Console.WriteLine(word);

Console.ReadLine();

0 comments:

Post a Comment