C# 在C中传递列表元素#

C# 在C中传递列表元素#,c#,console-application,C#,Console Application,正在尝试一个文本冒险游戏。Visual Studio 2019,控制台应用程序 我有一个这样的房间课 public class Room { public string name; public string description; public int roomNumber; public int lightLevel; public int trap; public int flag

正在尝试一个文本冒险游戏。Visual Studio 2019,控制台应用程序

我有一个这样的房间课

  public class Room
    {
        public string name;
        public string description;
        public int roomNumber;
        public int lightLevel;
        public int trap;
        public int flag1;
        public int flag2;
        public int north;
        public int northeast;
        public int east;
        public int southeast;
        public int south;
        public int southwest;
        public int west;
        public int northwest;
        public int up;
        public int down;
    }
    
和一个包含房间数据的文本文件

{front street}
{standing in a dirt road.} 
{1 10 0 0 0 0 0 2 0 0 0 0 0 0 0}
{front street}
{standing on a dirt road on the edge of town.}
{2 10 0 0 0 0 0 3 0 0 0 1 0 0 0}

我有112个房间(或街区),这些房间被列在一个列表中。我计划将此列表扩展到大约400个房间。我需要通过一些房间来使用一些不同的方法。我可以通过整个名单,我觉得这不是个好主意。这需要传递大量的数据,了解小精灵在大量多余的数据中找到了出路。我可以传递一个元素,所以

PrintRoom(ref rooms[currentRoom].name);
但是我似乎找不到合适的语法来通过一个单人房间。我想一次经过一个房间/街区。事实上,我只需要最后十个元素,来确定玩家是否能朝某个方向前进,但我认为所有的工作都会让玩家更容易通过整个房间。 那么,通过单个房间(或街区)的正确语法是什么? 请问函数的标题是什么样子的


我提前向您表示感谢。

如果您将这些内容读入列表,为什么不尝试使用LINQ呢?您可以使用lambda表达式来获得所需的内容,如果您希望将项目投影到一个具有多个属性但少于整个属性集合的对象中,则使用这种方法可以提供更大的灵活性

public class LinqExample
{
    public void LinqMethod()
    {
        var rooms = new List<Room> 
        { 
            new Room
            {
                name = "roomA",
                description = "Desc Abc",
                roomNumber = 1,
                // etc
            },
            new Room
            {
                name = "roomB",
                description = "Desc Def",
                roomNumber = 1,
                // etc
            }
        };

        var room = rooms.FirstOrDefault(x => x.name == "roomB");

        PassRoomExample(room);
    
    }

    public void PassRoomExample(Room room)
    {
        Console.WriteLine($"name: { room.name }, Description: { room.description }, Room #: {room.roomNumber}");
    }
}
public类linqe示例
{
public void LinqMethod()
{
var房间=新列表
{ 
新房间
{
name=“roomA”,
description=“Desc Abc”,
房间号=1,
//等
},
新房间
{
name=“roomB”,
description=“Desc Def”,
房间号=1,
//等
}
};
var room=rooms.FirstOrDefault(x=>x.name==“roomB”);
Passwormoom示例(房间);
}
公共空房间示例(房间)
{
Console.WriteLine($“name:{room.name},Description:{room.Description},room:{room.roomNumber}”);
}
}

打印室(房间[currentRoomIndex])?当然,
PrintRoom()
方法需要接受
Room
类型的参数(例如,
public void PrintRoom(Room roomToPrint){string name=roomToPrint.name;//更多代码}
。仅供参考,这里不需要
ref
关键字,因为
Room
是一个类,它是一种引用类型。您需要编写getter和setter来设置或获取这些参数的值。一旦这样做,您就可以使用.name或.attribI了。