Self-study C# Part 2 Variables and Expressions

variable

1> To declare a variable, you need to specify the type and variable name: <type> <name>  


type: Indicates what type is used to store the data
name: Indicates the name
instance for storing this type: (Each declaration is a statement, and the statement ends with ; (semicolon))

int age;
int hp;
string name;

Screenshot below:

2> Following the naming convention can make the program structure clearer and easier to read.

Specification: The first word starts with a lowercase letter, and the first letter of each subsequent word is capitalized. Variables are named according to Camel nomenclature (CamelCase). The first letter is lowercase, and the first letter of each subsequent word is uppercase.

Examples are as follows:

Two: characters

1> char and string:

char represents a character, letter, number, @#¥%...&*(), a Chinese character
string is an array of char, a collection of characters

2> Escape characters:

Escape characters are characters with special functions

If we don't want to recognize escape characters in the string, we can add an @ symbol in front of the string (except for double quotes, other escape characters are not recognized)

3> Two examples of the role of the @ character:

1. By default, the definition of a string is placed on one line. If you want to occupy multiple lines
2, use a string to represent the path
"c:\xxx\xx\xxx.doc" and
use @"c:\xxx\xx\xxx .doc" is more readable

4> The role of the Unicode value of the character:

Unicode is a hexadecimal number that indicates which number the character is stored in in memory.
You can also use Unicode to represent an escape character (\u plus the hexadecimal value)
"I\'s kmart!"
"I \u0027s kmart!"

5> Declaration and assignment

Variable declaration  int age;
Variable assignment  age = 54;
Variable declaration and assignment can be placed in one statement <equivalent to initialization>
int age = 54;
We can use one statement to declare multiple variables of the same type
string name1, name2;
in multi-variable declarations, variables can be followed by On = , initialize one of the variables or some or all of the variables

Precautions:

Variables must be initialized before use. How to judge whether the variable is used or not, but when you take something from the variable's box (memory), that is, when you want to use the variable, initialization is to put something into the box before you can take it. . The first time a variable is assigned a value, it is initialized.

6> Expression:

Combining variables with literals and operators is
the classification of expression operators:
unary operators handle one operand
binary operators handle two operands
ternary operators handle three operands

7> Mathematical operators:

Add, Subtract, Multiply, Divide

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297903&siteId=291194637