如何创建整数的静态 class?
How to create a static class of integers?
首先举个例子,在Windows我可以调用的表格中:
Color.Red;
并取回哈希值。我正在寻找的是有一个 ErrorLevel class 有这样的东西:
ErrorLevel.Warning
它只是 returns 一个值为 0、1 等的整数
我对静态classes、接口等有很好的理解
我对如何执行此操作的替代方法不感兴趣,例如某些控制台 class 已经具有此功能,因为我想了解此主题。
此外,它的名称可以帮助我完成 google searches/online 教程。
现在我正在弄乱这个,但不知道我在做什么。
internal class ErrorLevel
{
public static ErrorLevel Error { get; }
public static ErrorLevel Warning { get; }
public static ErrorLevel Info { get; }
}
在您的 class 中,一个 ErrorLevel
包含三个 ErrorLevel
。每一个都包含什么?它们将包含三个 ErrorLevel
。每个都包含三个 ErrorLevel
s...从来没有实际值。
最简单地说,这听起来像是在描述 enum
:
public enum ErrorLevel
{
Error,
Warning,
Info
}
但是,如果您想要更自定义的功能,例如您描述的功能,请关注您所描述的内容:
And get a hash back
所以 属性 的值为 string
。如果您的属性应该是字符串,请将它们设为:
internal class ErrorLevel
{
public static string Error { get; } = "Error";
public static string Warning { get; } = "Warning";
public static string Info { get; } = "Info";
}
或者甚至可能只是常量值:
internal class ErrorLevel
{
public const string Error = "Error";
public const string Warning = "Warning";
public const string Info = "Info";
}
为什么不使用 enumeration?
public enum ErrorLevel
{
Error,
Warning,
Info
}
你可以用这样的东西得到字符串:
var level = ErrorLevel.Warning; // 1
string str = level.ToString(); // "Warning"
当然,你也可以使用具有常量的静态class:
static public class ErrorLevel
{
public const int Error = 0;
public const int Warning = 1;
public const int Info = 2;
}
但枚举似乎更适合您的目的,除非您需要更大的灵活性或需要更改值(不再是常量):
static public class ErrorLevel
{
static public int Error = 0;
static public int Warning = 1;
static public int Info = 2;
}
首先举个例子,在Windows我可以调用的表格中:
Color.Red;
并取回哈希值。我正在寻找的是有一个 ErrorLevel class 有这样的东西:
ErrorLevel.Warning
它只是 returns 一个值为 0、1 等的整数
我对静态classes、接口等有很好的理解
我对如何执行此操作的替代方法不感兴趣,例如某些控制台 class 已经具有此功能,因为我想了解此主题。
此外,它的名称可以帮助我完成 google searches/online 教程。
现在我正在弄乱这个,但不知道我在做什么。
internal class ErrorLevel
{
public static ErrorLevel Error { get; }
public static ErrorLevel Warning { get; }
public static ErrorLevel Info { get; }
}
在您的 class 中,一个 ErrorLevel
包含三个 ErrorLevel
。每一个都包含什么?它们将包含三个 ErrorLevel
。每个都包含三个 ErrorLevel
s...从来没有实际值。
最简单地说,这听起来像是在描述 enum
:
public enum ErrorLevel
{
Error,
Warning,
Info
}
但是,如果您想要更自定义的功能,例如您描述的功能,请关注您所描述的内容:
And get a hash back
所以 属性 的值为 string
。如果您的属性应该是字符串,请将它们设为:
internal class ErrorLevel
{
public static string Error { get; } = "Error";
public static string Warning { get; } = "Warning";
public static string Info { get; } = "Info";
}
或者甚至可能只是常量值:
internal class ErrorLevel
{
public const string Error = "Error";
public const string Warning = "Warning";
public const string Info = "Info";
}
为什么不使用 enumeration?
public enum ErrorLevel
{
Error,
Warning,
Info
}
你可以用这样的东西得到字符串:
var level = ErrorLevel.Warning; // 1
string str = level.ToString(); // "Warning"
当然,你也可以使用具有常量的静态class:
static public class ErrorLevel
{
public const int Error = 0;
public const int Warning = 1;
public const int Info = 2;
}
但枚举似乎更适合您的目的,除非您需要更大的灵活性或需要更改值(不再是常量):
static public class ErrorLevel
{
static public int Error = 0;
static public int Warning = 1;
static public int Info = 2;
}