Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to use and Invoke Func generic type in C#

2 Answers

0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        Func<int, string> func1 = (n) => string.Format("return string: {0}", n);
 
        Func<bool, int, string> func2 = (b, n) =>
                                       string.Format("return string: {0} and {1}", b, n);
 
        Func<double> func3 = () => Math.PI;
 
        Func<string, string> func4 = (n) => string.Format("return string: {0}", n);
 
        Console.WriteLine(func1.Invoke(13));
        Console.WriteLine(func2.Invoke(true, 10));
        Console.WriteLine(func3.Invoke());
        Console.WriteLine(func4.Invoke("c#"));
    }
}



/*
run:

return string: 13
return string: True and 10
3.14159265358979
return string: c#

*/

 



answered Jan 9, 2017 by avibootz
edited Mar 11, 2024 by avibootz
0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        Func<int, int> func1 = n => n + 1;
 
        Func<int, int> func2 = n => { return n + 1; };
 
        Func<int, int> func3 = n => ++n;
 
        Func<int, int, int> func4 = (x, y) => x * y;
 
        Action func5 = () => Console.WriteLine();
 
        Console.WriteLine(func1.Invoke(12));
        Console.WriteLine(func2.Invoke(5));
        Console.WriteLine(func3.Invoke(3));
        Console.WriteLine(func4.Invoke(6, 7));
        
        func5.Invoke();
    }
}



/*
run:

13
6
4
42

*/

 



answered Jan 9, 2017 by avibootz
edited Mar 11, 2024 by avibootz

Related questions

1 answer 146 views
1 answer 94 views
94 views asked Jul 1, 2023 by avibootz
1 answer 91 views
1 answer 105 views
3 answers 126 views
126 views asked Jul 1, 2023 by avibootz
...