Chapter 8
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 22 Dec 2013 22:13:36 +0000 (23:13 +0100)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 22 Dec 2013 22:13:36 +0000 (23:13 +0100)
CSharpInDepth/Chapter8/Chapter8.sln [new file with mode: 0644]
CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs [new file with mode: 0644]
CSharpInDepth/Chapter8/Chapter8/Chapter8.csproj [new file with mode: 0644]
CSharpInDepth/Chapter8/Chapter8/Main.cs [new file with mode: 0644]
CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe [new file with mode: 0755]
CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb [new file with mode: 0644]

diff --git a/CSharpInDepth/Chapter8/Chapter8.sln b/CSharpInDepth/Chapter8/Chapter8.sln
new file mode 100644 (file)
index 0000000..8f1672d
--- /dev/null
@@ -0,0 +1,20 @@
+\r
+Microsoft Visual Studio Solution File, Format Version 11.00\r
+# Visual Studio 2010\r
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter8", "Chapter8\Chapter8.csproj", "{E962139C-E7D8-401B-8320-D13A7B189110}"\r
+EndProject\r
+Global\r
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution\r
+               Debug|x86 = Debug|x86\r
+               Release|x86 = Release|x86\r
+       EndGlobalSection\r
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution\r
+               {E962139C-E7D8-401B-8320-D13A7B189110}.Debug|x86.ActiveCfg = Debug|x86\r
+               {E962139C-E7D8-401B-8320-D13A7B189110}.Debug|x86.Build.0 = Debug|x86\r
+               {E962139C-E7D8-401B-8320-D13A7B189110}.Release|x86.ActiveCfg = Release|x86\r
+               {E962139C-E7D8-401B-8320-D13A7B189110}.Release|x86.Build.0 = Release|x86\r
+       EndGlobalSection\r
+       GlobalSection(MonoDevelopProperties) = preSolution\r
+               StartupItem = Chapter8\Chapter8.csproj\r
+       EndGlobalSection\r
+EndGlobal\r
diff --git a/CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs b/CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs
new file mode 100644 (file)
index 0000000..b0cf2a0
--- /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("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 (file)
index 0000000..42972b5
--- /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>{E962139C-E7D8-401B-8320-D13A7B189110}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>Chapter8</RootNamespace>
+    <AssemblyName>Chapter8</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/Chapter8/Chapter8/Main.cs b/CSharpInDepth/Chapter8/Chapter8/Main.cs
new file mode 100644 (file)
index 0000000..02317df
--- /dev/null
@@ -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<NewPerson> houseOfBeor3 = new List<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 }
+                       };
+                       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<NewPerson> friends = new List<NewPerson>();
+               public List<NewPerson> 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 (executable)
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 (file)
index 0000000..cd086d2
Binary files /dev/null and b/CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb differ