C# In Depth: Chapter 4
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 28 Jan 2014 22:58:05 +0000 (23:58 +0100)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 28 Jan 2014 22:58:05 +0000 (23:58 +0100)
CSharpInDepth/Chapter4/Chapter4.sln [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4.userprefs [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/Chapter4.csproj [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/Main.cs [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe [new file with mode: 0755]
CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe.mdb [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.csproj.FilesWrittenAbsolute.txt [new file with mode: 0644]
CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe [new file with mode: 0755]
CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb [new file with mode: 0644]

diff --git a/CSharpInDepth/Chapter4/Chapter4.sln b/CSharpInDepth/Chapter4/Chapter4.sln
new file mode 100644 (file)
index 0000000..d552b7f
--- /dev/null
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter4", "Chapter4\Chapter4.csproj", "{37471E60-2441-47FB-9060-DDEFD1FB9266}"
+EndProject
+Global
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution
+               Debug|x86 = Debug|x86
+               Release|x86 = Release|x86
+       EndGlobalSection
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution
+               {37471E60-2441-47FB-9060-DDEFD1FB9266}.Debug|x86.ActiveCfg = Debug|x86
+               {37471E60-2441-47FB-9060-DDEFD1FB9266}.Debug|x86.Build.0 = Debug|x86
+               {37471E60-2441-47FB-9060-DDEFD1FB9266}.Release|x86.ActiveCfg = Release|x86
+               {37471E60-2441-47FB-9060-DDEFD1FB9266}.Release|x86.Build.0 = Release|x86
+       EndGlobalSection
+       GlobalSection(MonoDevelopProperties) = preSolution
+               StartupItem = Chapter4\Chapter4.csproj
+       EndGlobalSection
+EndGlobal
diff --git a/CSharpInDepth/Chapter4/Chapter4.userprefs b/CSharpInDepth/Chapter4/Chapter4.userprefs
new file mode 100644 (file)
index 0000000..6d185f1
--- /dev/null
@@ -0,0 +1,12 @@
+<Properties>
+  <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
+  <MonoDevelop.Ide.Workbench ActiveDocument="Chapter4/Main.cs">
+    <Files>
+      <File FileName="Chapter4/Main.cs" Line="124" Column="7" />
+    </Files>
+  </MonoDevelop.Ide.Workbench>
+  <MonoDevelop.Ide.DebuggingService.Breakpoints>
+    <BreakpointStore />
+  </MonoDevelop.Ide.DebuggingService.Breakpoints>
+  <MonoDevelop.Ide.DebuggingService.PinnedWatches />
+</Properties>
\ No newline at end of file
diff --git a/CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs b/CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs
new file mode 100644 (file)
index 0000000..044b596
--- /dev/null
@@ -0,0 +1,27 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes. 
+// Change them to the values specific to your project.
+
+[assembly: AssemblyTitle("Chapter4")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("gustavo")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+
+[assembly: AssemblyVersion("1.0.*")]
+
+// The following attributes are used to specify the signing key for the assembly, 
+// if desired. See the Mono documentation for more information about signing.
+
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+
diff --git a/CSharpInDepth/Chapter4/Chapter4/Chapter4.csproj b/CSharpInDepth/Chapter4/Chapter4/Chapter4.csproj
new file mode 100644 (file)
index 0000000..7f5466e
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>10.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{37471E60-2441-47FB-9060-DDEFD1FB9266}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>Chapter4</RootNamespace>
+    <AssemblyName>Chapter4</AssemblyName>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>none</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Main.cs" />
+    <Compile Include="AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/CSharpInDepth/Chapter4/Chapter4/Main.cs b/CSharpInDepth/Chapter4/Chapter4/Main.cs
new file mode 100644 (file)
index 0000000..6b5d58d
--- /dev/null
@@ -0,0 +1,155 @@
+using System;
+using System.Text.RegularExpressions;
+using System.Runtime.InteropServices;
+
+namespace Chapter4
+{
+       class MainClass
+       {
+               public static void Main (string[] args)
+               {
+                       /**
+                        * Listing 4.1 Using various members of Nullable<T>
+                        * 
+                        */
+                       Console.WriteLine("Listing 4.1 Using various members of Nullable<T>");
+                       Nullable<int> x = 5;
+                       x = new Nullable<int>(5);
+                       Console.WriteLine("Instance with value:");
+                       Display(x);
+
+                       x = new Nullable<int>();
+                       Console.WriteLine("Instance without value:");
+                       Display(x);
+
+                       /**
+                        * Listing 4.2 Boxing and unboxing behaviour of nullable types.
+                        *
+                        */
+                       Console.WriteLine("Listing 4.2 Boxing and unboxing behaviour of nullable types.");
+                       Nullable<int> nullable42 = 5;
+
+                       object boxed42 = nullable42;
+                       Console.WriteLine(boxed42.GetType());
+
+                       int normal = (int)boxed42;
+                       Console.WriteLine(normal);
+
+                       nullable42 = (Nullable<int>)boxed42;
+                       Console.WriteLine(nullable42);
+
+                       nullable42 = new Nullable<int>();
+                       boxed42 = nullable42;
+                       Console.WriteLine(boxed42 == null);
+
+                       nullable42 = (Nullable<int>)boxed42;
+                       Console.WriteLine(nullable42.HasValue);
+
+                       /**
+                        * Listing 4.3 The same code as 4.2 but using the ? modifier.
+                        *
+                        */
+                       Console.WriteLine("Listing 4.3 The same code as 4.2 but using the ? modifier.");
+                       int? nullable43 = 5;
+
+                       object boxed43 = nullable43;
+                       Console.WriteLine(boxed43.GetType());
+
+                       int normal43 = (int)boxed43;
+                       Console.WriteLine(normal43);
+
+                       nullable43 = (Nullable<int>)boxed43;
+                       Console.WriteLine(nullable43);
+
+                       nullable43 = new Nullable<int>();
+                       boxed43 = nullable43;
+                       Console.WriteLine(boxed43 == null);
+
+                       nullable43 = (Nullable<int>)boxed43;
+                       Console.WriteLine(nullable43.HasValue);
+
+                       /**
+                        * Listing 4.4 Part of a Person class including calculation of age.
+                        *
+                        */
+                       Console.WriteLine("Listing 4.4 Part of a Person class including calculation of age.");
+                       Person turing = new Person("Alan Turing ", new DateTime(1912, 6, 23), new DateTime(1954, 6, 7));
+                       Person knuth = new Person("Donald Knuth ", new DateTime(1938, 1, 10), null);
+                       Console.WriteLine(turing.Age.Days);
+                       Console.WriteLine(knuth.Age.Days);
+                       Console.WriteLine(turing.AgeCoalescing.Days);
+                       Console.WriteLine(knuth.AgeCoalescing.Days);
+
+                       /**
+                        * Listing 4.5 An alternative implementation of th TryXXX patern.
+                        *
+                        */
+                       Console.WriteLine("Listing 4.5 An alternative implementation of th TryXXX patern.");
+                       int? parsed = TryParse("Not valid");
+                       if (parsed != null) {
+                               Console.WriteLine("Parsed to {0}", parsed.Value);
+                       } else {
+                               Console.WriteLine("Couldn't parse");
+                       }
+               }
+
+               static void Display(Nullable<int> x)
+               {
+                       Console.WriteLine("HasValue: {0}", x.HasValue);
+                       if (x.HasValue) {
+                               Console.WriteLine("Value: {0}", x.Value);
+                               Console.WriteLine("Explicit conversion: {0}", (int)x);
+                       }
+                       Console.WriteLine("GetValueOrDefault(): {0}", x.GetValueOrDefault ());
+                       Console.WriteLine("GetValueOrDefault(10): {0}", x.GetValueOrDefault (10));
+                       Console.WriteLine("ToString(): \"{0}\"", x.ToString());
+                       Console.WriteLine("GetHashCode(): {0}", x.GetHashCode());
+                       Console.WriteLine();
+               }
+
+               class Person
+               {
+                       DateTime birth;
+                       DateTime? death;
+                       string name;
+
+                       public TimeSpan Age
+                       {
+                               get {
+                                       if (death == null) {
+                                               return DateTime.Now - birth;
+                                       } 
+                                       else {
+                                               return death.Value - birth;
+                                       }
+                               }
+                       }
+
+                       public TimeSpan AgeCoalescing
+                       {
+                               get {
+                                       DateTime lastAlive = death ?? DateTime.Now;
+                                       return lastAlive - birth;
+                               }
+                       }
+
+                       public Person(string name, DateTime birth, DateTime? death)
+                       {
+                               this.birth = birth;
+                               this.death = death;
+                               this.name = name;
+                       }
+               }
+
+               static int? TryParse(string text)
+               {
+                       int ret;
+                       if (int.TryParse(text, out ret)) {
+                               return ret;
+                       } 
+                       else {
+                               return null;
+                       }
+               }
+       }
+}
diff --git a/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe b/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe
new file mode 100755 (executable)
index 0000000..4f7a656
Binary files /dev/null and b/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe differ
diff --git a/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe.mdb b/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe.mdb
new file mode 100644 (file)
index 0000000..9fcd319
Binary files /dev/null and b/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe.mdb differ
diff --git a/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs
new file mode 100644 (file)
index 0000000..a8d1a68
--- /dev/null
@@ -0,0 +1,2 @@
+// <autogenerated />
+[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.0", FrameworkDisplayName = "")]
diff --git a/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.csproj.FilesWrittenAbsolute.txt b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.csproj.FilesWrittenAbsolute.txt
new file mode 100644 (file)
index 0000000..324bb91
--- /dev/null
@@ -0,0 +1,5 @@
+/home/gustavo/github/CSharpForFun/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs
+/home/gustavo/github/CSharpForFun/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe.mdb
+/home/gustavo/github/CSharpForFun/CSharpInDepth/Chapter4/Chapter4/bin/Debug/Chapter4.exe
+/home/gustavo/github/CSharpForFun/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe
+/home/gustavo/github/CSharpForFun/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb
diff --git a/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe
new file mode 100755 (executable)
index 0000000..4f7a656
Binary files /dev/null and b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe differ
diff --git a/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb
new file mode 100644 (file)
index 0000000..9fcd319
Binary files /dev/null and b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb differ