c# - 如何将枚举传递给函数以及如何在 if 语句 c# 中评估枚举

标签 c# enums

假设我有一个包含一个枚举的类。所以当有人调用类函数时,他们会向函数发送一个枚举值,我们喜欢从函数中使用 if 语句来检查发送了什么值。

public class BBALogger
{
enum MsgType
{
    Error ,
    Info 
}

public void WriteToLog(String inLogMessage, Enum msgtype)
{
     if(msgtype==MsgType.Error)
     {

     }
     else if(msgtype==MsgType.info)
     {

     }
}
}

这样调用函数

class BBALogger x = new BBALogger()
x.WriteToLog("Hello",MsgType.Error);

我收到错误。我在哪里犯了错误。

最佳答案

您可以简单地使用 MsgType 而不是 Enum

public void WriteToLog(String inLogMessage, MsgType msgtype)
{
     if(msgtype==MsgType.Error)
     {

     }
     else if(msgtype==MsgType.info)
     {

     }
}

关于c# - 如何将枚举传递给函数以及如何在 if 语句 c# 中评估枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624096/

相关文章:

validation - Pydantic 与枚举子类?

c# - 覆盖属性的 XML 注释

c# - 在 C# 中生成代码块

c# - Visual Studio 中是否有创建方法的快捷方式?

Java 枚举和附加类文件

java - 常量和简化枚举值访问

c# - 如何反转列表内容的显示顺序?

c# - 无法将类型 'System.Data.SqlClient.SqlInternalConnectionTds' 的对象强制转换为类型 'System.Data.SqlClient.SqlInternalConnectionSmi'

python - 如何使用字典和 google protobuf 枚举的类型提示?

swift - 通用枚举函数参数?