Figure 2 2 tidying up using directives the using

Info icon This preview shows pages 40–42. Sign up to view the full content.

View Full Document Right Arrow Icon
Figure 2-2. Tidying up using directives The using directives are not the end of our simple program’s encounter with name- spaces. In fact, the very next line of code after these directives is also concerned with namespaces: namespace HelloWorld { ... } While using directives declare which namespaces our code consumes, this namespace keyword tells the compiler what namespace we plan to provide—the types we write in our programs belong to namespaces just like the types in the class library. * Here, Visual Studio has presumed that we’d like to put our code into a namespace named after the project we created. This is a common practice, although you’re free to use whatever * Strictly speaking, you can leave out the namespace, in which case your types will end up in the so-called global namespace . But this is considered a poor practice—you’ll normally want your own code to reap the same benefits that class libraries get from namespaces. 16 | Chapter 2: Basic Programming Techniques
Image of page 40

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
names you like for your namespaces—there’s no requirement that the namespace name match the program name. The C# compiler will even let you put your own code into namespaces whose names begin with System , but you should not do this (at least, not unless you work for Microsoft and are adding types to some future version of .NET’s class library). You’re likely to cause confusion if you break the convention that System namespaces contain .NET Framework types. Notice that the namespace is followed by an open brace ( { ). C# uses braces to denote containment—here, everything inside these braces will be in our HelloWorld name- space. Since namespaces contain types, it should come as no great surprise that the next line in the file defines a type. Specifically, it defines a class . The .NET Framework class library isn’t the only thing that gets to define classes—in fact, if you want to write any code at all in C# you must provide a type to contain that code. Some languages (such as C++) do not impose this constraint, but C# is an object- oriented (OO) programming language. We’ll explore OO concepts in the next chapter, but the main impact on our “Hello, world” example is that every bit of C# code must have a type that it calls home. There are a few different ways to define types in C#, which we’ll get to in the next few chapters, but for the present simple example, the distinctions are not yet relevant. So we use the most common, a class: class Program { ... } Again, note the braces—as with the namespace contents, the class’s contents are de- lineated by a pair of braces. We’re still not quite at the code yet—code lives inside a class, but more specifically, it must live inside a particular method inside a class. A method is a named block of code, which may optionally return some data. The class in our example defines a method called Main , and once again we use a pair of braces to show where it starts and ends: static void Main(string[] args) { ...
Image of page 41
Image of page 42
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern