From: Gustavo Martin Date: Wed, 1 Jan 2014 21:08:18 +0000 (+0100) Subject: C# In Depth: Chapter 9 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2168664fa2c23fce7f4040f360af57cacb3752d9;p=CSharpForFun%2F.git C# In Depth: Chapter 9 --- diff --git a/CSharpInDepth/Chapter9/Chapter9.v12.suo b/CSharpInDepth/Chapter9/Chapter9.v12.suo index 5b7fee9..73aa76d 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9.v12.suo and b/CSharpInDepth/Chapter9/Chapter9.v12.suo differ diff --git a/CSharpInDepth/Chapter9/Chapter9/Form1.cs b/CSharpInDepth/Chapter9/Chapter9/Form1.cs index 366618e..839212f 100644 --- a/CSharpInDepth/Chapter9/Chapter9/Form1.cs +++ b/CSharpInDepth/Chapter9/Chapter9/Form1.cs @@ -14,6 +14,8 @@ namespace Chapter9 { public partial class Form1 : Form { + delegate T MyFunc(); + public Form1() { @@ -143,6 +145,58 @@ namespace Chapter9 Console.WriteLine(compiled4("First", "Second")); Console.WriteLine(compiled4("First", "Fir")); + + /** + * + * Listing 9.11 Example of code requiring the new type inference rules. + */ + Console.WriteLine("Listing 9.11 Example of code requiring the new type inference rules."); + PrintConvertedValue("I am a string", x => x.Length); + + /** + * + * Listing 9.12 Attempting to infer the return type of an anonymous method. + */ + Console.WriteLine("Listing 9.12 Attempting to infer the return type of an anonymous method."); + WriteResult(delegate { return 5; }); + + /** + * + * Listing 9.13 Code returning an integer or an object depending on the time of day. + */ + Console.WriteLine("Listing 9.13 Code returning an integer or an object depending on the time of day."); + WriteResult(delegate + { + if (DateTime.Now.Hour < 12) + { + return 10; + } + else + { + return new object(); + } + }); + + /** + * + * Listing 9.14 Flexible type inference comibning information from multiple arguments. + */ + Console.WriteLine("Listing 9.14 Flexible type inference comibning information from multiple arguments."); + PrintType(1, new object()); + + /** + * + * Listing 9.15 Multistage type inference. + */ + Console.WriteLine("Listing 9.15 Multistage type inference."); + ConvertTwice("Another String", text => text.Length, length => Math.Sqrt(length)); + + /** + * + * Listing 9.16 Sample of overloading choice influenced by delegate return type. + */ + Console.WriteLine("Listing 9.16 Sample of overloading choice influenced by delegate return type."); + Execute(() => 1); } static void Log(string title, object sender, EventArgs e) @@ -157,6 +211,40 @@ namespace Chapter9 Console.WriteLine(" {0}={1}", name, value); } } + + static void PrintConvertedValue(TInput input, Converter converter) + { + Console.WriteLine(converter(input)); + } + + static void WriteResult(MyFunc function) + { + Console.WriteLine(function()); + } + + static void PrintType(T first, T second) + { + Console.WriteLine(typeof(T)); + } + + static void ConvertTwice(TInput input, + Converter firstConversion, + Converter secondConversion) + { + TMiddle middle = firstConversion(input); + TOutput output = secondConversion(middle); + Console.WriteLine(output); + } + + static void Execute(Func action) + { + Console.WriteLine("action returns an int: " + action()); + } + + static void Execute(Func action) + { + Console.WriteLine("action returns a double: " + action()); + } } class Film diff --git a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe index 05bb4c5..9c113c8 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe and b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe differ diff --git a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb index 9f0e2f3..21b8cc9 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb and b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb differ diff --git a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe index 05bb4c5..9c113c8 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe differ diff --git a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb index 9f0e2f3..21b8cc9 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb differ diff --git a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache index 752f0bd..65e2bca 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