Writing Your First Program


Writing Your First Program

The Program.cs file defines a class called Program that contains a method called Main. All methods must be defined inside a class. The Main method is special—it designates the program's entry point. It must be a static method. (Methods are discussed in Chapter 3, “Writing Methods and Applying Scope.” Static methods are discussed in Chapter 7, “Creating and Managing Classes and Objects.” The Main method is discussed in Chapter 11, “Understanding Parameter Arrays.”)

IMPORTANT
C# 2005 2005 is a case-sensitive language. You must spell Main with a capital M.

In the following exercises, you'll write the code to display the message Hello World in the console; you'll build and run your Hello World console application; you'll learn how namespaces are used to partition code elements.

Write the code using IntelliSense technology
  1. In the Code and Text Editor window displaying the Program.cs file, place the cursor in the Main method after the opening brace, and type Console. As you type the letter C at the start of the word Console an IntelliSense list appears. This list contains all of the valid C# 2005 2005 keywords and data types that are valid in this context. You can either continue typing, or scroll through the list and double-click the Console item with the mouse. Alternatively, after you have typed Con, the Intellisense list will automatically home in on the Console item and you can press the Tab, Enter, or Spacebar key to select it.

    Main should look like this:

    static void Main(string[] args) 
    { 
        Console 
    }
    NOTE
    Console is a built-in class that contains the methods for displaying messages on the screen and getting input from the keyboard.
  2. Type a period immediately after Console. Another Intellisense list appears displaying the methods, properties, and fields of the Console class.

  3. Scroll down through the list until WriteLine is selected, and then press Enter. Alter-natively, you can continue typing until WriteLine is selected and then press Enter.

    The IntelliSense list closes, and the WriteLine method is added to the source file. Main should now look like this:

    static void Main(string[] args) 
    { 
        Console.WriteLine 
    }
  4. Type an open parenthesis. Another IntelliSense tip appears.

    This tip displays the parameters of the WriteLine method. In fact, WriteLine is an overloaded method, meaning that Console contains more than one method named Write Line. Each version of the WriteLine method can be used to output different types of data. (Overloaded methods are discussed in Chapter 3.) Main should now look like this:

    static void Main(string[] args) 
    { 
        Console.WriteLine( 
    }

    You can click the tip's up and down arrows to scroll through the overloaded versions of WriteLine.

  5. Type a close parenthesis, followed by a semicolon.

    Main should now look like this:

    static void Main(string[] args) 
    { 
        Console.WriteLine(); 
    }
  6. Type the string "Hello World" between the left and right parentheses.

    Main should now look like this:

    static void Main(string[] args) 
    { 
        Console.WriteLine("Hello World"); 
    }
    TIP
    Get into the habit of typing matched character pairs, such as ( and ) and { and }, before filling in their contents. It's easy to forget the closing character if you wait until after you've entered the contents.
NOTE
You will frequently see lines of code containing two forward slashes followed by ordinary text. These are comments. They are ignored by the compiler, but are very useful for developers because they help document what a program is actually doing. For example:

Console.ReadLine(); // Wait for the user to press the Enter key

All text from the two slashes to the end of the line will be skipped by the compiler. You can also add multi-line comments starting with /*. The compiler will skip everything until it finds a */ sequence, which could be many lines lower down. You are actively encouraged to document your code with as many comments as necessary.
Build and run the console application
  1. On the Build menu, click Build Solution. This action causes the C# 2005 2005 code to be compiled, resulting in a program that you can run. The Output windows appears below the Code and Text Editor window.

    TIP
    If the Output window does not appear, click the View menu, and then click Output to display it.

    In the Output window, messages similar to the following show how the program is being compiled and display the details of any errors that have occurred. In this case there should be no errors or warnings, and the program should build successfully:

    ------ Build started: Project: TextHello, Configuration: Debug Any CPU ---- 
    Csc.exe /config /nowarn:"1701;1702" /errorreport: prompt /warn:4 ...
    
    
    Compile complete –- 0 errors, 0 warnings
    TextHello -> C:/Documents and Settings/John/My Documents/Microsoft Press/... 
    ============ Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ========
    NOTE
    An asterisk after the file name in the tab above the Code and Text Editor window indicates that the file has been changed since it was last saved. There is no need to manually save the file before building because the Build Solution command automatically saves the file.
  2. On the Debug menu, click Start Without Debugging. A Command window opens and the program runs. The message Hello World appears, and then the program waits for the user to press any key, as shown in the following graphic:

    Graphic
  3. Ensure that the Command window displaying the program has the focus, and then press Enter. The Command window closes and you return to the Visual Studio 2005 programming environment.

    NOTE
    If you run the program using Start Debugging on the Debug menu, the application runs but the Command window closes immediately without waiting for you to press a key.
  4. In the Solution Explorer, click the TextHello project (not the solution), and then click Show All Files button. Entries named bin and obj appear above the C# 2005 2005 source filenames. These entries correspond directly to folders named bin and obj in the project folder (/My Documents/Visual CSharp Step by Step/Chapter 1/TextHello/TextHello). These folders are created when you build your application, and they contain the executable version of the program and some other files.

  5. In the Solution Explorer, click the + to the left of the bin entry. Another folder named Debug appears.

  6. In the Solution Explorer, click the + to the left of the Debug entry. Three entries named TextHello.exe, TextHello.pdb, and TextHello.vshost.exe appear. The file TextHello.exe is the compiled program, and it is this file that runs when you click Start Without Debugging in the Debug menu. The other two files contain information that is used by Visual Studio 2005 if you run your program in Debug mode (when you click Start Debugging in the Debug menu).

    Graphic

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值