From: Gustavo Martin Date: Wed, 1 Jan 2014 21:53:52 +0000 (+0100) Subject: C# In Depth: Chapter 10 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d8b768e72607ffba7cf37da122b51f43597f5507;p=CSharpForFun%2F.git C# In Depth: Chapter 10 --- diff --git a/CSharpInDepth/Chapter10/Chapter10.sln b/CSharpInDepth/Chapter10/Chapter10.sln new file mode 100644 index 0000000..ab529fb --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter10", "Chapter10\Chapter10.csproj", "{33082672-213E-4011-9D0C-0CB052B64E64}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {33082672-213E-4011-9D0C-0CB052B64E64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33082672-213E-4011-9D0C-0CB052B64E64}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33082672-213E-4011-9D0C-0CB052B64E64}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33082672-213E-4011-9D0C-0CB052B64E64}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/CSharpInDepth/Chapter10/Chapter10.v12.suo b/CSharpInDepth/Chapter10/Chapter10.v12.suo new file mode 100644 index 0000000..668b7e7 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10.v12.suo differ diff --git a/CSharpInDepth/Chapter10/Chapter10/App.config b/CSharpInDepth/Chapter10/Chapter10/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter10/Chapter10/Chapter10.csproj b/CSharpInDepth/Chapter10/Chapter10/Chapter10.csproj new file mode 100644 index 0000000..fad2300 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/Chapter10.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {33082672-213E-4011-9D0C-0CB052B64E64} + Exe + Properties + Chapter10 + Chapter10 + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter10/Chapter10/Program.cs b/CSharpInDepth/Chapter10/Chapter10/Program.cs new file mode 100644 index 0000000..faf6a57 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/Program.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Chapter10 +{ + class Program + { + static void Main(string[] args) + { + + /** + * + * Listing 10.2 Using StreamUtil to copy a web response stream to a file. + */ + Console.WriteLine("Listing 10.2 Using StreamUtil to copy a web response stream to a file."); + WebRequest request1 = WebRequest.Create("http://gumartinm.name"); + using (WebResponse response1 = request1.GetResponse()) + using (Stream responseStream1 = response1.GetResponseStream()) + using (FileStream output1 = File.Create("response.dat")) + { + StreamUtil.Copy(responseStream1, output1); + } + + /** + * + * Listing 10.4 Copying a stream using an extension method. + */ + Console.WriteLine("Listing 10.4 Copying a stream using an extension method."); + WebRequest request2 = WebRequest.Create("http://gumartinm.name"); + using (WebResponse response2 = request2.GetResponse()) + using (Stream responseStream2 = response2.GetResponseStream()) + using (FileStream output2 = File.Create("response.dat")) + { + responseStream2.CopyToExtension(output2); + } + + /** + * + * Listing 10.5 Extension method being called on a null reference. + */ + Console.WriteLine("Listing 10.5 Extension method being called on a null reference."); + object y = null; + Console.WriteLine(y.IsNull()); + y = new object(); + Console.WriteLine(y.IsNull()); + + /** + * + * Listing 10.6 Using Enumerable.Range to print out the numbers 0 to 9. + */ + Console.WriteLine("Listing 10.6 Using Enumerable.Range to print out the numbers 0 to 9."); + var collection1 = Enumerable.Range(0, 10); + foreach (var element in collection1) + { + Console.WriteLine(element); + } + + /** + * + * Listing 10.7 Reversing a collection with the Reverse method. + */ + Console.WriteLine("Listing 10.7 Reversing a collection with the Reverse method."); + var collection2 = Enumerable.Range(0, 10).Reverse(); + foreach (var element in collection2) + { + Console.WriteLine(element); + } + + /** + * + * Listing 10.8 Using the Where method with a lambda expression to find odd numbers. + */ + Console.WriteLine("Listing 10.8 Using the Where method with a lambda expression to find odd numbers."); + var collection3 = Enumerable.Range(0, 10).Where(x => x % 2 != 0).Reverse(); + foreach (var element in collection3) + { + Console.WriteLine(element); + } + + /** + * + * Listing 10.9 Projection using a lambda expression and an anonymous type. + */ + Console.WriteLine("Listing 10.9 Projection using a lambda expression and an anonymous type."); + var collection4 = Enumerable.Range(0, 10) + .Where(x => x % 2 != 0) + .Reverse() + .Select(x => new { Original = x, SquareRoot = Math.Sqrt(x) }); + foreach(var element in collection4) + { + Console.WriteLine(element); + } + + /** + * + * Listing 10.10 Ordering a sequence by two properties. + */ + Console.WriteLine("Listing 10.10 Ordering a sequence by two properties."); + var collection5 = Enumerable.Range(-5, 11) + .Select(x => new { Original = x, Square = x * x }) + .OrderBy(x => x.Square) + .ThenBy(x => x.Original); + foreach (var element in collection5) + { + Console.WriteLine(element); + } + + // Wait for key. + Console.ReadKey(); + } + } + + public static class StreamUtil + { + const int BufferSize = 8192; + + public static void Copy(Stream input, Stream output) + { + byte[] buffer = new byte[BufferSize]; + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + output.Write(buffer, 0, read); + } + } + + public static byte[] ReadFully(Stream input) + { + using (MemoryStream tempStream = new MemoryStream()) + { + Copy(input, tempStream); + return tempStream.ToArray(); + } + } + } + + public static class StreamUtilExtension + { + const int BufferSize = 8192; + + public static void CopyToExtension(this Stream input, Stream output) + { + byte[] buffer = new byte[BufferSize]; + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + output.Write(buffer, 0, read); + } + } + + public static byte[] ReadFully(this Stream input) + { + using (MemoryStream tempStream = new MemoryStream()) + { + CopyToExtension(input, tempStream); + return tempStream.ToArray(); + } + } + } + + public static class NullUtil + { + public static bool IsNull(this object x) + { + return x == null; + } + } +} diff --git a/CSharpInDepth/Chapter10/Chapter10/Properties/AssemblyInfo.cs b/CSharpInDepth/Chapter10/Chapter10/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..8e3f607 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Chapter10")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Chapter10")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e813db06-46dd-411c-b1a4-2c9f4b7b20e1")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe new file mode 100644 index 0000000..2902cb3 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe differ diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe.config b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.pdb b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.pdb new file mode 100644 index 0000000..60a6ed9 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.pdb differ diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe new file mode 100644 index 0000000..c0dfecc Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe differ diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.config b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.manifest b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.manifest new file mode 100644 index 0000000..061c9ca --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/Chapter10.vshost.exe.manifest @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/CSharpInDepth/Chapter10/Chapter10/bin/Debug/response.dat b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/response.dat new file mode 100644 index 0000000..45e22cc --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/bin/Debug/response.dat @@ -0,0 +1,122 @@ + + + + gumartinm.name » index + + + + + +
+
+
+

