Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中使用enum作为整型常量#_C#_Oop_Enums_Constants - Fatal编程技术网

C# 在C语言中使用enum作为整型常量#

C# 在C语言中使用enum作为整型常量#,c#,oop,enums,constants,C#,Oop,Enums,Constants,我的问题很简单,但我没有找到一种实现代码的方法。所以我开始怀疑我想要实现的代码是否不好。如果是,最好的方法是什么 下面是: class InputManager { SortedDictionary<ushort,Keys> inputList = new SortedDictionary<ushort,Keys>(); public void Add(ushort id, Keys key) {...} public bool

我的问题很简单,但我没有找到一种实现代码的方法。所以我开始怀疑我想要实现的代码是否不好。如果是,最好的方法是什么

下面是:

class InputManager  
{  
    SortedDictionary<ushort,Keys> inputList = new SortedDictionary<ushort,Keys>();  

    public void Add(ushort id, Keys key) {...}  
    public bool IsPressed(ushort id) {...}  
}  

class Main  
{  
    private enum RegisteredInput : ushort  
    {  
        Up,  
        Down,  
        Confirm  
    }  

    public Main()  
    {  
            InputManager manager = new InputManager();

            manager.Add(RegisteredInput.Up, Keys.Q);
            manager.Add(RegisteredInput.Down, Keys.A);
            manager.Add(RegisteredInput.Confirm, Keys.Enter);
    }

    void update()
    {
    if(manager.IsPressed(RegisteredInput.Up)) action();
    }
}
类输入管理器
{  
SortedDictionary inputList=新的SortedDictionary();
public void Add(ushort id,key){…}
公共bool-IsPressed(ushort-id){…}
}  
班长
{  
专用枚举注册表项输入:ushort
{  
向上的
下来,,
证实
}  
公用干管()
{  
InputManager=新的InputManager();
Add(RegisteredInput.Up,key.Q);
添加(RegisteredInput.Down,key.A);
添加(RegisteredInput.Confirm,Keys.Enter);
}
无效更新()
{
if(manager.IsPressed(RegisteredInput.Up))action();
}
}
此代码无法编译,因此会出现此类错误:

与“InputManager.Add(ushort,Keys)”匹配的最佳重载方法具有一些无效参数
参数“1”:无法从“RegisteredInput”转换为“ushort”

如果我在
manager.Add((ushort)RegisteredInput.Up,Keys.Q)中使用类似的强制转换它会工作的。但是由于强制转换必须是显式的,所以我想知道它不是C语言中的重新编码代码,就像C++中的代码,如果有更好的方法(比如使用代码> const UsSuth<代码>,对于每一个值,我都不太喜欢)。
到目前为止,我得到的最好的答案是“从”,但听起来很像黑客,我很担心


谢谢

使InputManager成为泛型类型。即:

class InputManager<T>
{
   SortedDictionary<T,Keys> inputList = new SortedDictionary<T,Keys>();  

   public void add(T id, Keys key) {...}  
   public bool isPressed(T id) {...}    
}
类输入管理器
{
SortedDictionary inputList=新的SortedDictionary();
public void add(T id,Keys key){…}
公共bool-isPressed(T-id){…}
}

隐式强制转换对于枚举是必需的,我建议:

public static class RegisteredInput {
    public const ushort Up = 0;
    public const ushort Down = 1;
    public const ushort Confirm = 2;
}

为什么不使用枚举定义字典呢?是否有理由将其设为int

public void add(RegisteredInput id, Keys key) {...}  

另外,一般建议公开可访问的成员(方法、类型等)应使用pascal大小写(换句话说,
Add
而不是
Add
)。

为什么不将字典定义为
dictionary
?此外,当你询问一些事情时,尽量避免在网站上重新输入错误信息和代码,因为你做了一些错事,我们可能会被挂断电话。喜欢异常消息是否实际将方法命名为“Addd”并带有3d?我们怎么能相信你能真正复制实际使用的代码,而不是重新键入一些有其他问题的简化代码呢?因为我不想要重复的键。关于错误消息,我没有重新键入,只是从中删除了许多不相关的名称空间。我还更改了方法的真实名称,因为它们不是英文的。我不明白你所说的“重复键”是什么意思。你能详细解释一下吗?嗯,一开始我好像没有正确理解你的建议。很抱歉我以为你在问是否应该用字典而不是字典。你的建议和亚当·罗宾逊和莱皮的建议差不多。再次抱歉!那么。。。比我快一分钟!显然也是如此!但是你真的认为这是一种更好的编写代码的方法吗,c#wise?我不喜欢这样一个事实,即我必须模拟枚举的基本工作,自动为枚举赋值。我过去遇到的许多技术问题使我最近远离了枚举。我对所有常量都使用这种做法,无论它们是整数、字符串、日期等。这比枚举要多做一点工作,但不会太多。我自己也尝试在枚举必须在声明的类(或代码块)之外时避免枚举。我确实认为<代码>私有枚举<代码>非常安全。我同意。尽管底层类型是一个ushort,但在使用从ushort抽象出来的枚举时(如果只是一点点),编译器会抱怨您使用枚举是出于非枚举原因,因此显式强制转换。如果不希望强制转换,则需要更改签名以支持枚举,切换到类变量,或者只重载Add()以支持枚举和ushorts。关于pascal cased,它在原始版本中。我也会在这里纠正它。很抱歉我不想直接使用枚举作为键来避免耦合。很好!这么简单,但我没有想到。我想我会采用这个解决方案。谢谢