From: gu.martinm@gmail.com Date: Tue, 28 Jan 2014 22:58:05 +0000 (+0100) Subject: C# In Depth: Chapter 4 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=948c6c15aecfc870915166e6639d8f04d5dd3a70;p=CSharpForFun%2F.git C# In Depth: Chapter 4 --- diff --git a/CSharpInDepth/Chapter4/Chapter4.sln b/CSharpInDepth/Chapter4/Chapter4.sln new file mode 100644 index 0000000..d552b7f --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4.sln @@ -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 index 0000000..6d185f1 --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4.userprefs @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs b/CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs new file mode 100644 index 0000000..044b596 --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4/AssemblyInfo.cs @@ -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 index 0000000..7f5466e --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4/Chapter4.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {37471E60-2441-47FB-9060-DDEFD1FB9266} + Exe + Chapter4 + Chapter4 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + true + + + none + true + bin\Release + prompt + 4 + x86 + true + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter4/Chapter4/Main.cs b/CSharpInDepth/Chapter4/Chapter4/Main.cs new file mode 100644 index 0000000..6b5d58d --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4/Main.cs @@ -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 + * + */ + Console.WriteLine("Listing 4.1 Using various members of Nullable"); + Nullable x = 5; + x = new Nullable(5); + Console.WriteLine("Instance with value:"); + Display(x); + + x = new Nullable(); + 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 nullable42 = 5; + + object boxed42 = nullable42; + Console.WriteLine(boxed42.GetType()); + + int normal = (int)boxed42; + Console.WriteLine(normal); + + nullable42 = (Nullable)boxed42; + Console.WriteLine(nullable42); + + nullable42 = new Nullable(); + boxed42 = nullable42; + Console.WriteLine(boxed42 == null); + + nullable42 = (Nullable)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)boxed43; + Console.WriteLine(nullable43); + + nullable43 = new Nullable(); + boxed43 = nullable43; + Console.WriteLine(boxed43 == null); + + nullable43 = (Nullable)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 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 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 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 index 0000000..a8d1a68 --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs @@ -0,0 +1,2 @@ +// +[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 index 0000000..324bb91 --- /dev/null +++ b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.csproj.FilesWrittenAbsolute.txt @@ -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 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 index 0000000..9fcd319 Binary files /dev/null and b/CSharpInDepth/Chapter4/Chapter4/obj/x86/Debug/Chapter4.exe.mdb differ