In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the basic knowledge of C# example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Keywords: all keywords in C # are capitalized.
Comments: consistent with C++ / Java style, / / is a document comment and can only be written in front of classes, methods, and properties. Cannot be used to annotate a single variable.
C # language is also strictly case-sensitive.
Constants in C #:
Constant, as its name implies, is a quantity that will not change.
The numbers (such as 12.85), characters (such as'F') and strings (such as "thank you") that we usually write are all literal constants.
There are some constants that are both important and error-prone, such as the value of pi is 3.1415926.So we often use custom constants. Take a look at the following code:
Static void Main (string [] args) {const double PI = 3.1415926; / / Custom constant PI Console.Write ("the circumference of a circle with radius 4 is:"); / / string constant Console.WriteLine (PI * 4 * 2) / / use the custom constant PI Console.Write ("the area of the circle with radius 4 is:"); / / the string constant Console.WriteLine (PI * 4 * 4); / / use the custom constant PI}
Two keywords need to be explained: the const keyword, which indicates that PI is a constant, and the double keyword, which indicates that the type of PI is "double-precision floating-point" (a numeric type with high precision).
You will find that pi (perimeter, area) is used twice in this code, but because of the custom constant, the literal constant 3.1415926 is written only once. This avoids errors caused by repetitive writing.
One more thing to note: constants are assigned when they are declared and cannot be modified afterwards.
Variables of C#
We learned about constants in the last section, but variables are the most commonly used in programming, and in this section we learn about variables.
Variables can store data, and unlike constants, the data stored by variables can be modified. For example, 18 years old this year, can become 19 years old next year.
The use of variables is divided into three steps: declaration, assignment, and use.
Syntax for declaring variables: data type variable name
Syntax for assigning values to variables: variable name = value
Please read the following code:
Static void Main (string [] args) {int age; / / declare a variable of type int age age = 18; / / assign the variable age a value of 18 Console.WriteLine ("I am this year" + age+ "age"); / / use the variable age age = 19 / / modify the value of the age variable Console.WriteLine ("I am this year" + age+ "year"); / / use the variable age}
Variable declaration and assignment can be done at the same time (together, also known as initialization)
Data type of C #
We know that declaring variables requires writing data types, and what we're going to learn today are the most commonly used types.
The character type char, which stores a character enclosed in''(single quotation marks), for example:
Char sex=' male'; / / store gender
String type string, which stores a string of characters enclosed in "" (double quotation marks), for example:
String address= "Bei Kou, Niu Street, Xuanwu District, Beijing"; / / Storage address
Integer type int, which stores integers, for example:
Int age=23;// storage age
Double-precision floating-point double that stores decimals, such as:
Double salary=7991.63;// stores wages
The above four are the most commonly used data types, and the other commonly used types will be introduced slowly as the course progresses.
It is important to note that certain types of variables can only store this type of data, otherwise, errors may occur.
Type conversion of C #
We learned about four data types in the previous section, and also mentioned that each type of variable can only store this type of data. However, sometimes it is really necessary to put different types of values together, such as this: 3.5 to 8 what to do? There are two situations:
Automatic type conversion: two different types of data operations, low-precision types are automatically converted to higher-precision types.
Taking 3.5-8 as an example, it is obvious that the precision of the number 8 is lower (int), while that of 3.5 is higher (double), so 8 will be automatically converted to binary, that is, to 3.5-8.0 for operation, and the result is 11.5.
Take a look at this example: the precision of double diter2; 2 is obviously lower than that of the variable d, so 2 is automatically converted to 2.0 and assigned to d.
Take another look at this example: the precision of variable I is less than 3.0; the precision of variable I is less than 3.0, but because I has been declared as an int variable, the value of the variable can be changed, but the type of variable can not change, so this command will make an error.
Cast: cannot be automatically converted to the type we need, you can use a cast, such as the example above:
Int I = (int) 3.0
The (int) before the number indicates that the target type of the conversion is int,3.0 will be cast to 3.
It should be noted that the cast from double to int will lose the decimal part, such as (int) 2.8, and we will get 2.
Naming rules for C # identifiers
Variable names, constant names, class names, and method names in the program are all called identifiers. C # has a set of naming rules for identifiers, and if you don't follow the rules, you will make an error. In a nutshell, there are three rules in this set of rules:
① identifiers can only consist of English letters, numbers, and underscores, and cannot contain spaces and other characters.
Incorrect identifier declaration: string $user; / / error in the use of other characters
② variable names cannot start with a number.
Incorrect identifier declaration: double 6hashing / error begins with a number
③ cannot use keywords as variable names.
Incorrect identifier declaration: char static; / / error using the keyword static as the variable name
The arithmetic operator of C # (1)
Computer programs, of course, can not do without "calculation", to calculate must understand the operator. Today, we first learn about addition, subtraction, multiplication and division in arithmetic operators.
Plus: +. The plus sign serves two purposes: when you connect two numbers with a plus sign, the sum of the two numbers is calculated. For example:
Console.WriteLine (92.2); / / output 11.2
On the other hand, when both sides of the plus sign contain a string, the expressions on both sides are concatenated into a new string. For example:
Console.WriteLine (9 + "2.2"); / / output 92.2, because "2.2" is a string, so 9 is also converted to "9", and + acts as a concatenation string
Minus: -. The function of the minus sign is subtraction. For example:
Console.WriteLine (15-23); / / output-8
Multiply: *. The function of the multiplication sign is to find the product of two numbers. For example:
Console.WriteLine (0.8-3); / / output 2.4
Except: /. The function of the division sign is to find the quotient of dividing two numbers. For example:
Console.WriteLine (2 + 0.5); / / output 4.0
However, if the two integers are divided by each other, only the integer part is left, and the decimal part is dropped.
Console.WriteLine (5 + 10); / / output the arithmetic operator of 0C# (2)
In this section we learn about the remainder operator.
The remainder operator in C # is%. The function of the division sign in the previous section is to find the quotient of the division of two numbers, and the function of the remainder operator% is to find the remainder of the division of two numbers. For example:
Console.WriteLine (19pe5); / / find 19 divided by 5 quotient, output 3Console.WriteLine (19% 5); / / find 19 divided by 5 remainder, output 4 (quotient 3 remaining 4)
In programming,% is often used to check whether one number is divisible by another. For example, the following code snippet:
Int number = 29th Console.WriteLine (number%2); / / find the remainder of number divided by 2
If the output is 0, there is no remainder, that is, number is divisible by 2 (even); if the output is 1, there is a remainder, that is, number is not divisible by 2 (odd).
The arithmetic operator of C # (3)
This section learns two special operators + + and--.
+ + is called the self-addition operator. For example, if you are 18 years old this year, and you will be one year older next year, you can write it in code like this:
Int age=18;// is 18 years old this year, age=age+1;// next year, add one year to this year's age
It can also be written like this:
Int age=18;// is 18 years old this year, age++;// next year, add one year to this year's age
Age++; has the same effect as age=age+1;, which is the value of the variable + 1.
It's called the self-subtraction operator. Similarly, you are 18 years old, and after using XX skin care lotion, you will become 17 next year. You can write:
Int age=18;// is 18 years old. Age--;// is equivalent to age=age-1.
In addition, age++; and age--; can also write + + age; or-- age
But please note: if + + is written before or after the variable in the same statement as other operations, and the algorithm is different, please see the following example:
The function of Console.WriteLine (age++) is equivalent to the following two sentences:
Console.WriteLine (age); / / print age=age+1;// first and then add it
Console.WriteLine (+ + age); the function is equivalent to the following two sentences:
Age=age+1;// first add Console.WriteLine (age); / / then print
You see, the operation order is different, so the output will not be the same.
Comparison operator of C #
The operator that compares the size of numbers, or the equality of numbers, is the comparison operator. The comparison operators in C # are:
Note: the "equal" that indicates that two values are equal is made up of two "=".
The result of the comparison operation is the Boolean type (bool). The bool type is mentioned for the first time, which represents logically true (true) and false (not true). True and false are represented by the keywords true and false.
In the above program, if x > = y is not established, it will return false, x 0); / / the conditional expression is true, and the output TrueConsole.WriteLine (! (1 > 0)); / / invert the conditional expression with logic, and output False
Logic and are used to determine whether two bool type expressions are true at the same time. Take a look at the following code:
Int x = 5, y = 2 false / declare two int variables simultaneously and assign Console.WriteLine (x > 3 & & y > 3); / / determine whether x > 3 and y > 3 are true at the same time. Because y > 3 is false, the whole expression is false.
The entire expression is true only if both expressions of & & are true; if any expression is false, the entire expression is false.
Logic may be used to determine whether one of the two bool type expressions is true. Take a look at the following code:
Int x = 5, y = 2 true / declare two int variables at the same time and assign Console.WriteLine (x > 3 | | y > 3); / / determine whether one of x > 3 and y > 3 is true, so the whole expression is true.
As long as | | one of the expressions on both sides is true, the whole expression is true; if both expressions are false, the whole expression is false.
In contrast, the & & operator is true if both sides are true and false if one side is false; | | operator, one side is true and both sides are false.
Logical operator of C # (2)
As a consolidation of the previous section, we are familiar with the application of logical operators through a few examples. The first example is about logic and, for example, if Xiao Zhang wants to get married, the condition offered by his future mother-in-law is: the deposit must exceed 100000 and there must be a house. Since both conditions are "must", they need to be met at the same time, suitable for logic and connection:
The running result is: False
The next example is about logic or, for example, Lily's criteria for choosing a boyfriend, either an "engineer" or an "athlete".
The running result is: True
Assignment operator of C #
Earlier, we have learned an assignment operator "=", this time let's learn about other assignment operators:
Add assignment "+ =": add first and then assign. Take a look at the following example:
This sentence is equivalent to int xdestroy 2; after execution, the value of x is 7
Subtract assignment "- =": subtract first and then assign. Take a look at the following example:
Int xinher510 x-= 2 umbrellas / this sentence is equivalent to xinherxMui 2; after execution, the value of x is 3
Multiplication assignment "* =": multiply first and then assign. Take a look at the following example:
This sentence is equivalent to int xdestroy 2; after execution, the value of x is 10
Except for assignment "/ =": divide first and then assign. Take a look at the following example:
This sentence is equivalent to int xhandle 2; after execution, the value of x is 2
Take remainder assignment "% =": take the remainder first and then assign the value. Take a look at the following example:
Int x% x% = 2 X X% / this sentence is equivalent to x% 2; after execution, the value of x is 1
Unlike other operators, which evaluate from left to right, assignment operators evaluate from right to left.
Operator precedence of C #
We have learned so many operators earlier that if multiple operators are used at the same time in programming, which one will operate first? This is the question of priority. Please refer to the following order for the precedence of the C # operator:
① brackets. When we learn mathematics, we know that we have to calculate the contents in parentheses first. The same is true of the C # language. If there are multiple parentheses, it should be calculated from the inside out. Parentheses have the highest priority.
② unary operator. Some operators have two operands on both sides, such as 2x3, 6% 5, and so on. These are called binary operators. Those that have only one Operand are called unary operators, and they take precedence over binary operators. Unary operators include: + + (self-addition),-- (self-subtraction), and! (logic is not).
③ * (multiplication), / (division),% (remainder).
④ + (plus),-(minus).
⑤ > (greater than), = (greater than or equal to), 10 & & (2% 2 * 2 + 2) > 2 position Console.WriteLine (b)
Analysis: first calculate the parentheses with the highest priority, (15-8) get 7, (2% 2 / 2 / 2 / 2) you need to calculate% and * first, and then +. If the result is 2, the expression becomes:
Bool 20-7 2 > 10 million 2 > 2
And then the highest priority is 7-2, and then subtraction, which becomes:
Bool baccalaureate 6 > 10 class2 > 2
Continue to calculate the two greater than signs to get:
Bool b=false&&false
The end result, of course, is false:
C # uses flow chart to describe program logic
A flowchart is a graphical representation of program steps. The following symbols are included in the flowchart:
In the above figure, the flow line is used to connect two adjacent steps; each program has one and only one start and end.
The following flowchart describes finding the sum of two floating-point numbers, followed by a C # implementation:
Judgment and branching in C #
When you come to a fork in the road, you need to choose the direction. Writing programs will also encounter judgments and branches. Please take a look at the flow chart below to determine whether the mobile account balance is less than 10 yuan, and give a hint if it is insufficient:
This program is in "balance"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.