13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Fully functional code <strong>in</strong> snippet form29{}[STAThread]static void Ma<strong>in</strong>(str<strong>in</strong>g[] args){foreach (str<strong>in</strong>g x <strong>in</strong> new str<strong>in</strong>g[] {"Hello", "There"}){Console.WriteL<strong>in</strong>e (x);}}Occasionally extra methods or even types are required, with a bit of code <strong>in</strong> the Ma<strong>in</strong>method to access them. I <strong>in</strong>dicate this by list<strong>in</strong>g the non-Ma<strong>in</strong> code, then an ellipsis(...) and then the Ma<strong>in</strong> code. So the code <strong>in</strong> list<strong>in</strong>g 1.20 would turn <strong>in</strong>to list<strong>in</strong>g 1.21.List<strong>in</strong>g 1.20A code snippet with an extra method, called with<strong>in</strong> the Ma<strong>in</strong> methodstatic str<strong>in</strong>g[] GetGreet<strong>in</strong>gWords(){return new str<strong>in</strong>g[] {"Hello", "There"};}...foreach (str<strong>in</strong>g x <strong>in</strong> GetGreet<strong>in</strong>gWords()){Console.WriteL<strong>in</strong>e (x);}List<strong>in</strong>g 1.21 Expanded form of list<strong>in</strong>g 1.20us<strong>in</strong>g System;public class Snippet{static str<strong>in</strong>g[] GetGreet<strong>in</strong>gWords(){return new str<strong>in</strong>g[] {"Hello", "There"};}}[STAThread]static void Ma<strong>in</strong>(str<strong>in</strong>g[] args){foreach (str<strong>in</strong>g x <strong>in</strong> GetGreet<strong>in</strong>gWords()){Console.WriteL<strong>in</strong>e (x);}}Types declared <strong>in</strong> snippets will be nested with<strong>in</strong> the Snippet class, but that’s veryrarely a problem.Now that we understand what snippets are and what they look like when they’reexpanded, let’s make them a bit more user friendly.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!