From: gu.martinm@gmail.com Date: Sun, 22 Dec 2013 22:13:36 +0000 (+0100) Subject: Chapter 8 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=aebe02af7526c3da04d9377647282b9bd68a3a33;p=CSharpForFun%2F.git Chapter 8 --- diff --git a/CSharpInDepth/Chapter8/Chapter8.sln b/CSharpInDepth/Chapter8/Chapter8.sln new file mode 100644 index 0000000..8f1672d --- /dev/null +++ b/CSharpInDepth/Chapter8/Chapter8.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter8", "Chapter8\Chapter8.csproj", "{E962139C-E7D8-401B-8320-D13A7B189110}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E962139C-E7D8-401B-8320-D13A7B189110}.Debug|x86.ActiveCfg = Debug|x86 + {E962139C-E7D8-401B-8320-D13A7B189110}.Debug|x86.Build.0 = Debug|x86 + {E962139C-E7D8-401B-8320-D13A7B189110}.Release|x86.ActiveCfg = Release|x86 + {E962139C-E7D8-401B-8320-D13A7B189110}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Chapter8\Chapter8.csproj + EndGlobalSection +EndGlobal diff --git a/CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs b/CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs new file mode 100644 index 0000000..b0cf2a0 --- /dev/null +++ b/CSharpInDepth/Chapter8/Chapter8/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("Chapter8")] +[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/Chapter8/Chapter8/Chapter8.csproj b/CSharpInDepth/Chapter8/Chapter8/Chapter8.csproj new file mode 100644 index 0000000..42972b5 --- /dev/null +++ b/CSharpInDepth/Chapter8/Chapter8/Chapter8.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {E962139C-E7D8-401B-8320-D13A7B189110} + Exe + Chapter8 + Chapter8 + + + 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/Chapter8/Chapter8/Main.cs b/CSharpInDepth/Chapter8/Chapter8/Main.cs new file mode 100644 index 0000000..02317df --- /dev/null +++ b/CSharpInDepth/Chapter8/Chapter8/Main.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; + +namespace Chapter8 +{ + class Chapter8 + { + public static void Main (string[] args) + { + + /** + * + * Listing 8.1 Counting instances awkwardly with a static automatic property. + */ + Console.WriteLine ("Listing 8.1: Counting instances awkwardly with a static automatic property."); + Person person = new Person ("Beren", 71); + Console.WriteLine ("Name: {0}, Age: {1}", person.Name, person.Age); + + + /** + * + * Listing 8.2 A fairly simple Person class used for further demonstrations. + */ + Console.WriteLine ("Listing 8.2: A fairly simple Person class used for further demonstrations."); + NewPerson beor = new NewPerson (); + beor.Name = "Bëor"; + beor.Age = 93; + + NewPerson beor2 = new NewPerson ("Bëor"); + beor2.Age = 93; + + NewPerson beor3 = new NewPerson () { Name = "Bëor", Age = 93 }; + NewPerson beor4 = new NewPerson { Name = "Bëor", Age = 93 }; + NewPerson beor5 = new NewPerson ("Bëor") { Age = 93 }; + + NewPerson[] houseOfBeor = new NewPerson[] + { + new NewPerson { Name = "Baran", Age = 91 }, + new NewPerson { Name = "Boron", Age = 93 }, + new NewPerson { Name = "Boromir", Age = 94 }, + new NewPerson { Name = "Bregor", Age = 62 }, + new NewPerson { Name = "Barahir", Age = 60 } + }; + + NewPerson beor6 = new NewPerson ("Bëor"); + beor6.Age = 93; + beor6.Home.Country = "Estolad"; + beor6.Home.Town = "Estolad"; + + NewPerson beor7 = new NewPerson ("Bëor") + { + Age = 93, + Home = { Country = "Estolad", Town = "Estolad" } + }; + + + /** + * + * Listing 8.3 Building up a rich object using object and collection initializers. + */ + Console.WriteLine ("Listing 8.3: Building up a rich object using object and collection initializers."); + NewPerson beor8 = new NewPerson + { + Name = "Bëor", + Age = 93, + Home = { Town = "Estolad", Country = "Estolad" }, + Friends = + { + new NewPerson { Name = "Baran" }, + new NewPerson("Boron"), + new NewPerson { Name = "Boromir", Age = 94 } + } + }; + + + /** + * + * Listing 8.4 Creating objects of an anonymous type with Name and Age properties. + */ + Console.WriteLine ("Listing 8.4: Creating objects of an anonymous type with Name and Age properties."); + var finwe = new { Name = "Finwë", Age = 4293 }; + var feanor = new { Name = "Fëanor", Age = 3142 }; + var fingolfin = new { Name = "Fingolfin", Age = 3426 }; + Console.WriteLine ("{0} was {1} years old when he died", finwe.Name, finwe.Age); + Console.WriteLine ("{0} was {1} years old when he died", feanor.Name, feanor.Age); + Console.WriteLine ("{0} was {1} years old when he died", fingolfin.Name, fingolfin.Age); + + + /** + * + * Listing 8.5 Populating an array using anonymous types and then finding the total age. + */ + Console.WriteLine ("Listing 8.5: Populating an array using anonymous types and then finding the total age."); + var houseOfBeor2 = new [] + { + new { Name = "Baran", Age = 91 }, + new { Name = "Boron", Age = 93 }, + new { Name = "Boromir", Age = 94 }, + new { Name = "Bregor", Age = 62 }, + new { Name = "Barahir", Age = 60 } + }; + + int totalAge = 0; + foreach (var man in houseOfBeor2) { + totalAge += man.Age; + } + + Console.WriteLine ("Total age: {0}", totalAge); + + + /** + * + * Listing 8.6 Transformation from Person to a name and adulthood flag. + */ + Console.WriteLine ("Listing 8.6: Transformation from Person to a name and adulthood flag."); + List houseOfBeor3 = new List + { + new NewPerson { Name = "Baran", Age = 91 }, + new NewPerson { Name = "Boron", Age = 93 }, + new NewPerson { Name = "Boromir", Age = 94 }, + new NewPerson { Name = "Bregor", Age = 62 }, + new NewPerson { Name = "Barahir", Age = 60 } + }; + var converted = houseOfBeor3.ConvertAll (delegate(NewPerson man) + { return new { man.Name, IsAdult = (man.Age >= 18) };} + ); + foreach (var man in converted) + { + Console.WriteLine("{0} is an adult? {1}", man.Name, man.IsAdult); + } + } + } + + public class Person + { + public string Name { get; private set; } + public int Age { get; private set; } + + private static int InstanceCounter { get; set; } + private static readonly object counterLock = new object(); + + public Person (string name, int age) + { + Name = name; + Age = age; + lock (counterLock) { + InstanceCounter++; + } + } + } + + public class NewPerson + { + public int Age { get; set; } + public string Name { get; set; } + + List friends = new List(); + public List Friends { get { return friends; } } + + Location home = new Location(); + public Location Home { get { return home; } } + + public NewPerson () { } + + public NewPerson (string name) + { + Name = name; + } + } + + public class Location + { + public string Country { get; set; } + public string Town { get; set; } + } +} diff --git a/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe b/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe new file mode 100755 index 0000000..d07f3a8 Binary files /dev/null and b/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe differ diff --git a/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb b/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb new file mode 100644 index 0000000..cd086d2 Binary files /dev/null and b/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb differ