0. preface
Retired nurse sisters, what are you doing now? I made a bold decision-to cross-industry and learn. Net development techniques. This idea may seem unbelievable to some people, but for me, doing what I love is more important than anything else. Life is full of unknowns and challenges. As long as you bravely take the first step of trying, it will be a good start.
The first day of study is relatively easy, mainly for preschool introduction to various concepts. I gained an in-depth understanding of what. Net development is and the power that this technology can achieve. The development language I chose to learn is C#, which can use VS (full name Visual Studio), VS Code, Rider and other integrated development environments (IDEs) to write programs. At present, I have a preliminary grasp of the various functions of Visual Studio and the matters needing attention during using it.
Next, I will share in detail my learning content on the next day. These are important foundations for subsequent programming applications, mainly covering annotations, shortcuts, variables, operators, type conversions, escapes, etc. I recorded the parts that I think are critical as follows:
1. annotation
Comments have dual functions of logout and interpretation in programming. The teacher once joked that not writing annotations is like "playing a hooligan", which is enough to reflect the importance of annotations. In C#, there are 3 types of annotations:
-
-
- Single-line comment **: Use "//"and enter the comment content after it. This comment method is only valid for this line. For example:
-
// 这行代码的作用是将Hello World打印到控制台中
Console.WriteLine("Hello,World!");
-
-
- Multi-line comment *:"/ Content to be commented */", which can be used to comment on multiple lines of content. As follows:
-
/*
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
*/
-
-
- Document comments **:"///", which is mostly used to explain classes or methods. The rules are relatively complex, and I am still in the process of further learning and understanding.
-
2. Definition and related knowledge of variables
2.1 Storage variable syntax
When defining a variable, you must first declare the variable type and variable name, and the syntax is "variable type variable name;", and then assign a value to the variable, that is,"variable name = value;". When defining a variable, there are two key points: one is to accurately determine the type of stored data, and the other is to give the variable a meaningful name so that its purpose can be more clearly understood in subsequent use. It should be noted here that "=" is not equal in the mathematical sense, but an assignment operator that assigns the value on the right side of the equal sign to the variable on the left. In addition, there is a shorthand form for declaring and assigning variables: "variable type variable name = value". For example:
// 先声明后赋值
int num;
num = 10;
// 简写形式
int num2 = 20;
2.2 common data types
- 整数类型:
int,只能存储整数 ,无法存储小数。例如:int age = 25; - 小数类型:
double,既能存储整数,也能存储小数,小数点后的有效位数为15 - 16位 ,其取值范围大于int。例如:double price = 19.99; - 金钱类型:
decimal,用于存储金钱小数,数值后需加上“m”(大小写均可),如“decimal money = 5000m”。 - 字符串类型:
string,用于存储多个文本,也可存储空值,值需用英文半角双引号括起来,如string zsName = "张三",字符串可以存储空string s = ""。需要注意的是,字符串和字符不同,字符串由多个字符组成。 - 字符类型:
char,用于存储单个字符,不能存储空值,值需用英文半角单引号括起来,如“char c = 'a'” 。
3. Naming rules for variables
Variable names must have practical meaning. At this stage, variable names should start with 26 English letters and can be followed by letters, numbers or underscores. At the same time, the following points need to be noted:
- 关键字冲突:变量名不能与C#系统中的关键字(显示为蓝色字体)重复。例如,不能将变量命名为“
int”“class”等。 - 大小写敏感:在C#中,大小写是敏感的。“
myVariable”和“MyVariable”是两个不同的变量。 -
-
- Uniqueness within scope **: In the same scope, variables with the same name are not allowed to be defined repeatedly.
-
3.1 naming conventions
- Camel命名规范:变量名首单词首字母小写,其余单词首字母大写,常用于变量命名。例如:
int myAge = 28; - Pascal命名规范:每个单词首字母都大写,常用于类或方法命名。例如:
class MyClass { }
4. assignment operator
“=”为赋值运算符,它的作用是将等号右边的值赋给左边的变量。由“=”连接的表达式为赋值表达式,赋值表达式的值就是等号左边变量的值,如“int number = 10;” 。在这个例子中,“number”的值为10,整个赋值表达式“int number = 10”的值也为10。
5. The role of the "+" sign
- 连接:当“
+”号两边有一边是字符串时,它起连接作用 。例如:string str1 = "Hello"; string str2 = "World"; string result = str1 + " " + str2;,此时“result”的值为“Hello World”。 - 相加:当两边都是数字时,执行加法运算。例如:
int num1 = 5; int num2 = 3; int sum = num1 + num2;,“sum”的值为8。
6. placeholder
使用占位符时,先设置占位符{0}、{1}等“挖坑”,再按顺序传入参数“填坑”。传入参数数量需与占位符数量一致,否则多填无效果,少填会抛异常,且占位符按顺序输出。例如:
string name = "张三";
int age = 20;
Console.WriteLine("姓名:{0},年龄:{1}", name, age);
7. escape character
转义符由“\”和特殊字符组成,具有特殊意义:
- \\:表示一个“\”。例如:
string path = "C:\\Program Files"; - \n:表示换行。例如:
Console.WriteLine("第一行\n第二行"); - \":表示一个英文半角双引号。例如:
string str = "He said, \"Hello!\""; - \t:表示一个tab键的缩进。例如:
Console.WriteLine("Name\tAge"); - \b:表示一个退格键,放在字符串两端无效果。例如:
string str2 = "Hel\blo";,此时“str2”的值为“Helo”。 - \r\n:Windows系统中表示换行,Windows不识别“\n”。例如:
Console.WriteLine("第一行\r\n第二行"); -
- *@symbol **:
- 取消“\”在字符中的转义作用,使其仅表示一个“\”。例如:
string path2 = @"C:\Program Files"; - 将字符串按原格式输出。例如:
string str3 = @"This is a multiline string";
8. arithmetic operator
- “+”:加法。例如:
int a = 3; int b = 2; int c = a + b;,“c”的值为5。 - “-”:减法。例如:
int d = 5; int e = 3; int f = d - e;,“f”的值为2。 - “*”:乘法。例如:
int g = 4; int h = 3; int i = g * h;,“i”的值为12。 - “/”:除法。例如:
int j = 10; int k = 2; int l = j / k;,“l”的值为5。 - “%”:取余。例如:
int m = 10; int n = 3; int o = m % n;,“o”的值为1。
9. type conversion
9.1 implicit type conversion
In C#, it is required that the types of operands participating in the operation on both sides of the equal sign must be consistent. If they are inconsistent, automatic type conversion, that is, implicit type conversion, will occur if the following conditions are met:
- 两种类型兼容:例如,
int和double兼容,因为它们都是数字类型。 - 目标类型大于源类型:例如,
double的取值范围大于int,所以可以将int类型的值自动转换为double类型。
int number = 10;
// int --- double,自动类型转换
double d = number;
9.2 explicit type conversion
- 两者类型相兼容:例如
int和double是兼容的。 - 大的转成小的:当需要将取值范围大的类型(如
double)转换为取值范围小的类型(如int)时,需要进行显式类型转换,也称为强制类型转换。 语法:(待转换的类型)要转换的值
double d = 303.6;
// double---int,强制类型转换
int n = (int)d;
Console.WriteLine(n);
Console.ReadKey();
在显式类型转换中,使用“(int) d”是因为要明确告诉编译器将d(double类型)的值强制转换为int类型。这种转换可能会导致数据精度的丢失,比如上述例子中,303.6转换为int类型后变为303,小数部分被舍去。
10. note
前面关于\n和\r\n的描述我有点疑惑,通过问AI得到下面的回答:
In Windows systems,"\r\n" is usually used to indicate a line break, but in some cases,"\n" can also have the effect of a line break, but strictly speaking, the two are different, so the above statement is not completely accurate, the following is a specific analysis:
- The meaning of "\r\n" and "\n"
-
- *"\r\n"**:"\r" is Carriage Return, which moves the cursor to the beginning of the current line;"\n" is Line Feed, which moves the cursor to the next line. In Windows text files and many programming languages, the traditional newline feed is "\r\n", a habit that has been inherited from the early era of typewriters and teletype machines, when it was necessary to return to the beginning of the line first and then to the next line.
-
- *"\n"**: On Unix and Linux systems,"\n" is commonly used as a newline feed.
-
- Performance in different scenes
- 在 C# 语言中:
Console.WriteLine方法在 Windows 系统下,推荐使用 “\r\n” 作为换行符,这样可以确保在 Windows 的命令行环境或其他基于 Windows 的文本处理场景中,换行效果能正确显示。但实际上,使用 “\n” 也能实现换行,这是因为Console.WriteLine方法在处理输出时会对 “\n” 进行一定的转换处理,使其在 Windows 环境下也能实现换行显示。不过,在处理一些严格遵循 Windows 换行标准的场景,如写入文本文件等,如果使用 “\n” 可能会导致换行显示异常。 -
-
- In other scenarios **: In Windows system batch files (. bat), text editors and other environments, if "\n" is used as a newline feed, it may not be correctly recognized as a newline feed, but "\n" will be treated as a normal character, resulting in the inability to achieve the expected newline feed effect. Only by using "\r\n" can the newline be correctly wrapped.
-
- 在 C# 语言中:
summary
今天,我跟着老师进行了代码练习,边听边操作,确实加深了对这些知识的理解和记忆。但今天学习的内容较多,还需要更多时间去消化吸收。能自己敲出几行代码,心里充满了成就感,不过也有些地方不太明白,比如显式类型转换中“int n = (int) d;”的具体原理和应用场景,还得再深入研究。未来的学习之路还很长,我会继续努力,不断探索C#编程的奥秘。