Study time: September 17, 2020
- A lot of information about data types can be viewed online
- public: shared, all classes can be accessed
- private: private current class can be accessed
- protected: The protected current class and the subclasses that inherit it can access
- internal: The internal is limited to access within this project, others cannot be accessed
- protected internal: internal protected access can only be accessed within the project or subclasses can not be accessed by other classes
- Access level constraints
- The parent class and subclass access modifiers should be consistent
- The access modifier of the method should be consistent with the access modifier of the method parameter
- Note: The access level of the class is implicitly private by default, and public must be added to allow external access
Shortcut keys :
- Comment: Ctrl + K + C
- Uncomment: Ctrl + K + U
- Organize the code: Ctrl + K + D/F
- Code hint: Ctrl + J
- Method parameter prompt: Ctrl + shift + space
- Insert code block: Tab (press twice) [For example: enter and exit for and press twice Tab for loop code block will be generated]
- Delete the current line: Ctrl + shift + L
- Ctrl + M + M: hide or expand the current nested folding state (M is twice)
- Focus line rectangle: Alt + left mouse button
- About classes, attributes, methods
- You can view the ClassYin01 class, which has recorded properties and methods
class ClassYin01
{
/* 属性遵循大驼峰命名法
* 如果属性中具有 get 关键字,说明可以获取该属性的值
* 如果属性中具有 set 关键字,说明可以向该属性设置值
*
* 类使用关键字 new 实例化对象
* 一个类可以实例化多个对象
* 对象可以使用类定义的属性和方法
*
* 静态的属性和方法通过 static 关键字修饰
* 静态的属性可以通过类型直接获取,非静态则必须通过实例化的对象获取
*
* 静态方法中只能使用静态字段
*
* 调用静态属性和方法,需要使用类来点属性或方法【ClassYin01.IdCard】
*
* 静态类通过 static 关键字修饰
* 一般情况下类型不需要静态修饰,只有当类型中存在扩展方法时需要使用静态类
*/
// 下面三个属性:Name、Age、Height
public string Name {
get; set; }
public int Age {
get; set; }
public int Height {
get; set; }
// 下面一个静态属性:IdCard
public static string IdCard {
get; set;}
// 下面两个方法:Eat、Run
/// <summary>
/// 人在吃的方法
/// </summary>
public void Eat()
{
MessageBox.Show(Name + "在吃");
}
/// <summary>
/// 人在跑的方法
/// </summary>
public void Run()
{
MessageBox.Show(Name + "在跑");
}
// 下面静态方法:RedID
/// <summary>
/// 静态方法读取身份证号
/// </summary>
public static void RedID()
{
MessageBox.Show("身份证号为:" + IdCard);
}
}
- ClassYin01 class of property assignment and call its methods :
// 类的实例化
ClassYin01 XiaoYin01 = new ClassYin01();
XiaoYin01.Name = "小印01";
XiaoYin01.Age = 22;
XiaoYin01.Height = 173;
ClassYin01.IdCard = "421083199800000000";
XiaoYin01.Eat();
XiaoYin01.Run();
ClassYin01.RedID();
ClassYin01 XiaoYin02 = new ClassYin01()
{
Name = "小印02",
Age = 23,
Height = 175
};
ClassYin01.IdCard = "421083199800000000";
XiaoYin02.Eat();
XiaoYin02.Run();
ClassYin01.RedID();
//Integer type (int) to string type (String)
int i = 0;
String st = i.ToString();
// Pop-up prompt [similar to ShowMessage() in Delphi]
MessageBox.Show(st);
// Show Form1 form
Form1 yf1 = new Form1();
yf1.ShowDialog();
- The difference between switch and if statement [For example, i is a person who needs to find a private room, and the condition to be met is the corresponding private room]
- if is equivalent to going to the restaurant and asking the waiter, if there is a private room for XX, then go in for dinner if there is no else
- Switching is equivalent to going to a restaurant for dinner, the waiter, did you make an appointment for a private room, or not, only in the lobby (default)
switch statement :
switch (i)
{
case 1:
MessageBox.Show("这是1号房!");
break;
case 2:
MessageBox.Show("这是2号房!");
break;
case 3:
MessageBox.Show("这是3号房!");
break;
default:
MessageBox.Show("没有包房了,只能在大厅吃饭");
break;
}
Array :
// 声明没有元素的数组【5是数组长度,声明长度后不可改变】
int[] ints1 = new int[5];
// 声明初始化有元素的数组
int[] ints2 = new int[] {
1, 2, 3, 4 };
// 在声明初始化有元素的数组时,也可以指定数组大小
string[] strs1 = new string[5] {
"ab", "bc", "c", "cd", "de" };
// 使用 for 循环给没有元素的数组赋值
for (int ii = 0; ii < ints1.Length; ii++)
{
ints1[ii] = ii;
}
/* (int)5.21 表示使用显示强制转换 C# 整形是 int32,因此使用此方法转成 int32位,不遵循四舍五入,只截取整数部分
* int.Parse(""); 只支持将 string 类型转成 int,Parse 就是把 String 类型转换成 int,char,double...等,
* 也就是 类型.Parse(string) 括号里面一定是string类型的
*
* Convert.ToInt32(4.5); 不完全遵循四舍五入,如果括号里面的需要四舍五入的数字是中间数(5),那么就返回二者中间的偶数
*/
Convert.ToInt32(4.5);// 输出为4
Convert.ToInt32(4.6);// 输出为5
Convert.ToInt32(5.4);// 输出为5
Convert.ToInt32(5.5);// 输出为6
Method : [directly call the method written in the Form]
// 调用下面的 NumAdd 方法
MessageBox.Show(NumAdd(10, 5));
/* 调用下面带 out 的方法
* 其中传递的是参数的地址,当方法中修改后,传进去的原参数因为和里面已经修改的参数地址相同,所以原参数也会同时跟着修改
* 如果被调用的方法没有给输出参数赋值,就会出现变异错误。
* out 可以调用一次方法获得多个返回值【C#7.0 要实现调用一次方法返回多个参数,建议使用元组】
* out 传进去的值,在方法里面的【必须】给传进去的重新赋值
*/
string stxt = "我是默认的赋值";// 提示不需要对 stxt 赋值
MessageBox.Show(Text1(out stxt));
// 所谓的返回多个参数就是利用 out 传进去的是参数地址,传进去进行修改地址里面的内容,进而实现多参数修改,这就是上面所说的返回多参数
MessageBox.Show(Text2(stxt, out string stxt2));// 输出:我是在里面被改变了
MessageBox.Show(stxt2);// 输出:我是在里面被改变了的stxt2
/* 调用下面带 ref 的方法
* 其跟 out 一样都是引用传递,但是 ref 传进去的的参数必须先赋值,在方法里面可以给传进去的参数赋值,也可以不给它赋值
*/
string stxt3 = "ref 传过去的参数必须先赋值";
MessageBox.Show(Text3(stxt, ref stxt3));// 输出:我是在里面被改变了
- Method written in Form
/* 这些三条斜杠的是为函数写注释 */
/// <summary>
/// 两个数加法加法方法
/// </summary>
/// <param name="snum1">数字1</param>
/// <param name="snum2">数字2</param>
/// <returns>返回两个数的和</returns>
public string NumAdd(int snum1, int snum2)
{
int ssum;
ssum = snum1 + snum2;
return "和为:" + ssum;
}
/// <summary>
/// out单个返回值【传进来的是参数地址进行修改里面的内容】
/// </summary>
/// <param name="stxt">传进来的参数</param>
/// <returns>返回的是方法里面的stxt</returns>
public string Text1(out string stxt)
{
stxt = "我是在里面被改变了";
return stxt;
}
/// <summary>
/// 所谓的多参数返回 就是传进来多个参数地址,最终返回其中一个,但是传进来的参数地址里面的内容都被修改了,所以在调用的时候直接可以得到被修改后的的参数
/// </summary>
/// <param name="stxt">传进来的stxt参数,返回的也是这个参数的修改值</param>
/// <param name="stxt2">传进来stxt2参数地址</param>
/// <returns>返回的是方法里面的stxt,但是传进来的stxt2地址里面的内容也被修改了</returns>
public string Text2(string stxt, out string stxt2)
{
stxt = "我是在里面被改变了";
stxt2 = "我是在里面被改变了的stxt2";
return stxt;
}
/// <summary>
/// 带有 ref 的方法,在方法里面不修改参数的情况
/// </summary>
/// <param name="stxt">传进来的stxt参数</param>
/// <param name="stxt3">传进来的stxt3参数地址</param>
/// <returns>返回的是方法里面的stxt</returns>
public string Text3(string stxt, ref string stxt3)
{
stxt = "我是在里面被改变了";
return stxt;
}
Take a few notes so you can read them later.