From f9b61a3de4f1e500022bacaa35d5fec6b59eaa7e Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 20 Dec 2013 22:42:17 +0100 Subject: [PATCH] C# In Depth: Chapters 5 and 6 --- CSharpInDepth/Chapter5/Chapter5.sln | 20 ++ CSharpInDepth/Chapter5/Chapter5.userprefs | 13 + CSharpInDepth/Chapter5/Chapter5/AssemblyInfo.cs | 27 ++ CSharpInDepth/Chapter5/Chapter5/Chapter5.csproj | 74 +++++ CSharpInDepth/Chapter5/Chapter5/Main.cs | 306 +++++++++++++++++++++ CSharpInDepth/Chapter5/Chapter5/MainWindow.cs | 16 ++ .../Chapter5/Chapter5/bin/Debug/Chapter5.exe | Bin 0 -> 12288 bytes .../Chapter5/Chapter5/bin/Debug/Chapter5.exe.mdb | Bin 0 -> 4095 bytes .../Chapter5/Chapter5/gtk-gui/MainWindow.cs | 27 ++ .../Chapter5/Chapter5/gtk-gui/generated.cs | 29 ++ CSharpInDepth/Chapter5/Chapter5/gtk-gui/gui.stetic | 21 ++ CSharpInDepth/Chapter6/Chapter6.sln | 20 ++ CSharpInDepth/Chapter6/Chapter6.userprefs | 12 + CSharpInDepth/Chapter6/Chapter6/AssemblyInfo.cs | 27 ++ CSharpInDepth/Chapter6/Chapter6/Chapter6.csproj | 41 +++ CSharpInDepth/Chapter6/Chapter6/Main.cs | 182 ++++++++++++ .../Chapter6/Chapter6/bin/Debug/Chapter6.exe | Bin 0 -> 6144 bytes .../Chapter6/Chapter6/bin/Debug/Chapter6.exe.mdb | Bin 0 -> 1445 bytes 18 files changed, 815 insertions(+) create mode 100644 CSharpInDepth/Chapter5/Chapter5.sln create mode 100644 CSharpInDepth/Chapter5/Chapter5.userprefs create mode 100644 CSharpInDepth/Chapter5/Chapter5/AssemblyInfo.cs create mode 100644 CSharpInDepth/Chapter5/Chapter5/Chapter5.csproj create mode 100644 CSharpInDepth/Chapter5/Chapter5/Main.cs create mode 100644 CSharpInDepth/Chapter5/Chapter5/MainWindow.cs create mode 100755 CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe create mode 100644 CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe.mdb create mode 100644 CSharpInDepth/Chapter5/Chapter5/gtk-gui/MainWindow.cs create mode 100644 CSharpInDepth/Chapter5/Chapter5/gtk-gui/generated.cs create mode 100644 CSharpInDepth/Chapter5/Chapter5/gtk-gui/gui.stetic create mode 100644 CSharpInDepth/Chapter6/Chapter6.sln create mode 100644 CSharpInDepth/Chapter6/Chapter6.userprefs create mode 100644 CSharpInDepth/Chapter6/Chapter6/AssemblyInfo.cs create mode 100644 CSharpInDepth/Chapter6/Chapter6/Chapter6.csproj create mode 100644 CSharpInDepth/Chapter6/Chapter6/Main.cs create mode 100755 CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe create mode 100644 CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe.mdb diff --git a/CSharpInDepth/Chapter5/Chapter5.sln b/CSharpInDepth/Chapter5/Chapter5.sln new file mode 100644 index 0000000..b6a79a4 --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter5", "Chapter5\Chapter5.csproj", "{06326E93-0CD5-488A-A283-D26D36968815}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {06326E93-0CD5-488A-A283-D26D36968815}.Debug|x86.ActiveCfg = Debug|x86 + {06326E93-0CD5-488A-A283-D26D36968815}.Debug|x86.Build.0 = Debug|x86 + {06326E93-0CD5-488A-A283-D26D36968815}.Release|x86.ActiveCfg = Release|x86 + {06326E93-0CD5-488A-A283-D26D36968815}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Chapter5\Chapter5.csproj + EndGlobalSection +EndGlobal diff --git a/CSharpInDepth/Chapter5/Chapter5.userprefs b/CSharpInDepth/Chapter5/Chapter5.userprefs new file mode 100644 index 0000000..d763d61 --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5.userprefs @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter5/Chapter5/AssemblyInfo.cs b/CSharpInDepth/Chapter5/Chapter5/AssemblyInfo.cs new file mode 100644 index 0000000..c378ed2 --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5/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("Chapter5")] +[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/Chapter5/Chapter5/Chapter5.csproj b/CSharpInDepth/Chapter5/Chapter5/Chapter5.csproj new file mode 100644 index 0000000..d92dd08 --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5/Chapter5.csproj @@ -0,0 +1,74 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {06326E93-0CD5-488A-A283-D26D36968815} + WinExe + Chapter5 + Chapter5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + false + + + none + true + bin\Release + prompt + 4 + x86 + false + + + + + False + gtk-sharp-2.0 + + + False + gtk-sharp-2.0 + + + False + glib-sharp-2.0 + + + False + glade-sharp-2.0 + + + False + gtk-sharp-2.0 + + + False + gtk-sharp-2.0 + + + + + + gui.stetic + + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter5/Chapter5/Main.cs b/CSharpInDepth/Chapter5/Chapter5/Main.cs new file mode 100644 index 0000000..5cf439d --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5/Main.cs @@ -0,0 +1,306 @@ +using System; +using Gtk; +using System.IO; +using System.Collections.Generic; + +namespace Chapter5 +{ + class MainClass + { + delegate Stream StreamFactory(); + delegate void SampleDelegate(string x); + delegate void MethodInvoker(); + + public static void Main (string[] args) + { + Application.Init (); + + MainWindow win = new MainWindow (); + + + /** + * + * Listing 5.1 Subscribing to three of a button's events. + */ + Button buttonA = new Button ("Click me 5.1"); + buttonA.Pressed += new EventHandler (LogPlainEvent); + buttonA.KeyPressEvent += new KeyPressEventHandler (LogKeyEvent); + win.Add (buttonA); + + + /** + * + * Listing 5.2 Demonstration of method group conversion and delegate contravariance. + */ + Button buttonB = new Button ("Click me 5.2"); + buttonB.Pressed += LogPlainEvent; + buttonB.KeyPressEvent += LogPlainEvent; + win.Add (buttonB); + + + /** + * + * Listing 5.3 Demonstration of covariance of return types for delegates. + */ + StreamFactory factoryA = GenerateSampleData; + + using (Stream stream = factoryA()) { + int data; + + while ((data = stream.ReadByte()) != -1) { + Console.WriteLine (data); + } + } + + + /** + * + * Listing 5.4 Demonstration of braking change between C# 1 and C# 2. + */ + Derived x = new Derived (); + SampleDelegate factoryB = new SampleDelegate (x.CandidateAction); + factoryB ("test"); + + + /** + * + * Listing 5.5 Anonymous methods used with the Action delegate type. + */ + Action printReverse = delegate(string text) { + char[] chars = text.ToCharArray (); + Array.Reverse (chars); + Console.WriteLine (new String (chars)); + }; + + Action printRoot = delegate(int number) { + Console.WriteLine (Math.Sqrt (number)); + }; + + Action> printMean = delegate(IList numbers) { + double total = 0; + foreach (double value in numbers) { + total += value; + } + Console.WriteLine (total / numbers.Count); + }; + + printReverse ("Hello Gus"); + printRoot (2); + printMean (new double[] {1.5, 2.5, 3.5, 4.5, 5, 7, 10.1}); + + + /** + * + * Listing 5.7 Returning a value from an anonymous method. + */ + Predicate isEven = delegate(int number) { + return number % 2 == 0; + }; + Console.WriteLine (isEven (1)); + Console.WriteLine (isEven (4)); + + + /** + * + * Listing 5.8 Using anonymous methods to sort files simply. + */ + SortAndShowFiles ("Sorted by name:", delegate(FileInfo f1, FileInfo f2) { + return f1.Name.CompareTo (f2.Name); + } + ); + + SortAndShowFiles ("Sorted by length:", delegate(FileInfo f1, FileInfo f2) { + return f1.Length.CompareTo (f2.Length); + } + ); + + + /** + * + * Listing 5.9 Subscribing to event with anonymous methods that ignore parameters. + */ + Button buttonC = new Button ("Click me 5.9"); + buttonC.Pressed += delegate { + Console.WriteLine ("LogPlain"); + }; + buttonC.KeyPressEvent += delegate { + Console.WriteLine ("LogKey"); + }; + win.Add (buttonC); + + + /** + * + * Listing 5.10 Examples of variable kinds with respect to anonymous methods. + */ + int outerVariable = 5; + string capturedVariable = "captured"; + + if (DateTime.Now.Hour == 1) { + int normalLocalVariable = DateTime.Now.Minute; + Console.WriteLine (normalLocalVariable); + } + + MethodInvoker method = delegate() { + string anonLocal = " local to anonymous method"; + Console.WriteLine (capturedVariable + anonLocal); + }; + method (); + + + /** + * + * Listing 5.11 Accessing a variable both inside and outside an anonymous method. + */ + string captured = "before methodB is created"; + + MethodInvoker methodB = delegate { + Console.WriteLine (captured); + captured = "changed by methodB"; + }; + captured = "directly before methodB is invoked"; + methodB (); + + Console.WriteLine (captured); + + captured = "before second invocation"; + methodB (); + + + /** + * + * Listing 5.12 Demonstration of a captured variable having its lifetime extended. + */ + MethodInvoker methodC = CreateDelegateInstance (); + methodC (); + methodC (); + + + /** + * + * Listing 5.13 Capturing multiple variable instantiations with multiple delegates. + */ + List list = new List (); + + // The variable declared by the initial part of the loop is only instantiated once. + for (int index = 0; index < 5; index++) { + // Because counter is declared inside the loop, it's instantiated for each iteration. + int counter = index * 10; + list.Add (delegate { + Console.WriteLine (counter); + counter++; + } + ); + } + + foreach (MethodInvoker t in list) { + t (); + } + list [0] (); + list [0] (); + list [0] (); + + list [1] (); + + + /** + * + * Listing 5.14 Capturing variables in different scopes. Warning: nasty code ahead! + */ + MethodInvoker[] delegates = new MethodInvoker[2]; + + int outside = 0; + + for (int i = 0; i < 2; i++) { + // Because inside is declared inside the loop, it's instantiated for each iteration. + int inside = 0; + + delegates[i] = delegate { + Console.WriteLine ("({0},{1})", outside, inside); + outside++; + inside++; + }; + } + + MethodInvoker first = delegates[0]; + MethodInvoker second = delegates[1]; + + first(); + first(); + first(); + + second(); + second(); + + + + win.ShowAll (); + Application.Run (); + } + + + static void LogPlainEvent (object sender, EventArgs e) + { + Console.WriteLine("LogPlain"); + } + + + static void LogKeyEvent (object sender, KeyPressEventArgs e) + { + Console.WriteLine("LogKey"); + } + + + static MemoryStream GenerateSampleData () + { + byte[] buffer = new byte[16]; + for (int i = 0; i < buffer.Length; i++) { + buffer[i] = (byte) i; + } + + return new MemoryStream(buffer); + } + + + public void CandidateAction(string x) + { + Console.WriteLine("Snippet.CandidateAction"); + } + + + public class Derived:MainClass + { + public void CandidateAction(object o) + { + Console.WriteLine("Derived.CandidateAction"); + } + } + + static void SortAndShowFiles (string title, Comparison sortOrder) + { + FileInfo[] files = new DirectoryInfo (@"/").GetFiles (); + + Array.Sort (files, sortOrder); + + Console.WriteLine (title); + foreach (FileInfo file in files) { + Console.WriteLine(" {0} ({1} bytes)", file.Name, file.Length); + } + } + + static MethodInvoker CreateDelegateInstance () + { + int counter = 5; + + MethodInvoker ret = delegate + { + Console.WriteLine(counter); + counter++; + }; + + ret(); + return ret; + } + } + +} diff --git a/CSharpInDepth/Chapter5/Chapter5/MainWindow.cs b/CSharpInDepth/Chapter5/Chapter5/MainWindow.cs new file mode 100644 index 0000000..b450b52 --- /dev/null +++ b/CSharpInDepth/Chapter5/Chapter5/MainWindow.cs @@ -0,0 +1,16 @@ +using System; +using Gtk; + +public partial class MainWindow: Gtk.Window +{ + public MainWindow (): base (Gtk.WindowType.Toplevel) + { + Build (); + } + + protected void OnDeleteEvent (object sender, DeleteEventArgs a) + { + Application.Quit (); + a.RetVal = true; + } +} diff --git a/CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe b/CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe new file mode 100755 index 0000000000000000000000000000000000000000..c5745e9a603f381bf58eee5ddd0551f180dc7017 GIT binary patch literal 12288 zcmeHNdvF|Qk?)z^on5US(d^oiY)j5qwqfm9E6I{=$;h(xvaQ4qVabkx1bekRk_NAK zmYG?{iesY?&X-&WgagBoqYeT`LF(Xu3ONWQ2rPJZ{X|IWt{b8{ZJ99vdM# zpoHk?#HWvxTYH-7lxjsIN`b0CZfWles+Rf^Czw-9|`6-mCuiL}4mgl=EW=T~p& z|4d!0rWB(1=Dp|f3hzXMQR6%p;mSJ~l$|-mx zt~xp-Q`L(_#510%Wqft2j$f&30DB=pe$hsL$5rEAG?7AO6t{3OA+kw^>SYa0Ywijl zsg(fSuX2Cabey7Bp*yfjcV7vyc&Y&+Y`a$NfKYTBf!D-VT-MO2<~9Md5yUuCV4dc! zX383XXv}WL%NZ!ws;yd9Yk{&@t4eA?65Z5XsMeBUJ#0fjOGbKNI_X-f1&Z~Ey%w*H zNp0nxXRlnj@*}u{^hmFWMC@z9`v-R;SBvV=%i1eg9qMVxm>zRp2cx*Ub{?S&x9MmP z?$TTXwdR>5^kmg}IKyYdg_^sL*-R38GJYQ6_Sx{Q=C(4MNkUIn>s5})4Xp=Vk2l#H z_+8y{4|=6+d

