C#之nameOf的基本使用

nameof表达式也是C#的语法糖之一,它是编译时求值,有和反射相同的时功能,这样可以提高代码的性能,质量和可读性

1 让变量、类型或成员的名称作为字符串常量

2 用于异常消息的创建,让异常更准确捕获错误的来源。

3 监测属性的更改,下面的案例将展示属性更改的检查

官方文档:https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/nameof?redirectedfrom=MSDN

Console.WriteLine(nameof(System.Collections.Generic));  // output: Generic
Console.WriteLine(nameof(List<int>));  // output: List
Console.WriteLine(nameof(List<int>.Count));  // output: Count
Console.WriteLine(nameof(List<int>.Add));  // output: Add

List<int> numbers = [1, 2, 3];
Console.WriteLine(nameof(numbers));  // output: numbers
Console.WriteLine(nameof(numbers.Count));  // output: Count
Console.WriteLine(nameof(numbers.Add));  // output: Add
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的MUD引擎示例,使用C#语言编写: ```csharp using System; using System.Collections.Generic; namespace MUD_Engine { class MUD { static void Main(string[] args) { Console.WriteLine("Welcome to MUD!"); // Create a list of rooms List<Room> rooms = new List<Room>(); rooms.Add(new Room(0, "The Entrance", "You are standing in front of the entrance to a mysterious castle.")); rooms.Add(new Room(1, "The Courtyard", "You are in a large courtyard surrounded by castle walls.")); rooms.Add(new Room(2, "The Great Hall", "You have entered a grand hall with high ceilings and ornate decorations.")); // Create some connections between rooms rooms[0].AddConnection(Direction.NORTH, rooms[1]); rooms[1].AddConnection(Direction.SOUTH, rooms[0]); rooms[1].AddConnection(Direction.EAST, rooms[2]); rooms[2].AddConnection(Direction.WEST, rooms[1]); // Start the game in the entrance room Player player = new Player("Player 1", rooms[0]); player.Look(); // Game loop while (true) { string input = Console.ReadLine(); Command command = new Command(input); player.ExecuteCommand(command); } } } enum Direction { NORTH, SOUTH, EAST, WEST } class Room { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public Dictionary<Direction, Room> Connections { get; set; } public Room(int id, string name, string description) { Id = id; Name = name; Description = description; Connections = new Dictionary<Direction, Room>(); } public void AddConnection(Direction direction, Room room) { Connections.Add(direction, room); } } class Player { public string Name { get; set; } public Room CurrentRoom { get; set; } public Player(string name, Room startingRoom) { Name = name; CurrentRoom = startingRoom; } public void Look() { Console.WriteLine(CurrentRoom.Description); } public void Move(Direction direction) { if (CurrentRoom.Connections.ContainsKey(direction)) { CurrentRoom = CurrentRoom.Connections[direction]; Look(); } else { Console.WriteLine("You cannot go that way."); } } public void ExecuteCommand(Command command) { switch (command.Type) { case CommandType.LOOK: Look(); break; case CommandType.MOVE: Move(command.Direction); break; default: Console.WriteLine("Invalid command."); break; } } } enum CommandType { LOOK, MOVE } class Command { public CommandType Type { get; set; } public Direction Direction { get; set; } public Command(string input) { string[] parts = input.Split(' '); if (parts[0].ToLower() == "look") { Type = CommandType.LOOK; } else if (parts[0].ToLower() == "move") { Type = CommandType.MOVE; switch (parts[1].ToLower()) { case "north": Direction = Direction.NORTH; break; case "south": Direction = Direction.SOUTH; break; case "east": Direction = Direction.EAST; break; case "west": Direction = Direction.WEST; break; default: Console.WriteLine("Invalid direction."); break; } } else { Console.WriteLine("Invalid command."); } } } } ``` 这个MUD引擎使用了一些基本的面向对象编程概念,如类、对象、方法、属性和枚举类型。它包括一个房间类、一个玩家类和一个命令类。 在这个引擎中,玩家可以通过“看”和“移动”两个命令来与游戏交互。玩家可以移动到与当前房间连接的其他房间,也可以查看当前房间的描述。游戏通过一个简单的循环来处理玩家的输入,并将其转换为相应的命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值