On the first day, the first C # program learns the basic C # syntax. Master the basic concepts of namespaces, classes, variables, and methods.

Source: Internet
Author: User

I have always believed that,Only continuous practice is the best way to learn.. Therefore, we recommend that you edit, compile, and run the program examples provided by me from now on. In this process, you will gain useful experience in developing C # programs.

1,

Open vs2010, file-New-project-Console Application,NameHello, Enter the code console. writeline ("Hello world! ");

C # is case-sensitive, but we do not need to worry about Case sensitivity when entering the code. You can simply select the input. If you place the cursor on the console, the system will prompt that this is the XXX of the console program. The console is a class, that is, a class. Make a small dot to call the method writeline under the console class. A line of strings is output literally. Note that each sentence of code should be added; number to end

View code

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             Console.WriteLine("hello world!");13         }14     }15 }

To put it bluntly, we can press the green triangle or press the F5 shortcut key. When running, a console window pops up and cannot be seen. This is because the execution of the program ends naturally.

2,
Let's add a code like this.

View code

1 static void Main(string[] args)2         {3             Console.WriteLine("hello world!");4             Console.ReadLine();5         }

Press F5, and the window will stop. Console. Readline (); read the code with the mouse prompt and read the next row of data. The user will remain in the waiting state without entering, so the program will not end.

Press ENTER for a few characters. In this way, after entering a line of code, the program will end.

 

3. Strengthen the code and execute it.

View code

1 static void main (string [] ARGs) 2 {3 // console. writeline ("Hello world! "); 4 // console. readline (); 5 console. writeline ("enter your name and press Enter:"); 6 string name = console. readline (); 7 console. writeline ("Hello:" + name); 8 console. readkey (); 9}

This is a simple human-computer interaction program. 1. Hello, please enter your name. 2. Enter a line of characters on the screen and press Enter. Read by computer. 3. Computer Display: Hello, xxx. 4. Enter another character and the program ends. (PS: console. readkey () is used here. It is read-only)

-------------------- Split line ---------------------------------------------

It doesn't matter if you can't understand the following code :)

The first thing is boring. I don't understand it. I have an impression first. It is mainly to run the program and then understand the code in a row. In fact, there are not many knowledge points, but it takes time to accept them.

 

Namespace:When a project is created, the outermost layer {} is namespace consoleapplication1, which means namespace in Chinese. We can understand the name of this project.

Class: • Let's remember from the first program: Everything must belong to a class. The code system is multiple classes in the namespace, and our code is written in the class • The same as in C and C ++, the source code block is included in a pair of braces "{" and "}". Class program is the class that the system has created for us. Class Members include fields, methods, and attributes. Looking back at the code we wrote, it contains a static void main (string [] ARGs) method. Variables and assignments:The string name above defines a string variable named name. Variable types include string, Int, and bool. These are all in "C # Getting Started classic" and will not be repeated here.

Why is it a variable? It is a variable. Here, the name is entered by us, and we do not know in advance what its value is. Assign a value with a = sign. String name = console. Readline (); indicates that the value returned by the console. Readline (); function is assigned to name.

Method:Our code is written in the first method in the class. The program entry and project are all started from here. Static void main (string [] ARGs)

The method format is: return type method name (parameter), such as string getname (int id) Call Method: In the code above, the console class shows us two basic methods: writeline () and Readline ()

Add notes:  

• The application is not as long as you can understand it by yourself. Whether or not computer teachers or programming books have been warned before, I would like to emphasize again and again: Develop a good habit of coding comments. This is a must for a good programmer One of the standby Conditions. Code annotation does not waste your programming time. It only improves your programming efficiency and makes your program clearer, complete, and friendly. • The comments below seem to be a little complicated, but they all illustrate how to use the comments in C. View code

1 // output a string on the screen 2 console. writeline ("enter your name and press Enter:"); 3 string name = console. readline (); // read the string you entered on the screen and assign it to the name4 console. writeline ("Hello:" + name); 5 console. readkey ();

 

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.