在列表中使用两个单独的字符串类型 [英] Using two separate string types in a list
问题描述
好了,所以我的C#编程类我提出一个冒险游戏。我想我明白该怎么做大部分希望我无法建立世界。
Ok so for my C# programming class I am making an adventure game. I think I understand how to do most of it expect I'm having trouble setting up the "world".
我对世界的类( World.cs
),在那里我开始创建为每个房间列表。不过,我很困惑的增加对每个房间的名称和说明。
I have a class for the world (World.cs
) where I started creating a list for each room. However I'm confused of to add a name and description for each room.
例如,如果列表
(室)键入字符串
我会做 room.Add(监狱,这是一个监狱)
。
就恰如是做到这一点的最好方法是什么?
For example if the List
(room) is type String
I would do room.Add("Prison", "This is a prison)
.
What is the best way to do this?
推荐答案
这是当你创建一个类的
public class Room {
public string Name { get; set; }
public string Description { get; set; }
}
和持有的房间列表:
List<Room> rooms = new List<Room>();
为了任何东西添加到列表中,只要做到这一点:
In order to add anything to the list, simply do this:
rooms.Add(new Room { Name = "Prison", Description = "This is a prison" });
对象用于组数据一起,这将允许更干净的代码,它是面向对象编程的基石之一。
Objects are used to group data together, this will allow for much cleaner code. It is one of the keystones of Object-Oriented Programming.
这篇关于在列表中使用两个单独的字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!