C # basic learning notes -- C # variables,

Source: Internet
Author: User

C # basic learning notes -- C # variables,

Variables can be used in three steps: Declaration, value assignment, and usage.

Syntax for declaring variables: data type variable name;

Syntax for assigning values to variables: variable name = value;

1 namespace Test 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 int age; // declare the variable, type: int, variable name: age 8 age = 18; // assign the variable age to 18 9 Console. writeLine ("My years" + age = "years old"); // print age10 age = 19; // modify the variable age value 11 Console. writeLine ("My next year" + age + "years old"); // print age12} 13} 14}

The running result is:

The declaration and value assignment of variables can be completed at the same time. The first two lines in the Main () method of the code above can be rewritten:

Int age = 18; // declare and assign values

Exercise questions:

1 static void Main (string [] args) 2 {3 int age; // age 4 age = 18; 5 age = 20; 6 age = 20 + 1; 7 Console. writeLine ("I am {0} years old", age); 8 age = age-2; 9}

This code prints: I am 21 years old.

The above is taken from MOOC course C # getting started with development

 

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.