|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
Func<TResult>-Delegat
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Typparameter
- out TResult
Der Typ des Rückgabewerts der Methode, die dieser Delegat kapselt. Dieser Typparameter ist Covariant. Das heißt, Sie können entweder den angegebenen Typ oder einen weiter abgeleiteten Typ verwenden. Weitere Informationen zu Ko- und Kontravarianz finden Sie unter Kovarianz und Kontravarianz in Generika.
Rückgabewert
Typ: TResult Hinweis |
|---|
using System; using System.IO; delegate bool WriteMethod(); public class TestDelegate { public static void Main() { OutputTarget output = new OutputTarget(); WriteMethod methodCall = output.SendToFile; if (methodCall()) Console.WriteLine("Success!"); else Console.WriteLine("File write operation failed."); } } public class OutputTarget { public bool SendToFile() { try { string fn = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(fn); sw.WriteLine("Hello, World!"); sw.Close(); return true; } catch { return false; } } }
using System; using System.IO; public class TestDelegate { public static void Main() { OutputTarget output = new OutputTarget(); Func<bool> methodCall = output.SendToFile; if (methodCall()) Console.WriteLine("Success!"); else Console.WriteLine("File write operation failed."); } } public class OutputTarget { public bool SendToFile() { try { string fn = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(fn); sw.WriteLine("Hello, World!"); sw.Close(); return true; } catch { return false; } } }
using System; using System.IO; public class Anonymous { public static void Main() { OutputTarget output = new OutputTarget(); Func<bool> methodCall = delegate() { return output.SendToFile(); }; if (methodCall()) Console.WriteLine("Success!"); else Console.WriteLine("File write operation failed."); } } public class OutputTarget { public bool SendToFile() { try { string fn = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(fn); sw.WriteLine("Hello, World!"); sw.Close(); return true; } catch { return false; } } }
using System; using System.IO; public class Anonymous { public static void Main() { OutputTarget output = new OutputTarget(); Func<bool> methodCall = () => output.SendToFile(); if (methodCall()) Console.WriteLine("Success!"); else Console.WriteLine("File write operation failed."); } } public class OutputTarget { public bool SendToFile() { try { string fn = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(fn); sw.WriteLine("Hello, World!"); sw.Close(); return true; } catch { return false; } } }
using System; static class Func1 { public static void Main() { // Note that each lambda expression has no parameters. LazyValue<int> lazyOne = new LazyValue<int>(() => ExpensiveOne()); LazyValue<long> lazyTwo = new LazyValue<long>(() => ExpensiveTwo("apple")); Console.WriteLine("LazyValue objects have been created."); // Get the values of the LazyValue objects. Console.WriteLine(lazyOne.Value); Console.WriteLine(lazyTwo.Value); } static int ExpensiveOne() { Console.WriteLine("\nExpensiveOne() is executing."); return 1; } static long ExpensiveTwo(string input) { Console.WriteLine("\nExpensiveTwo() is executing."); return (long)input.Length; } } class LazyValue<T> where T : struct { private Nullable<T> val; private Func<T> getValue; // Constructor. public LazyValue(Func<T> func) { val = null; getValue = func; } public T Value { get { if (val == null) // Execute the delegate. val = getValue(); return (T)val; } } } /* The example produces the following output: LazyValue objects have been created. ExpensiveOne() is executing. 1 ExpensiveTwo() is executing. 5 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
Hinweis