/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/language-reference/operators/snippets/shared/DelegateOperator.cs
60 строк
2 KB
Bill Wagner
first draft of nameof updates (#29549)
15 июн 2022, 00:43
Не верифицирован
15 июн 2022, 00:43
86ba43a
Код
Авторство
О чём код?
namespace operators; public static class DelegateOperator { public static void Examples() { AnonymousMethod(); Lambda(); WithoutParameterList(); Discards(); Static(); } private static void AnonymousMethod() { // <SnippetAnonymousMethod> Func<int, int, int> sum = delegate (int a, int b) { return a + b; }; Console.WriteLine(sum(3, 4)); // output: 7 // </SnippetAnonymousMethod> } private static void Lambda() { // <SnippetLambda> Func<int, int, int> sum = (a, b) => a + b; Console.WriteLine(sum(3, 4)); // output: 7 // </SnippetLambda> } private static void WithoutParameterList() { // <SnippetWithoutParameterList> Action greet = delegate { Console.WriteLine("Hello!"); }; greet(); Action<int, double> introduce = delegate { Console.WriteLine("This is world!"); }; introduce(42, 2.7); // Output: // Hello! // This is world! // </SnippetWithoutParameterList> } private static void Discards() { // <SnippetDiscards> Func<int, int, int> constant = delegate (int _, int _) { return 42; }; Console.WriteLine(constant(3, 4)); // output: 42 // </SnippetDiscards> } private static void Static() { // <SnippetStatic> Func<int, int, int> sum = static delegate (int a, int b) { return a + b; }; Console.WriteLine(sum(10, 4)); // output: 14 // </SnippetStatic> } }