c# - 有没有办法在开发环境中查看数组元素及其索引值?
问题描述
我正在使用非常大的数组来存储常量,并且很难找到要编辑的正确值,因为我必须计算它们。
在 Visual Studio 中是否有一个选项或以这种方式显示数组的东西?(例如,将鼠标悬停在元素上会显示它在数组中的索引)
这是我正在使用的:
public static IDictionary<string, Object[]> elementStatus = new Dictionary<string, Object[]>()
{
{"Fire", (new Object[spellinfoLength]
{"Burn",null,null,null, null, null, null, null,null,null,null, null, null, null, null, null, null, null, null, 0.5,null,"Fire", null}
)
},
{"Lightning", (new Object[spellinfoLength]
{ "Shock", null, null, null, null, null, null, null, null, null, null, null, null, 0.5,
null, null, null, null, null, null, null, null, null }
)
},
//etc... (25 more parallel arrays)
}
任何建议都会有所帮助。
解决方案
考虑使用一个实际的类来表示该数据,而不仅仅是一个对象数组。想想你将如何使用这个数组——你必须在所有地方都投射东西。鉴于我们谈论的是咒语,尽管这是一个有趣的双关语,但在 C# 中进行转换通常是一种代码味道。C# 最棒的地方之一就是强类型。但通过使用object
一切,你把它扔掉了。
您拥有的事实spellinfoLength
告诉我,您的意图是在每个代表咒语的数组中具有相同的数字。因此,您可以通过一次将默认值初始化为 null 来实现这一点,而不是通过每个构造函数来设置它们。
作为附带的好处,您现在知道您正在使用一个 Spell 而不仅仅是一个命名的对象数组。
public class Spell
{
public Spell(string name, string typeOfDamage, double damage)
{
Name=name;
TypeOfDamage=typeOfDamage; // consider using an enum?
Damage=damage;
// etc Add more properties here. I cant tell what type the other things in the array are
// since they are all null.
// some of these properties may be writable during their lifetime, so for those ones, add a 'set;' as well
}
public string Name { get; }
public string TypeOfDamage { get; }
public double Damage { get; }
}
现在,当您使用构造函数创建 Spell 时,每次在括号内按逗号时,intellisense 都会告诉您参数是什么。
PS你可以Name
像这样制作他们的字典......
public static IDictionary<string, Spell> elementStatus = new[]
{
new Spell("Fire", "Burn", 0.5),
new Spell("Lightning", "Shock", 0.5),
//etc... (25 more)
}.ToDictionary(x=>x.Name,y=>y);
推荐阅读
- javascript - 忽略部分字符串的正则表达式
- ruby - 如何观察实例变量并触发方法?
- jpa - DAO 类中的 get 方法(对于 OneToMany)是否总是进行数据库调用?
- sql-server - 带有 OUTPUT 子句的 INSERT INTO TEMP 表向不在源中的值添加额外字符
- ada - 在定点声明上使用 gnat 出现奇怪的编译错误
- php - 将项目从 xampp 文件夹更改为另一个文件夹并删除了以前的 xampp 版本
- oauth - 授予访问令牌后的 Oauth2 流程
- linux - 如何控制内核空间中的 LED 列表
- python - 散景(Python):在悬停工具提示中格式化日期时间
- kubernetes - 如何配置 Kubernetes 以接收 TCP 连接