首页 > 解决方案 > 有没有办法在开发环境中查看数组元素及其索引值?

问题描述

我正在使用非常大的数组来存储常量,并且很难找到要编辑的正确值,因为我必须计算它们。

在 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#visual-studio

解决方案


考虑使用一个实际的类来表示该数据,而不仅仅是一个对象数组。想想你将如何使用这个数组——你必须在所有地方都投射东西。鉴于我们谈论的是咒语,尽管这是一个有趣的双关语,但在 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);

推荐阅读