第28课 匿名函数
1.写一个函数传入一个整数,返回一个函数,之后执行这个匿名函数时传入一个整数和之前那个函数传入的数相乘返回结果
static Func<int, int> Fun01(int num)
{
return delegate (int a)
{
return a * num;
};
}
主函数调用:
Func<int,int> test = Fun01(4);
Console.WriteLine(test(5));
第30课 Lambda表达式与闭包
1.有一个函数,会返回一个委托函数,这个委托函数中只有一句打印代码之后执行返回的委托函数时,可以打印出1~10
static Func<Action> LambdaTest()
{
return () =>
{
Action action =null;
Console.WriteLine("打印代码");
for (int i = 1; i < 11; i++)
{
int index = i;
action += () =>
{
Console.Write(index);
};
}
return action;
};
}
主函数调用:
Func<Action> test = LambdaTest();
test()();
第32课 List排序
1.写一个怪物类,创建10个怪物将其添加到List中,对List列表进行排序,根据用户输入数字进行排序:1、攻击排序 2、防御排序 3、血量排序 4、反转
创建一个怪物类:
class Monister
{
public int att;
public int def;
public int hp;
public Monister(int att,int def,int hp)
{
this.att = att;
this.def = def;
this.hp = hp;
}
public void ShowInfo()
{
Console.WriteLine("怪物的攻击力为:{0},防御力为:{1},血量为:{2}",att,def,hp);
}
}
主函数进行调用,这里只演示攻击排序:
List<Monister> monisters = new List<Monister>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
monisters.Add(new Monister(r.Next(1, 100), r.Next(1, 100), r.Next(1, 100)));
}
monisters.Sort((a,b)=>
{
//此处是按照攻击力排序,其他排序只需更改此处即可,为了节省时间。此处省略
return a.att > b.att ? -1 : 1;
});
foreach (var item in monisters)
{
item.ShowInfo();
}
2.写一个物品类(类型,名字,品质),创建10个物品,添加到List中,同时使用类型、品质、名字疮毒进行比较,排序的权重是:类型>品质>名字长度
物品数据类:
class ItemList
{
public int type;
public string name;
public int quality;
public ItemList(int type,string name,int quality)
{
this.type = type;
this.name = name;
this.quality = quality;
}
public void ShowItemInfo()
{
Console.WriteLine("物品的类型是:{0},品质是:{1},名字是:{2}",type,quality,name);
}
}
主函数调用:
List<ItemList> itemLists = new List<ItemList>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
itemLists.Add(new ItemList(r.Next(1, 5), "" + i + "" + r.Next(100, 100000), r.Next(1, 10)));
}
itemLists.Sort((a, b) =>
{
if (a.type != b.type)
{
return a.type > b.type ? 1 : -1;
}
if (a.quality != b.quality)
{
return a.quality > b.quality ? 1 : -1;
}
return a.name.Length > b.name.Length ? 1 : -1;
});
foreach (var item in itemLists)
{
item.ShowItemInfo();
}
运行结果:
物品的类型是:1,品质是:3,名字是:136130
物品的类型是:1,品质是:4,名字是:760853
物品的类型是:2,品质是:1,名字是:349943
物品的类型是:2,品质是:1,名字是:261133
物品的类型是:2,品质是:3,名字是:919746
物品的类型是:2,品质是:6,名字是:020356
物品的类型是:2,品质是:7,名字是:419499
物品的类型是:3,品质是:1,名字是:587941
物品的类型是:3,品质是:4,名字是:832324
物品的类型是:4,品质是:7,名字是:672702
3.请尝试利用List排序方式对Dictionary中的内容排序提示:得到Dictionary的所有键值对信息存入List中
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(2, "456");
dic.Add(6, "564");
dic.Add(1, "456");
dic.Add(4, "156453");
dic.Add(3, "15343");
dic.Add(5, "12233");
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
foreach (KeyValuePair<int, string> item in dic)
{
list.Add(item);
}
list.Sort((a, b) =>
{
return a.Key > b.Key ? 1 : -1;
});
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i].Key + "_" + list[i].Value);
}
第34课 协变与逆变
1.请描述协变逆变有什么作用
(此处参考@四月的白羊座)
//请描述协变逆变有什么作用
//是用来修饰泛型替代符的 泛型委托和泛型接口中
//1.out修饰的泛型类型 只能作为返回值类型 协变
// in修饰的泛型类型 只能作为参数类型 逆变
//2.遵循里氏替换原则 用out和in修饰的泛型委托 如果类型是父子关系 那么可以相互装载
// 协变: 父类泛型委托容器可以装 子类泛型委托容器
// 逆变: 子类泛型委托容器可以装 父类泛型委托容器
2.通过代码说明协变和逆变的作用
//通过代码说明协变和逆变的作用
//协变
delegate T TestOut<out T>();
//逆变
delegate void TestIn<in T>(T v);
class Father
{
}
class Son:Father
{
}
class Program
{
static void Main(string[] args)
{
//协变 代码
TestOut<Son> ts = () =>
{
return new Son();
};
TestOut<Father> tf = ts;
Father f = tf();
//逆变 代码
TestIn<Father> tif = (value) =>
{
};
TestIn<Son> tis = tif;
tis(new Son());
}
}
第36课 多线程
1.贪吃蛇中蛇的移动实现
enum E_MoveDir
{
Up,
Down,
Right,
Left,
}
class Icon
{
//当前移动的方向
public E_MoveDir dir;
//当前位置
public int x;
public int y;
public Icon(int x, int y, E_MoveDir dir)
{
this.x = x;
this.y = y;
this.dir = dir;
}
//移动
public void Move()
{
switch (dir)
{
case E_MoveDir.Up:
y -= 1;
break;
case E_MoveDir.Down:
y += 1;
break;
case E_MoveDir.Right:
x += 2;
break;
case E_MoveDir.Left:
x -= 2;
break;
}
}
//绘制
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write("■");
}
//擦除
public void Clear()
{
Console.SetCursorPosition(x, y);
Console.Write(" ");
}
//转向
public void ChangeDir(E_MoveDir dir)
{
this.dir = dir;
}
}
主函数调用:
Console.CursorVisible = false;
icon = new Icon(10, 5, E_MoveDir.Right);
icon.Draw();
//开启多线程
Thread t = new Thread(NewThreadLogic);
t.IsBackground = true;
t.Start();
while (true)
{
Thread.Sleep(500);
icon.Clear();
icon.Move();
icon.Draw();
}
void NewThreadLogic()
{
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W:
icon.ChangeDir(E_MoveDir.Up);
break;
case ConsoleKey.A:
icon.ChangeDir(E_MoveDir.Left);
break;
case ConsoleKey.S:
icon.ChangeDir(E_MoveDir.Down);
break;
case ConsoleKey.D:
icon.ChangeDir(E_MoveDir.Right);
break;
}
}
第38课 预处理命令
1.请说出至少4种预处理器指令
//请说出至少4种预处理器指令
//#define 定义一个符号 (没有值的变量)
//#undef 取消定义一个符号
//#if
//#elif
//#else
//#endif
//#warning
//#error
2.请使用预处理器指令实现 写一个函数计算两个数 当是Unity5版本时算加法 是Unity2017版本时算乘法 当时Unity2020版本时算减法 都不是返回0
static int Calc(int a, int b)
{
#if Unity5
return a + b;
#elif Unity2017
return a * b;
#elif Unity2020
return a - b;
#else
return 0;
#endif
}