From aebe02af7526c3da04d9377647282b9bd68a3a33 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 22 Dec 2013 23:13:36 +0100 Subject: [PATCH] Chapter 8 --- CSharpInDepth/Chapter8/Chapter8.sln | 20 +++ CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs | 27 ++++ CSharpInDepth/Chapter8/Chapter8/Chapter8.csproj | 41 +++++ CSharpInDepth/Chapter8/Chapter8/Main.cs | 176 +++++++++++++++++++++ .../Chapter8/Chapter8/bin/Debug/Chapter8.exe | Bin 0 -> 10240 bytes .../Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb | Bin 0 -> 2228 bytes 6 files changed, 264 insertions(+) create mode 100644 CSharpInDepth/Chapter8/Chapter8.sln create mode 100644 CSharpInDepth/Chapter8/Chapter8/AssemblyInfo.cs create mode 100644 CSharpInDepth/Chapter8/Chapter8/Chapter8.csproj create mode 100644 CSharpInDepth/Chapter8/Chapter8/Main.cs create mode 100755 CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe create mode 100644 CSharpInDepth/Chapter8/Chapter8/bin/Debug/Chapter8.exe.mdb 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 0000000000000000000000000000000000000000..d07f3a8f9c066d017ae832b4bff3adb685270f90 GIT binary patch literal 10240 zcmeHNYiu0Xbv}1?Ib1Hu6=zqZNRbjpmK3igcSG5XBRee3ryjK|sTM8EFHytgkQ{5d zL(eQJF^&|vv5h2>?W#?jSZNW<&8w*UNRh&B-~=iH88sRQZBf7|nxJayqK~AoQMf=; zG*Z8F?u>RtGD3gkUoz61d*1h)bI-jqJJP)m{W>LxD2e;(Rif80@--mv#$XBJO!qf4 z^o@qMHod0yy|rm<#tZGaz@HABS-a?z%YMb4aP45eY&CTg8k?Vt_ zMEg{N9xA?iUp(7o%BV(V64~HzV0_upJ%Ev;voRP|ajZs5nhz*>5w*zQ>KKu%fUDgm zI>5*P(ONFV^6MB;({kSbgKn{jQZ;&$p!d|!g^GK!0{TZA0U(R>0e0=OiH;S5Feri} zu?+y>L~p`fa}6N+Lg1EsC`zoyAVh8G1GsCh0ivNAQqZegTWs*JT?4j8ABsk%_5uF{b2N@d#~e_X*MkSf$J6~OC^du>AVf9qu<&_ba1U+7ldbi9`v&5%`RtlK8tvb zt!-5Ye;P*vy@w?nEBW%)bybB+H3~XjR%ov({8f#@(Q6($stW&5qtJg%VSQEM-5Q0y zSRtn?C3VTAthh2(%Fl7luCh%VuTzYZz>1C36kI8l!fxe(D0OFs)%o zOi4crmpa2nt1+lnxsiiTTUsID;!1|K+^Si%w2j?SX=D3bI{4!_*x>#I&Doc5bcO5S zJl7OB%-Ds7(~Rc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ycGjgABTXGB9h>LUKv^j1rJnL|$aP&NKzwoTQa=*XY%6&-6auDWoSUqmU z>h6q-5MT;N_8ONUUacDyhXZ0c2gG|*2c*-fc88X*hL4-qk*)YZ!76STTfwdFNLo6x zS~7P?GGh`h?+?9);?2)1$@_`-!7Q-rcOeJC)7#LBPr5h-2Z`vDYzA7sq%|eQn&wb% ziPwh@H=Q`c*ye8V^or^8)FzdB^`nm*?&!Vzsf3xMRBN`~$Wcy#z|UuD%{Jf{p{a*@ z+hAElsybVx@K&3ngiRSEhhOk2vmpZyUE%>BF;oU`(JIr`1y${)LuFgw21HK5YrBz2 zC2hE500#z?>mcrGZ)(kMFtRP|EMr(bO0{M?)JDX!0nRs4Dh&pUQZ}_(o8T|a3YwQ@ zjz%#DYav3zg!ir%gTv}-2EXbafvXJ}uf8MOlx?a0*-%Awq_uE^CQ|Vj%Q!)fL_Lp8AOfhD3t;y#c`(+dCK5UB~p~CM-Dts z!pyGaDhPo(g38BK9uzznUFmvE8Le!+N}2U~iSk@}03~w0Zq15P@yjYcGAVA9Cj2Uz zp2wTvf{(A?`~hgpXnP=V=JxyL(UV12-fHk7?4Jl#QYw>f4*IH^E!8gb~?!v(2AuY4Eas#GL0KE+Elvk$}0K~!40lc&PhD*l!b*MYxO zg~*L(VZAsr!u%_3yJ~Xf_hO!xKf>F%2jp8s?00#k2v6?<>RQ0R_3e-<7f|UPsQC`! zf50Mqk!VI-2cmp=wK$j9U;KPp8CL;uoW#x7trB0ghPpfS+aveUafw#PD+WI=z}Ek^ OVsK;s;`+I(!2bc3@5R&r literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..cd086d20021adefdce48dac67fb59782fa9efa9f GIT binary patch literal 2228 zcmbtWdu$X%9G;o&&F;?L(Uu~Q77(RMiKdl;Kq@KFB9(+fu`wprM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