提示:以下是本篇文章正文内容
abstract | decimal | finally | in | partial | short | typeof | volatile |
可以和类、方法、属性、索引器及事件一起使用,标识一个可以扩展但不能被实体化的、必须被实现的类或方法。 | ~ | 定义一个代码块,在程序控制离开try代码块后执行。参见try和catch。 | ~ | ~ | ~ | 一个操作符,返回传入参数的类型。 | 标识一个可被操作系统、某些硬件设备或并发线程修改的attribute。 |
as | default | fixed | int | out | set | uint | where |
一个转换操作符,如果转换失败,就返回null。 | ~ | 在一个代码块执行时,在固定内存位置为一个变量指派一个指针。 | ~ | 标识一个参数值会受影响的参数,但在传入方法时,该参数无需先初始化。 | ~ | ~ | ~ |
base | delegate | float | interface | namespace | stackalloc | unchecked | while |
用于访问被派生类或构造中的同名成员隐藏的基类成员。 | 指定一个声明为一种委托类型。委托把方法封装为可调用实体,能在委托实体中调用。 | ~ | 将一个声明指定为接口类型,即实现类或构造必须遵循的合同。 | 定义一个逻辑组的类型和命名空间。 | 返回在堆上分配的一个内存块的指针。 | 禁止溢出检查。 | ~ |
bool | continue | for | internal | override | sizeof | ulong | yield |
true/false | ~ | ~ | 一个访问修饰符。 | ~ | 一个操作符,以byte为单位返回一个值类型的长度。 | ~ | ~ |
break | double | foreach | is | private | static | unsafe | class |
终止循环语句 | ~ | 用于遍历一个群集的元素。 | ~ | ~ | ~ | 标注包含指针操作的代码块、方法或类。 | ~ |
byte | do | get | lock | ref | this | void | true |
范围是0~255 | ~ | ~ | ~ | 标识一个参数值可能会受影响的参数。 | ~ | ~ | ~ |
case | else | goto | long | readonly | struct | ushort | extern |
~ | ~ | 一个跳转语句,将程序执行重定向到一个标签语句。 | ~ | 标识一个变量的值在初始化后不可修改。 | 是一种值类型,可以声明常量、字段、方法、property、索引器、操作符、构造器和内嵌类型。 | ~ | 标识一个将在外部(通常不是c#语言)实现的方法。 |
catch | enum | if | new | public | throw | using | false |
定义一个代码块,在特定类型异常抛出时,执行块内代码。 | 表示一个已命名常量群集的值类型。 | ~ | ~ | ~ | 抛出一个异常。 | 当用于命名空间时,using关键字允许访问该命名空间中的类型,而无需指定其全名。也用于定义finalization操作的范围。 | ~ |
char | event | implicit | null | return | try | value | sbyte |
~ | 允许一个类或对象提供通知的成员,他必须是委托类型。 | 一个操作符,定义一个用户定义的转换操作符,通常用来将预定义类型转换为用户定义类型或反向操作,隐式转换操作符必须在转换时使用。 | ~ | ~ | 异常处理代码块的组成部分之一。try代码块包括可能会,抛出异常的代码。参阅catch和finally关键字。 | ~ | ~ |
checked | explicit | const | object | protected | switch | virtual | sealed [18] |
既是操作符又是语句,确保编译器运行时,检查整数类型操作或转换时出现的溢出。 | 一个定义用户自定义转换操作符的操作符,通常用来将内建类型转换为用户定义类型或反向操作,必须再转换时调用显示转换操作符。 | 标识一个可在编译时计算出来的变量值,即一经指派不可修改的值。 | ~ | ~ | ~ | 一个方法修饰符,标识可被覆载的方法。 | 防止类型被派生,防止方法和property被覆载。 |
Hello world!
1.1 创建项目
打开VS->新键项目->找到控制台程序(.NET Framework)->下一步->创建
eg:
1.2 打印Hello world!
🔔如何编译当前程序?
1.C#程序–人能看懂,机器看不懂
2.执行程序的确是机器
3.需要将C#程序编译(翻译)成机器能够读懂的语言(二进制)
4.这样程序就可以被机器执行了
5.Windows:生成->生成解决方案 Ctrl + Shift + B
🔔如何运行当前程序?
1.Windows:运行而不调试(Ctrl + F5/F5)
eg:
🔔注释
1.注释是不会被编译的,更不会被执行
2.注释的作用:
3.解释说明当前的代码是什么含义
3.1、强调
1.在目前学习代码阶段,保证你写的每行代码都要配一行注释
2.解释说明,你这句代码是什么含义
3.1、暂时取消某些代码的执行
快捷键:
注释当前选中行的代码:Ctrl + K + C
取消注释当前选中行的代码:Ctrl + K + U
https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.fieldinfo?view=netframework-4.7.2
//int showYouAge = age++;//结果是18 //意味着 age++ 得到的结果是 age //解析: //第一步:将age的值赋给showYouAge //第二步:age自增 int showYouAge = ++age; //意味着 ++age 得到的结果是 age+1 //解析: //第一步:age自增 //第二步:将age的值赋给showYouAge //总结: //age++;++age; //++符号在前就先自增,后赋值 //++符号在后就先赋值,后自增
练习题:
//整型 //1 2 4 8 //sbyte short int long sbyte englishScore = 100; //sbyte --> int int myScore = englishScore; //int --> long long classScore = myScore; //int --> float float newScore = myScore; //float --> double double newClassScore = newScore;
//强制转换 int damage = 10000; //int --> sbyte sbyte health = (sbyte)damage; //输出结果 Console.WriteLine(health); //16 float mathScore = 90.5f; //float --> int int myAllScore = (int)mathScore; //会把小数点后面的内容全部舍去 Console.WriteLine(myAllScore); //90
//int 和 char之间的类型转换
int num = 11101;
//int --> char
char letter = (char)num;
//a-97
Console.WriteLine(letter);
if(条件表达式){
语句1;
}
if (条件表达式) { 语句1; } else { 语句2; }
if (条件表达式1) { 语句1; } else if (条件表达式2) { 语句2; } else { 语句3; }
while (条件表达式)
{
//循环内容
}
int[] array = { 1,8,4,8,7,45,456,789,78,76};
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(i + ':' + array[i]);
}
for (int i = 0; i < array.Length; i++) { //立个flag,表示判断过程中是否发生了交换 bool IsjiaoHuan= false; for (int j = 0; j < array.Length - i - 1; j++) { if (array[j] > array[j + 1]) { //说明交换了 IsjiaoHuan= true; //交换 int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } if(!IsjiaoHuan) { //说明已经排好序了,后面的轮次就没有必要了 break; } for (int m = 0; m < array.Length; m++) { Console.Write(array[m] + '\t'); } Console.WriteLine(); } Console.WriteLine('排序结束'); for (int m = 0; m < array.Length; m++) { Console.Write(array[m] + '\t'); }
👂int[,] array;
👂int[,] arr= new int[50,3];
👂int[,] arr= new int[,]{{1,0,1},{3,0,5}};
👂int[,] arr= {{1,0,1},{3,0,5}};
👂 arr[3,2]
for (int i = 0; i < heroData.GetLength(0); i++) { for (int j = 0; j < heroData.GetLength(1); j++) { Console.Write(heroData[i,j] + '\t'); } //换行 Console.WriteLine(); }
//foreach性能消耗要大一点,所以能用for的尽量用for
foreach (var item in number)
{
Console.WriteLine(item);
//迭代遍历是只读的,不能写入
//item = 1;
}
//装备类型 enum EquipType { Helmet = 100,//头盔 BodyArmor = 200,//防弹衣 Knapsack,//背包 Knife }
//定义一个枚举类型的变量 EquipType myEquip = EquipType.Knapsack; EquipType yourEquip = EquipType.Knapsack; //判断枚举变量 if(myEquip == EquipType.BodyArmor) { } switch (myEquip) { case EquipType.BodyArmor: break; case EquipType.Helmet: break; case EquipType.Knife: break; //case EquipType. default: break; } //枚举类型和整型之间的转换 //枚举类型可以强制转换为整型 int num = (int)myEquip; Console.WriteLine(num); //整型可以强制转换为枚举类型 myEquip = (EquipType)200; Console.WriteLine(myEquip); //既然枚举可以用整数去表示 Console.WriteLine(myEquip+2);
//学生类型
struct Student
{
public string name;
public char sex;
public int age;
}
//定义一个学生变量
Student xiaoming;
//学生结构内变量赋值
xiaoming.name = 'xiaoming';
xiaoming.age = 16;
xiaoming.sex = 'M';
public Student()
{
}
//自定义构造函数 public Student(string n,char s,int a) { //作用:快速给结构体字段赋初值 //而且必须给每一个字段都赋初值 name = n; sex = s; age = a; }
//有了自定义的构造函数后,如何新建结构体变量
Student xiaogang = new Student('xiaogang','M',18);
eg:
#region 结构体、枚举 //创建英雄装备结构体,包含名称,攻击力加成,法术强度加成,血量加成,装备类型 enum EquipType { AD, AP, HP, Other } //英雄装备 struct HeroEquip { public string name; public float adAddition; public float apAddition; public float hpAddition; public EquipType equipType; public HeroEquip(string name, float adBuff, float apBuff, float hpBuff, EquipType equipType) { //给所有字段赋初值 this.name = name; adAddition = adBuff; apAddition = apBuff; hpAddition = hpBuff; this.equipType = equipType; } } #endregion #region 结构体、枚举练习 //有5个装备保存在结构体数组当中,编程找出血量加成最高者 //对装备数组按照攻击力加成排序并使装备按照攻击力加成升序进行信息打印 HeroEquip wjzr = new HeroEquip( '无尽之刃', 100, 0, 50, EquipType.AD); HeroEquip yxj = new HeroEquip( '饮血剑', 80, 0, 20, EquipType.AD); HeroEquip ktkj = new HeroEquip( '狂徒铠甲', 0, 0, 150, EquipType.AD); HeroEquip dmj = new HeroEquip( '兰德里的折磨', 20, 100, 0, EquipType.AD); //声明结构体数组存储这些装备 HeroEquip[] heroEquips = { wjzr, yxj, ktkj, dmj }; //设置初始血量最大值 float maxHPBuff = heroEquips[0].hpAddition; //设置初始血量最大值的装备名称 string maxHPEquipName = heroEquips[0].name; HeroEquip maxHPEquip = heroEquips[0]; //找血量最大 for (int i = 0; i<heroEquips.Length; i++) { if (maxHPBuff<heroEquips[i].hpAddition) { //更新最大值 maxHPBuff = heroEquips[i].hpAddition; //更新最大值的装备名称 maxHPEquipName = heroEquips[i].name; } //如果声明结构体 if (maxHPEquip.hpAddition<heroEquips[i].hpAddition) { maxHPEquip = heroEquips[i]; } } Console.WriteLine('装备列表中,血量加成最高的装备是\n' + maxHPEquip.name + ',最大值是' + maxHPEquip.hpAddition); Console.Read(); #endregion
[访问修饰符] Class 类名//大驼峰命名法
类名 对象名;
类名 对象名 = new 类名();
Random random = new Random();
第二步,调用随机数对象的Next方法
int num = random.Next(0,array.Length);
[访问修饰符] 返回值类型 方法名(参数列表)
{
//方法体
//实现方法功能
return 结果;//最终别忘了返回方法结果,结果类型需与返回值类型保持一致
}
对象名.方法名(参数列表);//无论有无参数,小括号都要存在
Contains | Contains |
---|---|
Remove | LastIndexOf |
public string Remove(( int startIndex)/ (int startIndex, int count )) | public int LastIndexOf( (string/char) value ) |
CopyTo | PadLeft |
从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。 | 补充左边长度:.PadLeft(2, '0’) |
Format | PadRight |
把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。 | 补充右边长度:.PadRight(2, '0’) |
IndexOf | Replace |
返回指定 Unicode 字符在当前字符串中第一次出现的索引,索引从 0 开始。 | 替换文字:stringObj.replace(“终古”,“中国”); |
Insert | Split |
返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。 | 返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。 |
Join | ToLower |
连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素。 | 把字符串转换为小写并返回。 |
Substring | ToUpper |
string substr = str.Substring(23); | 把字符串转换为大写并返回。 |
Trim | |
移除当前 String 对象中的所有前导空白字符和后置空白字符。 |
using System.Text;
System.Text.StringBuilder strB = new System.Text.StringBuilder();
StringBuilder stringB = new StringBuilder();
//追加字符串
stringBuilder.Append('天气好晴朗');
class Funs { public string Fun(string a, string b) { return 'a+b'; } public int Fun(int a, int b) { return a + b; } } Funs funs = new Funs(); int a = funs.Fun(1, 1); string b = funs.Fun('2', '2'); string c = string.Format('{0},{1}', a, b); Console.WriteLine(c); Console.Read();
class Person { private int age; private string name; public Person(int age) { this.age = age; Console.WriteLine(age); } public Person(string name) : this(18) { this.name = name; Console.WriteLine(name); } public Person(int age, string name) : this('xian') { Console.WriteLine('我是性别,年龄!'); } } static void Main(string[] args) { Person body = new Person(128, 'alll'); Console.Read(); }
public class Person { public string name; public Person(string name) { this.name = name; Console.WriteLine(name); } public void Say() { Console.WriteLine('你在干什么!'); } } public class Person1:Person { public Person1(string name):base(name) { } public new void Say() { Console.WriteLine('弄啥呢!'); } } public class Person2:Person1 { public Person2(string name) : base(name) { } public new void Say() { Console.WriteLine('搞啥呢!'); } } static void Main(string[] args) { Person p = new Person('1'); p.Say(); Person1 p1 = new Person1('2'); p1.Say(); Person2 p2 = new Person2('3'); p2.Say(); PersonFun(p2); Console.ReadLine(); } public static void PersonFun(Person person) { person.Say(); }
public class Person { public string name; public Person() { } public Person(string name) { this.name = name; } public virtual void Say() { Console.WriteLine('我是父类的方法'); } } public class Person1 : Person { public Person1(string name) { } public override void Say() { Console.WriteLine('我是字类的方法'); } } static void Main(string[] args) { Person1 p1 = new Person1('我是字类'); p1.Say(); Person p = new Person1('我是父类'); p.Say(); Console.ReadLine(); }
代码:
//抽象类 public abstract class Person { public void Fun(string name) { Console.WriteLine(name); } public abstract void Say(); } public class Person1 : Person { public override void Say() { Console.WriteLine('asd'); } } static void Main(string[] args) { Person1 a = new Person1(); a.Say(); Console.Read(); }
代码:
public class Person { public virtual void Fun() { Console.WriteLine(1); } } public class Person1 : Person { public sealed override void Fun() { Console.WriteLine(2); } } public class Person2 : Person1 { //这里报错 因为续承的Person1是密封函数 public override void Fun() { base.Fun(); } } static void Main(string[] args) { Person a = new Person1(); a.Fun(); Console.Read(); }
代码
class Person { public static float age=88; public static void Fun() { Console.WriteLine('我是父静态类!'); } static Person() { Console.WriteLine('我是基静态类!'); } } class Per : Person { static Per() { Console.WriteLine('我是子静态类!'); } } static void Main(string[] args) { Per p = new Per(); Console.WriteLine(); Console.ReadLine(); }
代码:
//实例化动态数组 ArrayList score = new ArrayList(); //向动态数组中添加元素 score.Add(90); score.Add(85.5f); score.Add('English:100'); int[] array = { 90,80,70 }; //向动态数组中批量添加元素 score.AddRange(array); //向动态数组中插入元素 score.Insert(2, 'Math:80.5'); //删除动态数组中的元素 score.Remove(85.5f); //删除的是单个约束,如果动态数组中没有该元素,就忽略 score.Remove(90); //根据下标移除动态数组中的元素 score.RemoveAt(0); score.AddRange(new string[] { 'A', 'B', 'C', 'A' }); //批量删除元素 score.RemoveRange(2, 3); //如果数组中没有该下标所对应的元素,则也会出现越界异常 Console.WriteLine(score[1]); //数组元素翻转 score.Reverse(); //一般想要删除某个元素,会先进行判断是否存在 bool containsA = score.Contains('A'); Console.WriteLine('containsA:' + containsA); //判断数组中是否包含某个元素 if(score.Contains('A')) { score.Remove('A'); } Console.WriteLine('Count:' + score.Count); //给一个元素的值,查该值在动态数组中的下标 Console.WriteLine('IndexOf:' + score.IndexOf(790)); score.Clear(); score.AddRange(new float[] { 1.1f,5.7f,4.5f,9.8f,3,2,1}); //从小到大排序 score.Sort(); //清空动态数组 //score.Clear(); Console.WriteLine('-------------'); for (int i = 0; i < score.Count; i++) { Console.WriteLine(score[i]); } Console.WriteLine('-------------'); foreach (var item in score) { Console.WriteLine(item); }
代码:
//初始化范型集合 List<int> list = new List<int>(); //添加元素 list.Add(200); list.Add(250); list.Add(280); //批量添加元素 list.AddRange(new int[] { 1,3,5,6,7,9 }); //移除某个元素 list.Remove(280); //通过下标移除某个元素 list.RemoveAt(0); list.RemoveAt(0); //批量删除 list.RemoveRange(0, 3); int index = list.IndexOf(9); Console.WriteLine(index); Console.WriteLine('-----------'); foreach (var item in list) { Console.WriteLine(item); }
代码:
eg:非范型为例😥
Object<=Stack stack = new Stack(); //进栈 stack.Push('我是第一个进去的'); stack.Push('我是第二个进去的'); stack.Push('我是第三个进去的'); //出栈 Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); Console.Read(); //返回栈顶的元素Peek();
😀😀😀略略略
Queue queue = new Queue(); //进列 queue.Enqueue('我是第一个进去的'); queue.Enqueue('我是第二个进去的'); queue.Enqueue('我是第三个进去的'); //出列 Console.WriteLine(queue.Dequeue()); Console.WriteLine(queue.Dequeue()); Console.WriteLine(queue.Dequeue()); Console.Read();
Dictionary<string, int> dis = new Dictionary<string, int>(); dis.Add('我是第一个进去的', 1); dis.Add('我是第二个进去的', 2); dis.Add('我是第三个进去的', 3); Console.WriteLine(dis.Values); Console.WriteLine(dis.ContainsValue(2)); Console.WriteLine(dis.ContainsValue(3)); Console.Read();
代码:
public class fanxing { } public static void FX<T>(T sex, T age) { T temp = sex; sex = age; age = temp; Console.WriteLine(age); } public static void FX<T>(T sex, T age, T a) { T temp = sex; sex = age; age = temp; Console.WriteLine(age); } static void Main(string[] args) { FX<int>(7,8); FX<float>(7,8); Console.ReadLine(); } - 范型方法的范型重载 eg 代码: ```代码 public static void faning<T>() { } public static void faning<T,F>() { } public static void faning<T,F,U>() { } static void Main(string[] args) { faning<> }
代码eg:
eg:非范型为例😥
//定义委托 delegate void weituo(float momey); class p1 { public void zhao1(float momey) { Console.WriteLine('中介一需要:'+momey+'$'); } public void zhao2(float momey) { Console.WriteLine('中介二需要:'+ momey + '$'); } public void zhao3(float momey) { Console.WriteLine('中介三需要:'+ momey + '$'); } public void zhao4(float momey) { Console.WriteLine('中介四需要:'+momey + '$'); } } internal class Program { static void Main(string[] args) { p1 p = new p1(); weituo we; we = p.zhao1; we += p.zhao2; we += p.zhao3; we += p.zhao4; we(1232143); Console.Read(); } }
代码eg:
eg:😥
class p1 { public void wcs() { Console.WriteLine('我是无参数的中介需要:' + '18456456456456'+ '$'); } public void zhao1(float momey) { Console.WriteLine('中介一需要:'+momey+'$'); } public void zhao2(float momey) { Console.WriteLine('中介二需要:'+ momey + '$'); } public void zhao3(float momey) { Console.WriteLine('中介三需要:'+ momey + '$'); } public void zhao4(float momey) { Console.WriteLine('中介四需要:'+momey + '$'); } } internal class Program { static void Main(string[] args) { p1 p = new p1(); //使用系统委托 //无参数 Action action; action = p.wcs; action(); //参数 Action<float> action1; action1 = p.zhao1; action1 += p.zhao2; action1 += p.zhao3; action1 += p.zhao4; action1(182); Console.Read(); } }
代码eg:
eg:😥
class Hout { } class p1 { public string Func1() { return '有返回值函数无参数:'; } public Hout Func2(string name) { Console.WriteLine('有返回值函数有参数:'+name); return null; } } internal class Program { static void Main(string[] args) { p1 p = new p1(); Func<string> func; func = p.Func1; Console.WriteLine(func()); Func<string,Hout> func1; func1 = p.Func2; Hout a= func1('先生'); Console.Read(); } }
代码eg:
eg:😥
class p1 { public Action<string> action; public string name; public int age; public p1(string name, int age) { this.name = name; this.age = age; } public void isAction(string age) { Console.WriteLine('我是个中介:'+ age); if (action != null) { action(age); Console.WriteLine('我是个中介:' + age); } } } internal class Program { static void Main(string[] args) { p1 p = new p1('先生',18); p.action = delegate (string a) { Console.WriteLine(a); Console.WriteLine('我好蒙啊2'); }; p.action += read; p.isAction('我好蒙啊1'); Console.Read(); } public static void read(string name) { Console.WriteLine(name); Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Black; } }
联系客服