0. Foreword
My fellow nurses who have resigned, what are you doing now? I made a bold decision—change careers and learn .NET development. This idea may seem incredible to some, but to me, doing what I love is the most important thing. Life is full of unknowns and challenges; as long as you bravely take the first step, it's a good start.
The first day of learning was relatively easy, mainly focusing on pre-study concepts. I deeply explored what .NET development is and the powerful functions this technology can achieve. The development language I chose to learn is C#, which can be programmed using integrated development environments (IDEs) such as VS (full name Visual Studio), VS Code, Rider, etc. Currently, I have a preliminary grasp of Visual Studio's functions and precautions during use.
Next, I will share the details of my second day of learning, which are crucial foundations for subsequent programming. It mainly covers comment symbols, shortcuts, variables, operators, type conversions, escape characters, etc. I have recorded the key points as follows:
1. Comment Symbols
Comment symbols in programming serve both as commenting out and explaining. The teacher once joked that not writing comments is like "being a rogue," which shows the importance of comments. In C#, there are 3 types of comment symbols:
- Single-line comment: Use "//", followed by the comment content. This comment is only valid for the current line. For example:
// The role of this line of code is to print Hello World to the console
Console.WriteLine("Hello,World!");
- Multi-line comment: "/* content to comment */", can be used to comment multiple lines. As shown below:
/*
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
*/
- Documentation comment: "///", mostly used to explain classes or methods. Its rules are relatively complex, and I am still learning to understand it further.
2. Variable Definition and Related Knowledge
2.1 Storing Variable Syntax
When defining a variable, first declare the variable type and variable name. The syntax is "variable type variable name;". Then assign a value to the variable: "variable name = value;". There are two key points when defining a variable: first, accurately determine the type of data to be stored; second, give the variable a meaningful name so that its purpose can be understood more clearly in subsequent use. Note that "=" is not the mathematical equals sign but an assignment operator, which assigns the value on the right to the variable on the left. Additionally, there is a shorthand form for declaring and assigning a variable: "variable type variable name = value". For example:
// Declare first, then assign
int num;
num = 10;
// Shorthand form
int num2 = 20;
2.2 Common Data Types
- Integer type:
int, can only store integers, cannot store decimals. For example:int age = 25; - Decimal type:
double, can store both integers and decimals, with an effective decimal precision of 15-16 digits. Its range is larger thanint. For example:double price = 19.99; - Money type:
decimal, used to store monetary decimals. The value must be followed by "m" (case-insensitive), such as "decimal money = 5000m". - String type:
string, used to store multiple texts, can also store empty values. The value must be enclosed in English half-width double quotes, such asstring zsName = "张三". Strings can be empty:string s = "". Note that strings are different from characters; strings are composed of multiple characters. - Character type:
char, used to store a single character, cannot store empty values. The value must be enclosed in English half-width single quotes, such as "char c = 'a'".
3. Variable Naming Rules
Variable names should have a meaningful purpose. At this stage, variable names should start with one of the 26 English letters, followed by letters, numbers, or underscores. Also, note the following:
- Keyword conflict: Variable names must not duplicate C# system keywords (displayed in blue font). For example, you cannot name a variable "
int" or "class". - Case sensitivity: C# is case-sensitive. "
myVariable" and "MyVariable" are two different variables. - Uniqueness within scope: Within the same scope, duplicate variable names are not allowed.
3.1 Naming Conventions
- Camel naming convention: The first word of the variable name starts with a lowercase letter, and subsequent words start with uppercase letters. Often used for variable naming. For example:
int myAge = 28; - Pascal naming convention: The first letter of each word is capitalized. Often used for class or method naming. For example:
class MyClass { }
4. Assignment Operator
"=" is the assignment operator, which assigns the value on the right to the variable on the left. An expression connected by "=" is an assignment expression. The value of the assignment expression is the value of the variable on the left. For example, "int number = 10;". In this example, the value of "number" is 10, and the value of the entire assignment expression "int number = 10" is also 10.
5. The Role of the "+" Operator
- Concatenation: When "
+" has a string on either side, it acts as a concatenation operator. For example:string str1 = "Hello"; string str2 = "World"; string result = str1 + " " + str2;, the value of "result" is "Hello World". - Addition: When both sides are numbers, it performs addition. For example:
int num1 = 5; int num2 = 3; int sum = num1 + num2;, the value of "sum" is 8.
6. Placeholders
When using placeholders, first set placeholders like {0}, {1}, etc. ("dig holes"), then pass parameters in order to "fill the holes." The number of parameters must match the number of placeholders; extra parameters have no effect, while missing parameters will throw an exception. Placeholders are output in order. For example:
string name = "张三";
int age = 20;
Console.WriteLine("Name: {0}, Age: {1}", name, age);
7. Escape Characters
Escape characters consist of "\" followed by special characters and have special meanings:
\\: Represents a single backslash. For example:string path = "C:\\Program Files";\n: Represents a newline. For example:Console.WriteLine("First line\nSecond line");\": Represents an English half-width double quote. For example:string str = "He said, \"Hello!\"";\t: Represents a tab indentation. For example:Console.WriteLine("Name\tAge");\b: Represents a backspace. It has no effect when placed at the beginning or end of a string. For example:string str2 = "Hel\blo";, the value of "str2" is "Helo".\r\n: Represents a newline in Windows systems. Windows does not recognize "\n" alone. For example:Console.WriteLine("First line\r\nSecond line");- @ symbol:
- Cancels the escaping effect of "
\" in a string, making it represent only a single backslash. For example:string path2 = @"C:\Program Files"; - Outputs the string in its original format. For example:
string str3 = @"This is a multiline string";
- Cancels the escaping effect of "
8. Arithmetic Operators
- "+": Addition. For example:
int a = 3; int b = 2; int c = a + b;, the value of "c" is 5. - "-": Subtraction. For example:
int d = 5; int e = 3; int f = d - e;, the value of "f" is 2. - "*": Multiplication. For example:
int g = 4; int h = 3; int i = g * h;, the value of "i" is 12. - "/": Division. For example:
int j = 10; int k = 2; int l = j / k;, the value of "l" is 5. - "%": Modulus (remainder). For example:
int m = 10; int n = 3; int o = m % n;, the value of "o" is 1.
9. Type Conversions
9.1 Implicit Type Conversion
In C#, the types of operands on both sides of the assignment operator must be consistent. If they are not, an automatic type conversion (implicit type conversion) occurs when the following conditions are met:
- Two types are compatible: For example,
intanddoubleare compatible because they are both numeric types. - The target type is larger than the source type: For example, the range of
doubleis larger thanint, so anintvalue can be automatically converted todouble.
int number = 10;
// int --- double, automatic type conversion
double d = number;
9.2 Explicit Type Conversion
- Both types are compatible: For example,
intanddoubleare compatible. - Converting a larger type to a smaller type: When you need to convert a type with a larger range (e.g.,
double) to a type with a smaller range (e.g.,int), an explicit type conversion (also called a cast) is required. Syntax:(type to convert to)value to convert
double d = 303.6;
// double---int, forced type conversion
int n = (int)d;
Console.WriteLine(n);
Console.ReadKey();
In explicit type conversion, using "(int) d" tells the compiler to forcefully convert the value of d (of type double) to int. This conversion may cause loss of data precision. For example, in the above example, 303.6 becomes 303 when converted to int, with the decimal part discarded.
10. Note
Earlier, I was confused about the description of \n and \r\n. After asking AI, I got the following answer:
In Windows systems, "\r\n" is typically used to represent a newline. However, in some cases, "\n" can also achieve a newline effect, but strictly speaking, there is a difference between the two. The above statement is therefore not entirely accurate. Here is a detailed analysis:
- The meaning of "
\r\n" and "\n"- "
\r\n": "\r" is a carriage return (CR), which moves the cursor to the beginning of the current line; "\n" is a line feed (LF), which moves the cursor to the next line. In Windows text files and many programming languages, the traditional newline character is "\r\n". This is a legacy from the era of typewriters and teleprinters, where you first needed to return the carriage to the start of the line and then advance to the next line. - "
\n": In Unix and Linux systems, "\n" is typically used as the newline character.
- "
- Behavior in different scenarios
- In C#: Under Windows,
Console.WriteLinerecommends using "\r\n" as the newline character to ensure correct newline display in the Windows command prompt or other Windows-based text processing scenarios. However, using "\n`` also achieves a newline effect because theConsole.WriteLinemethod performs some transformation on "\nwhen outputting, making it display as a newline in the Windows environment. Nevertheless, in scenarios that strictly adhere to Windows newline standards, such as writing to text files, using "`\nmay cause abnormal newline display. - In other scenarios: In Windows batch files (.bat), text editors, etc., using "
\n" as a newline may not be correctly recognized as a newline; instead, "\n" may be treated as a regular character, failing to achieve the expected newline effect. Only "\r\n" will correctly create a newline.
- In C#: Under Windows,
Summary
Today, I followed the teacher to practice coding, listening and operating simultaneously. This indeed deepened my understanding and memory of these concepts. However, the content covered today is quite extensive and requires more time to digest and absorb. Being able to write a few lines of code myself filled me with a sense of accomplishment, but there are still parts I don't fully understand, such as the specific principle and application scenarios of "int n = (int) d;" in explicit type conversion. I will need to study further. The learning path ahead is long, and I will continue to work hard, exploring the mysteries of C# programming.