gumartinm.name

+
+ +

java

+ +
+ + +

android

+ +
+ + +

symfony

+ +
+ + +

java spring

+ + +
+ + +

javapos

+ +
+ + +

java mybatis

+ +
+ + +

C

+ +
+ + +

C#

+ +
+ + +

Light color means currently no plans for further development.

+
+ +
+
+

contact

+ +

+ Gustavo Martin Morcuende
+

+
+
+
+
+
+ + diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csproj.FileListAbsolute.txt b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..a42e3d6 --- /dev/null +++ b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csproj.FileListAbsolute.txt @@ -0,0 +1,6 @@ +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\bin\Debug\Chapter10.exe.config +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\bin\Debug\Chapter10.exe +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\bin\Debug\Chapter10.pdb +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\obj\Debug\Chapter10.csprojResolveAssemblyReference.cache +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\obj\Debug\Chapter10.exe +C:\Users\Gustavo\Source\Repos\CSharpForFun\CSharpInDepth\Chapter10\Chapter10\obj\Debug\Chapter10.pdb diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csprojResolveAssemblyReference.cache b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csprojResolveAssemblyReference.cache new file mode 100644 index 0000000..9542bb3 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.csprojResolveAssemblyReference.cache differ diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.exe b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.exe new file mode 100644 index 0000000..2902cb3 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.exe differ diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.pdb b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.pdb new file mode 100644 index 0000000..60a6ed9 Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/Chapter10.pdb differ diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..f05787a Binary files /dev/null and b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs new file mode 100644 index 0000000..e69de29 diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs new file mode 100644 index 0000000..e69de29 diff --git a/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs b/CSharpInDepth/Chapter10/Chapter10/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs new file mode 100644 index 0000000..e69de29 diff --git a/CSharpInDepth/Chapter9/Chapter9.v12.suo b/CSharpInDepth/Chapter9/Chapter9.v12.suo index 73aa76d..124c6c2 100644 Binary files a/CSharpInDepth/Chapter9/Chapter9.v12.suo and b/CSharpInDepth/Chapter9/Chapter9.v12.suo differ