Hello World in C

for RuBoard

Hello World in C#

Whenever learning a new programming language, a good first step is to write and run a simple program that will display a single line of text. Such a program demonstrates the basic structure of the language, including output.

Here is "Hello, World" in C#. (See the Hello directory for this chapter.)

 // Hello.cs  using System;  class Hello  {      public static int Main(string[] args)      {          Console.WriteLine("Hello, World");          return 0;      }  } 

Compiling and Running (Command Line)

You can learn how to use the Microsoft Visual Studio.NET IDE (integrated development environment) in Appendix A. You can also use the command-line tools of the .NET Framework SDK. Be sure to get the environment variables set up properly, as described in the sidebar. To compile this program at the command line, enter the command

 >csc Hello.cs 

An executable file Hello.exe will be generated. To execute your program, type at the command line:

 >Hello 

The program will now execute, and you should see displayed the greeting:

 Hello, World 

Setting Environment Variables

In order to run command-line tools such as the C# compiler using a simple name such as csc rather than a complete path , we must set certain environment variables. To do so we can use a batch file, corvars.bat , which can be found in the bin directory of the Framework SDK.

I experienced different behavior in different beta versions of the .NET Framework SDK. In one version the environment variables were set up automatically as part of the install, and in another version I had to use the corvars.bat file.

If you have Visual Studio.NET installed, you can ensure that the environment variables are set up by starting your command prompt session from Start Programs Microsoft Visual Studio.NET 7.0 Microsoft Visual Studio Tools Microsoft Visual Studio.NET Command Prompt.

Program Structure

 // Hello.cs  class Hello   {  ...  }  

Every C# program has at least one class . A class is the foundation of C#'s support of object-oriented programming. A class encapsulates data (represented by variables ) and behavior (represented by methods ). All of the code defining the class (its variables and methods) will be contained between the curly braces. We will discuss classes in detail later.

Note the comment at the beginning of the program. A line beginning with a double slash is present only for documentation purposes and is ignored by the compiler. C# files have the extension . cs .

  // Hello.cs  ... 

An alternate form of comment is to use an opening /* and a closing */.

 /* This is a comment     that may be continued over     several lines */ 

There is a distinguished class, which has a method whose name must be Main . The method should be public and static . An int exit code can be returned to the operating system. Note that in C# the file name need not be the same as the name of the class containing the Main method.

 // Hello.cs  using System;  class Hello  {  public static int Main(string[] args)  {          ...  return 0;  }  } 

Use void if you do not return an exit code.

 public static void Main(string[] args) 

Command-line arguments are passed as an array of strings. The runtime will call this Main method ”it is the entry point for the program. All the code of the Main method will be between the curly braces.

 // Hello.cs  using System;  class Hello  {      public static int Main(string[] args)      {  Console.WriteLine("Hello, World");  return 0;      }  } 

Every method in C# has one or more statements . A statement is terminated by a semicolon. A statement may be spread out over several lines.

The Console class provides support for standard output and standard input. The method WriteLine displays a string, followed by a new line.

Namespaces

Much standard functionality in C# is provided through many classes in the .NET Framework. Related classes are grouped into namespaces . Many useful classes, such as Console , are in the System namespace. The fully qualified name of a class is specified by the namespace followed by a dot followed by a class name.

 System.Console 

A using statement allows a class to be referred to by its class name alone.

 // Hello.cs  using System;  class Hello  {      public static int Main(string[] args)      {  Console  .WriteLine("Hello, World");          return 0;      }  } 
for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net