The first C # program,

Source: Internet
Author: User
Tags net command net command line

The first C # program,

Enter the following code in the text editor and save it as a. cs file (such as First. cs ). The Main () method is as follows:

 

using System;namespace Wrox{    public class MyFirstClass    {        static void Main()        {            Console.WriteLine("Hello from Wrox.");        }    }}

 

 

Compile and run the program

  Compile the program for source file and sex csung command line compilers (csc.exe:

Csc First. cs

If you use the csc command to compile code on the command line, you should note that the. NET command line tool (including csc) can only be used after some environment variables are set. If no environment variable is set, there are two solutions. You can run the batch processing file % Microsoft Visual Studio 1st \ Common7 \ Tools \ vsvars32.bat from the command prompt window before running csc. % Microsoft Visual Studio 2013% is the installation folder of Visual Studio 2013. You can use the Visual Studio 2nd Command Prompt to replace the common Command Prompt window. Visual Studio 2013 command prompt under the menu "start" | "program" | Microsoft Visual Studio 2013 | Visual Studio Tools sub menu. It is only a command prompt window. When it is opened, vsvars32.bat is automatically run.

Compile the code to generate an executable file first.exe. On the command line or Windows Explorer, run the file as if you were running any executable file. The following result is displayed:

First.exe

 

  

  

Details

  In C #, most statements end with semicolons (;). Statements can be written on multiple lines of code without the need to continue. Use braces ({}) to combine statements into blocks. A single line comment starts with a parallel bar character (//). A multi-line comment starts with a slash and an asterisk (/*) and ends with an asterisk and a slash. C # case sensitive. That is to say, the variables myVar and MyVar are two different variables.

In the preceding code example, the first few lines of code are related to namespaces. namespaces combine classes. The namespace keyword declares the namespace related to the class. All the code in the braces is considered in this namespace. The compiler searches for classes not defined in the current namespace but referenced in the code in the namespace specified by the using statement.

Using System;

Namespace Wrox

{

The reason why the using command is used in the First. cs file is that a library class System. Console will be used later. The using System statement allows you to abbreviated this class as Console. If there is no using, you must completely limit the call to the Console. WriteLine () method, as shown below:

 

System.Console.WriteLine(“Hello from Wrox.”);

 

The standard System namespace contains the most common. NET types. All the work done in C # depends on the. NET base class. In this example, use the Console class in the System namespace to write data to the Console window. C # does not have built-in keywords for input and output, but relies entirely on the. NET class.

Next, declare a class MyFirstClass. However, because the class is in the Wrox namespace, its complete name is Wrox. MyFirstCSharpClass:

Class MyFirstCSharpClass {

All C # code must be included in a class. The class declaration includes the class keyword, followed by the class name and a pair of curly braces. All Code related to the class should be placed in this curly braces.

The following declaration method is Main (). Each C # executable file (such as the console application, Windows application, and Windows Service) must have an entry point -- Main () method (note that M is capitalized ):

Public static void Main (){

This method is called when the program starts. This method either has no return value (void) or returns an integer (int ). Note: The method definition in C # is as follows:

[Modifiers] return type MethodName ([parameters]) {}

The content in the first square brackets indicates an optional keyword. Modifiers is used to specify certain features of a user-defined method, for example, where the method can be called. In this example, there are two modifiers: public and static. The public modifier indicates that the method can be accessed anywhere, so it can be called outside the class. The modifier static indicates that the method cannot be executed on the instance of the class, so it is not necessary to instantiate the class before calling. This is very important because we create an executable file instead of a class library.

In this example, only System. the WriteLine () method of the Console class writes a line of text to the Console window. WriteLine () is a static method, and you do not need to instantiate the Console object before calling it.

Console. ReadLine () reads user input. Adding this line of code will let the application wait for the user to press the Enter key, and then exit the application.

Then call return to exit the method (because this is the Main method, the program is also exited ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.