The first keyword here static tells c that its not

Info icon This preview shows pages 41–43. Sign up to view the full content.

View Full Document Right Arrow Icon
} The first keyword here, static , tells C# that it’s not necessary to create a Program object ( Program being the class that contains this method, remember) in order to use this method. As you’ll see in the next chapter, a lot of methods require an object, but our simple example doesn’t need one. Namespaces and Types | 17
Image of page 41

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

View Full Document Right Arrow Icon
The next keyword is void . This tells the compiler that our method doesn’t return any data—it just does some work. Many methods return information. For example, the System.Math class’s Cos method calculates the cosine of its input, and since it doesn’t know what you want to do with that result, it provides it as a return value—the output of the method. But the code in this example is rather more proactive than that—it decides to show a message on the screen, so there’s nothing for it to return. On meth- ods that return data, you’d write the type of data being returned here, but since there’s nothing to return in this case, the nothingness is denoted by the void keyword. The next part, Main , is the name of the method. This happens to be a special name— the C# compiler will expect your program to provide one static method called Main , and it’ll run that method when the program is launched. The method name is followed by a parameter list , which declares the input the method requires. This particular example’s parameter list is (string[] args) , which says that it expects just a single input and that the code will refer to it using the name args . It expects this input to be a sequence of text strings (the square brackets indicating that multiple strings may be passed instead of just one). As it happens, this particular pro- gram doesn’t use this input, but it’s a standard feature of the specially named Main method—command-line arguments are passed in here. We’ll return to this later in the chapter when we write a program that makes use of command-line arguments, but for now, our example doesn’t use it. So we’ll move on to the final part of the example— the code inside the Main method that was the one part we added to Visual Studio’s contributions and which represents the only work this program does: Console.WriteLine("Hello, world"); This shows the C# syntax for invoking a method. Here we’re using a method provided by the Console class, which is part of the .NET Framework class library, and it is defined in the System namespace. We could have written the fully qualified name, in which case the code would look like this: System.Console.WriteLine("Hello, world"); But because of the using System; directive earlier, we can use the shorter version—it means the same thing, it’s just more concise. The Console class provides the ability to display text in a console window and to read input typed by the user in an old-fashioned command-line application. In this case, we’re invoking the class’s WriteLine method, passing it the text "Hello, world" . The WriteLine method will write whatever text we provide out to the console window.
Image of page 42
Image of page 43
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