C # variable 1

Source: Internet
Author: User
Tags first string

| Copyright Notice: This article for Bo Master original article, without Bo Master permission not reproduced.

1. Variable: represents this memory (RAM, saves the data that is running the program, the data in the power-off RAM will be lost ), we can save /fetch the data by the name of the variable ,

Therefore, we do not need to directly remember the data stored in the memory of the digital encoded address, just remember the name of the variable line, so the definition of the variable name from the above must be meaningful to

Remember for yourself.

2. Three ways of declaring a variable
(1), declare first, then assign value;

1 char mysex; 2 mysex = ' male ';

(2), the first variable, directly assigned value;

1 char mysex = ' male ';

(3), declare more than one variable of the same type at a time, there should be a comma separating the tomorrow morning of the same type, and finally; End of the number.

1 int twonumber = 2, Threenumber = 3;

Attention:
C # variables must be named with a "letter", an underscore _, and an AI symbol @;
". You can follow any number of letters, underscores, numbers in the back.
". The variable name cannot be the same as the name of the keyword in the C # namespace
》. C # is case sensitive
". The same variable name (in most cases) does not allow duplicate definitions

3. The principle of the use of variables: must first declare the variable, and then to assign the value, and finally in use. (Each step in the middle is necessary, otherwise it will go wrong), in a method

, in general, a variable can only be defined once, but it may be assigned multiple times, and after re-assignment, the new variable value will replace the old variable value (that is, the old variable value will be changed by the new

The measure value is covered)


  4. The "+" number operator: the "+" operator has two meanings in C #: A+b
(1), addition operations in mathematical operations: perform addition operations when a and B are numbers
(2), link meaning, link two strings: Performs a JOIN operation when there is only one character in a, b
as follows:  

1Console.WriteLine ("My name"+myname+", Today"+myage+"years old."+", each month's salary is"+mysalary+"money for one months. ");2Console.Write ("The addition operation result of the "+" number is:");3Console.WriteLine (onenumber + Twonumber + threenumber);

5. Use of placeholders (and "+" connections have similar effects)
The first string/variable in Consloe.wrinteline () can be represented by a placeholder; {0},{1},{2},.... The first one, the second one, the third one, ...

The pit bit of the placeholder is used as follows: The use of placeholders has strict rules, 0 go to the first variable, 1 go to the second variable, ..., n takes the first n+1 variable, where the variable can be more than a placeholder

The number of characters but must not be less than the number of placeholders

1 Console.WriteLine (" I call {0}, today {1} years old, each month's salary is {2} money one months.")  ", MyName, MyAge, mysalary); 2 Console.WriteLine (" I call "+myname+", today "+myage+ " years old. "+", the monthly salary for "+ mysalary+" money one months. ");

6. Code Explanation:

1 ///2 ///request to write a program that highlights the "+" operator3 ///change of the embodiment variable4 ///5 usingSystem;6 usingSystem.Collections.Generic;7 usingSystem.Linq;8 usingSystem.Text;9 usingSystem.Threading.Tasks;Ten  One namespaceTest3 A { -     class Program -     { the         Static voidMain (string[] args) -         { -             intMyAge = +; -             intOnenumber =1; +             intTwonumber =2, Threenumber =3; -             stringMyName ="John Doe"; +             //char mysex = ' male '; A             //mysex = ' male '; at             decimalMysalary =8000.23m; -  - //The decimal variable is a variant of C # specific that is specifically designed for monetary and financial calculations, with a precision representation of the number of bits 28~29 - //but his range is much smaller than the double type, but the double type is less accurate than the decimal type - //*: The decimal variable must be followed by M or m; tell the compiler that the variable is a decimal variable instead of a double variable -  inConsole.WriteLine ("The result of the join operation for the "+" number is:"); -Console.WriteLine ("My name"+ MyName +", Today"+ MyAge +"years old."+", each month's salary is"+ Mysalary +"money for one months. "); to  +             //use of placeholders -             //the first string/variable in Consloe.wrinteline () can be represented by a placeholder; the             //{0},{1},{2},.... The first one, the second one, the third one, ... Placeholder for the pit bit, use the following: *             //The use of placeholders has strict rules, 0 go to the first variable, 1 go to the second variable, ..., n take the n+1 variable; $             //where variables can be more than the number of placeholders but must not be less than the number of placeholdersPanax Notoginseng              -Console.WriteLine ("My name is {0}, today {1} years old, each month's salary is {2} money one months. ", MyName, MyAge, mysalary); theMysalary =12000; +Console.WriteLine ("My name"+myname+", Today"+myage+"years old."+", each month's salary is"+ Mysalary +"money for one months. "); A  theConsole.Write ("The addition operation result of the "+" number is:"); +Console.WriteLine (Onenumber + Twonumber +threenumber); -  $             //In the Console.WriteLine () statement, if there is a comma in the statement, the content after the comma is used to replace the placeholder preceding the comma, if the comma $             //There is no placeholder, then the content after the comma is not output, (Console.WriteLine () When there are multiple parameters, the output is the first -             //parameter, starting with the second argument, is used to replace the value of the placeholder in the first argument, so start with the second argument and do not output -             //, it is necessary to see that there are no related placeholders in the first parameter) as follows: theConsole.WriteLine ("My name"+ MyName,", I this year"+ MyAge +"years old."); -Console.WriteLine ("{0} I call"+ MyName,", I this year"+ MyAge +"years old.");Wuyi  the Console.readkey (); -         } Wu     } -}

C # variable 1

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.