- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Func generic type in C#
The Func generic type store anonymous methods and is a parameterized type.
In the below example, we have 4 func type instance −
The first type receives int and returns string
Func<int, string> one = (p) => string.Format("{0}", p);
The second type receives bool & long and returns string
Func<bool, long, string> two = (q, p) =>string.Format("{0} and {1}", q, p);
The third type receives bool & int and returns string
Func<bool, int, string> three = (q, p) => string.Format("{0} and {1}", q, p);
The fourth type receives decimal and returns string
Func<decimal, string> four = (p) =>string.Format("{0}", p);
Let us see how to display them −
Example
using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { // four func type instance // first type receives int and returns string Func<int, string> one = (p) => string.Format("{0}", p); // second type receives bool & long and returns string Func<bool, long, string> two = (q, p) => string.Format("{0} and {1}", q, p); // three type receives bool & int and returns string Func<bool, int, string> three = (q, p) => string.Format("{0} and {1}", q, p); // fourth type receives decimal and returns string Func<decimal, string> four = (p) => string.Format("{0}", p); Console.WriteLine(one.Invoke(25)); Console.WriteLine(two.Invoke(false, 76756566)); Console.WriteLine(three.Invoke(true, 50)); Console.WriteLine(four.Invoke(1.2m)); } } }
Output
25 False and 76756566 True and 50 1.2
Advertisements