C# In Depth: Chapter 9
authorGustavo Martin <gu.martinm@gmail.com>
Wed, 1 Jan 2014 21:08:18 +0000 (22:08 +0100)
committerGustavo Martin <gu.martinm@gmail.com>
Wed, 1 Jan 2014 21:08:18 +0000 (22:08 +0100)
CSharpInDepth/Chapter9/Chapter9.v12.suo
CSharpInDepth/Chapter9/Chapter9/Form1.cs
CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe
CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb
CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe
CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb
CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache

index 5b7fee9..73aa76d 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9.v12.suo and b/CSharpInDepth/Chapter9/Chapter9.v12.suo differ
index 366618e..839212f 100644 (file)
@@ -14,6 +14,8 @@ namespace Chapter9
 {
     public partial class Form1 : Form
     {
+        delegate T MyFunc<T>();
+
         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, TOutput>(TInput input, Converter<TInput, TOutput> converter)
+        {
+            Console.WriteLine(converter(input));
+        }
+
+        static void WriteResult<T>(MyFunc<T> function)
+        {
+            Console.WriteLine(function());
+        }
+
+        static void PrintType<T>(T first, T second)
+        {
+            Console.WriteLine(typeof(T));
+        }
+
+        static void ConvertTwice<TInput, TMiddle, TOutput>(TInput input, 
+                                                           Converter<TInput, TMiddle> firstConversion,
+                                                           Converter<TMiddle, TOutput> secondConversion)
+        {
+            TMiddle middle = firstConversion(input);
+            TOutput output = secondConversion(middle);
+            Console.WriteLine(output);
+        }
+
+        static void Execute(Func<int> action)
+        {
+            Console.WriteLine("action returns an int: " + action());
+        }
+
+        static void Execute(Func<double> action)
+        {
+            Console.WriteLine("action returns a double: " + action());
+        }
     }
 
     class Film
index 05bb4c5..9c113c8 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe and b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.exe differ
index 9f0e2f3..21b8cc9 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb and b/CSharpInDepth/Chapter9/Chapter9/bin/Debug/Chapter9.pdb differ
index 05bb4c5..9c113c8 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.exe differ
index 9f0e2f3..21b8cc9 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/Chapter9.pdb differ
index 752f0bd..65e2bca 100644 (file)
Binary files a/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/CSharpInDepth/Chapter9/Chapter9/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