!r(j^*hHdfl@UgE7k@9m z^XRo-8zI$P#56w7B%yPXXwG|C40+~@A*_KIlZ5VzJqj@iav2+Ve~j zy2KqaNL613$>7I-o=HOYGeN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g=Wy9M4k-)$D+2crhat$ZJ_|qJ`T?;y&=poU_hAkmwk6U_ zeN7v)@H$&NEv=Lzzce0{WEMY5;1lnU z7>c6T;`UPsqtv`WXRJ;$R3))77B(oci3Jmz!lcyI#iBj`@&07``^Tj6HfFc6d@KU_ z<|dKcC2&OGsKB-`K?IG*A}bR&D|G^6Ra{<~$SAcel*C~MO74?&751Lyd5rbM1=W6Q} zRcTpEtZglY<4_5~xP}|ay5?pmXbItr1{0)G2o2TN0w}fIN@D&2=z}HmA6IJm^??6+ zNU25tV|5JT5mM{w>Qqe?J#8Ang^4O4iH&t3EwL`jPD;!_8jgaZK?{80zhZI0#2ZY3 z1EP3ww^4BfzfsoNQg=LhZ6cMZPYmE+Q{reWu_4hcdKknOJSrLz12H&^e+h&C1x|}4 zx&uEYjFyBEOY|_4y${iTNiJ^Ou^VhLSjna@+_3)-g#l z5QX_CRSm9;p$dGbcfbXyTHMnvY)2`gU@FGt4@IX-*n*w5@r9VL#V6E;8=Xw?kX;x# zlQm@>nwW9yTU;e9HR$}e|NXh`HP)dEzqqr?IbSyfWKzoZzAz|Y^j0% zR}kExETgmBe*BJ~Bzi<;%zqwHyHPlXzr#2II6_BI8pU5)9s)iJxYz%yOMK^e{<449 zqR`Lt&*2N4{{$cN}cM)J$$Sl?MVwrB-Jt_F>1Dvx_J1-iG(8cTFG*&F^V({jj ziw8ch?gU$_P=~$3Yp8=a*Id**Br6^7LYRxc;la}D3zy%+nG2K9_^2x~RRV$p|wcI3dFMI8tGA-2}HR|>x`!0^A7tG=s$UwZwA Ieg+=+Uoz}RI{*Lx literal 0 HcmV?d00001 diff --git a/CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe.mdb b/CSharpInDepth/Chapter5/Chapter5/bin/Debug/Chapter5.exe.mdb new file mode 100644 index 0000000000000000000000000000000000000000..22a0c618ab00f7ad0e8c3887ecda1a7fbaede670 GIT binary patch literal 4095 zcmc&%eQ;FO6~Fhrw`V`zZW5BsCq4}q z-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VHa(>e{ z+2g#gUjKK+Ew7)5jaeBpli1l+Ri)Q!DdL}ui53fU&b?~cGk4W36xmsuO&z@JI?zOT1=0k2vlB~pjk+|lkyz;SI)_tQ#qnj^SV0?n=NxuvnH1(B@+@ zE$0lm%Gp&08IOz79=8T*Tk!Y6?Lq9{Ox+iIe3{te;uKe2&8PMGoAO0OLa`!KZQjYJ zck}lN`Ij1sa_+SIFrSX+pM;(>Z^SSS=kOA5&M{s4^@F>Xg>&H58?9zDGAc6nQ`E*# zhlum30(!3C`GVC2BBC4XWHp>bomzCRfX)|OD0r;!E2G}bGs8YyNPjK-5PDdo3i*v1 z>Q)hjv*#}tQcvNdMNbudC5rvMQG8HD|0wzt`d*706~$i=g(v(cCypnI=_kcc7C&3u z>jf7_Op0?rIE&!pV)~@`4D_&&or#}(jcE1#>7f$tY1s+E>4zJ~3jtCqko zup(1=Sz1ERmMklIz61+^(GH2RZ2*3B1b+tsTsx=-^eli|GX){=1GuD88R#HkfZsUK zDgbvu`XT5v0H0d49JCFjBPoJTK!2k|fno?`GJq{hn?ctD*k)9XpuPiO z1JEAO?EuyxjYNRM0IUXD3%UyU7WiGDn}MO=4}*3C!@yqxJq474Z^3{fKpFUM(A~f- z;7@}d2W|!b00ue{7!G~`r~!-s|2NQXpd7q}1e^t50e&v{7$ClA#pf@kf^`E(B^v>u z_{3cfiWIV=pu(;OM#63y{)C+dD(vE*!fw4#$~jSdMLgypNOYMeDIp*0S + + + .. + 2.12 + + + + + + + + + MainWindow + CenterOnParent + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter6/Chapter6.sln b/CSharpInDepth/Chapter6/Chapter6.sln new file mode 100644 index 0000000..0def22a --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter6", "Chapter6\Chapter6.csproj", "{DD91EB8A-A3C0-4A8D-A529-82621728B868}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DD91EB8A-A3C0-4A8D-A529-82621728B868}.Debug|x86.ActiveCfg = Debug|x86 + {DD91EB8A-A3C0-4A8D-A529-82621728B868}.Debug|x86.Build.0 = Debug|x86 + {DD91EB8A-A3C0-4A8D-A529-82621728B868}.Release|x86.ActiveCfg = Release|x86 + {DD91EB8A-A3C0-4A8D-A529-82621728B868}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Chapter6\Chapter6.csproj + EndGlobalSection +EndGlobal diff --git a/CSharpInDepth/Chapter6/Chapter6.userprefs b/CSharpInDepth/Chapter6/Chapter6.userprefs new file mode 100644 index 0000000..2147a1e --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6.userprefs @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharpInDepth/Chapter6/Chapter6/AssemblyInfo.cs b/CSharpInDepth/Chapter6/Chapter6/AssemblyInfo.cs new file mode 100644 index 0000000..2d11b04 --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6/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("Chapter6")] +[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/Chapter6/Chapter6/Chapter6.csproj b/CSharpInDepth/Chapter6/Chapter6/Chapter6.csproj new file mode 100644 index 0000000..4e1f340 --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6/Chapter6.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {DD91EB8A-A3C0-4A8D-A529-82621728B868} + Exe + Chapter6 + Chapter6 + + + 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/Chapter6/Chapter6/Main.cs b/CSharpInDepth/Chapter6/Chapter6/Main.cs new file mode 100644 index 0000000..bbd61f5 --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6/Main.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Chapter6 +{ + class MainClass + { + static readonly string Padding = new string(' ', 30); + + public static void Main (string[] args) + { + + /** + * + * Listings 6.1 and 6.2 Skeleton of th new collection type, with no iterator implementation. + */ + object[] values = {"a", "b", "c", "d", "e"}; + IterationSampleBad badCollection = new IterationSampleBad(values, 3); + try { + foreach(object x in badCollection) + { + Console.WriteLine(x); + } + } catch(NotImplementedException e) { + Console.WriteLine("Listings 6.1 and 6.2 exception: {0}", e); + } + + + /** + * + * Listings 6.3 Nested class implementing the collection's iterator. + */ + IterationSample collection = new IterationSample(values, 3); + foreach(object x in collection) + { + Console.WriteLine(x); + } + + + /** + * + * Listings 6.4 Iterating through the sample collection with C# 2 and yield return. + */ + IterationSampleYield yieldCollection = new IterationSampleYield(values, 3); + foreach(object x in yieldCollection) + { + Console.WriteLine(x); + } + + + /** + * + * Listings 6.5 Showing the sequence of calls between an iterator and its caller. + */ + IEnumerable iterable = CreateEnumerable(); + IEnumerator iterator = iterable.GetEnumerator(); + Console.WriteLine("Starting to iterate"); + } + + static IEnumerable CreateEnumerable () + { + Console.WriteLine ("{0}Start of CreateEnumerable()", Padding); + + for (int i=0; i < 3; i++) { + Console.WriteLine("{0}About to yield {1}", Padding, i); + yield return i; + Console.WriteLine("{0}After yield", Padding); + } + + Console.WriteLine("{0}Yielding final value", Padding); + yield return -1; + + Console.WriteLine("{0}End of CreateEnumerable()", Padding); + } + } + + + public class IterationSampleBad : IEnumerable + { + object[] values; + int startingPoint; + + public IterationSampleBad (object[] values, int startingPoint) + { + this.values = values; + this.startingPoint = startingPoint; + } + + public IEnumerator GetEnumerator() + { + throw new NotImplementedException(); + } + } + + + // The same as the Java style of creating a custom Iterator: + // see http://stackoverflow.com/questions/5849154/can-we-write-our-own-iterator-in-java + // (basically it is the same) + public class IterationSample : IEnumerable + { + object[] values; + int startingPoint; + + public IterationSample (object[] values, int startingPoint) + { + this.values = values; + this.startingPoint = startingPoint; + } + + public IEnumerator GetEnumerator() + { + // If GetEnumerator is called several times, serveral independent iterators should be returned. + return new IterationSampleIterator(this); + } + + // Let's create another nested class to implement the iterator itself. + public class IterationSampleIterator : IEnumerator + { + IterationSample parent; + // Iterators are stateful. We need to store some state somewhere (the position in this example) + int position; + + internal IterationSampleIterator (IterationSample parent) + { + this.parent = parent; + position = -1; + } + + public bool MoveNext () + { + if (position != parent.values.Length) { + position++; + } + return position < parent.values.Length; + } + + public object Current { + get { + if (position == -1 || + position == parent.values.Length) { + throw new InvalidOperationException(); + } + int index = position + parent.startingPoint; + index = index % parent.values.Length; + return parent.values[index]; + } + } + + public void Reset() + { + position = -1; + } + } + } + + + // The C# 2.0 style! Rocks! + public class IterationSampleYield : IEnumerable + { + object[] values; + int startingPoint; + + public IterationSampleYield (object[] values, int startingPoint) + { + this.values = values; + this.startingPoint = startingPoint; + } + + public IEnumerator GetEnumerator () + { + // * Whenever MoveNext is called, it has to execute code from the GetEnumerator + // method until you are ready to provide the next value (in other words, until you + // hit a yield return statement) + // * When the Current property is used, it has to return the last value you yielded. + // * It has to know when you have finished yielding values so that MoveNext can return false. + for (int index = 0; index < values.Length; index++) { + yield return values[(index + startingPoint) % values.Length]; + } + } + } +} diff --git a/CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe b/CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe new file mode 100755 index 0000000000000000000000000000000000000000..e2cf7af5d627734feaecc5ca4fc0f6de80f146de GIT binary patch literal 6144 zcmeHLYit}>6+U-%ch>xu_0LPvPs-DNsYV7+9Zygm+d^75ZUA1$!5sz z&SqxTwPU#$r9xCdT10r23Zkk)2;~P7k%&q`k&r5oS_xG}>W>D3h$@gmBM=h&KsDdF zGwWSH)ba}*^w`2Ju4?3Rze+(I`ZuXo)P zBd>RjpEn(?Y+HF-FKSu6RI*%c%FygeNi$2@;E^${XyuG#b91!YH+^)7Xha0)2j96d zTJ?5^T12BziCQtkfpOFPp2o=0X&96myDCNt-aMepOB!K2Y)pZG zh?Q-UsA=Wge?hluL=AOvInW2{=%j1RxS(fR0Kk*6A)orD5ltp-$IfC>#+C-ciN?|E zS{l(OZKGg8P{uk5CfizvUf0q@`|3!EF7~u)m|tpX%|lQ7BU6Wgb3SXgiHv0r%@~Jt zvqVKFYuSd8%oPe$wdCckP>8N~6TKGk)&=3IKLX9^{IA+3Wvd{H_HIZe=wV>^k8SuG zBR%VE4CNPlI_!`fJ8i@XdcEB&#|P~gk5P5KgH56~hAjlHHthnJ8Z0CeZH{aOC)&V_ z=$=0>E-v2Y@;6jKi=MXN?l7oW(1QKw+qc_5w1w<;7>$LtVyj1QV}@WJe6>xDsTL;H zw(u_Av$3#r(bf=akWN5LFL&8XhZOMLL_9YU&kf8qvXy8oYGGrjZH=C}M$cSRtjROi zxYAssZ?4%h*X)_=g{QqaZ)@EL{t5JU)QuzHhBDAKbe?||y?%ZG^k(!)^!jc2DCj}- zIdm=x(ibPZby5QJFdkHb^DrEQ^9LQ&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;-2F>6u zG;sH_v<2=S%+67Y=J}?9J&i=e&n|uL#QMHF&wxk-Nr(gx1s9rVN#J@C8(TJnTQ(-* zP4P$~9*eifTRo&x4gqyzLQxx3O%WRt4MC0Kk$@U+4QWEeTLWRy5ZDTSOsEoA?2Oc z)t3H+qy@50(W9`PqCT498qij}Q3yH;O$%qbf^+O58UwWBpJgHqa@|rc1F0g;Ima@K z%ym$>E6_A_PiDETl`?(!x)3x1tvs{~#1_l2#gXUn9>l*RYBme*a literal 0 HcmV?d00001 diff --git a/CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe.mdb b/CSharpInDepth/Chapter6/Chapter6/bin/Debug/Chapter6.exe.mdb new file mode 100644 index 0000000000000000000000000000000000000000..03887f8089f72a61824ea16de400413ec7bbe19a GIT binary patch literal 1445 zcmb7EU2IfE6rOYLy>sv0+wB(cf%u@I77`_}M50aFrfN4BD^$c9V>G&M+e>$KySHU` zn=SP(y7dQ*S~O{b3;=G4&@jQG6)^elwT4mH6T$-~GNh zXXflVvuEat{ZE{~X3gBD>qR)q>jn{{*Gf|e2KQrL2Uhx_ zOL{rN?j0-EZuxWWwu7UG)()I&8=LxzMf3j&dP_d< z_jP55+H>0j1h3Qw_nwN})Bg1RxAw07vbgb!ZwJR`Ui|pTk~mX>vtD2K`!qsCeCUp%4bz{Oh|E)D~M>8`8a@+GPI*~;X4{1S}%G#joHMwUFM*=u>@g<-YyP00u42Ux0-E};}@oY_;jl)M?y|!+zh{4< z+SL&nx$cqBJxb$5`YOke6}e7* z9ooU;)`(*)=#E&SQ@mjFMSDVBcbu6HsF&+ZzH<0$r=oqTG;*6%e!n~XhjSKI6fC>& zc>uswk^T5w1sKq;fJXo{job&`3*c{)N5PW-PEdXWo&`{pTma7l2rI8dfEa-HM{WW) z0xtA@;JW}1dKer4HP92_JwO!tQ}7f}3;jL#BoKo>3;q?rJ1DmxsSUs~=rlM9;60R& wgNp#l(eEeH5l8j82vHx_D7Gpmn}u{HA8Bdbo=hsmVXwI@y5(Av$qhpM4RQ}#c>n+a literal 0 HcmV?d00001 -- 2.1.4