C# 将类型添加到类中

C# 将类型添加到类中,c#,C#,我有一个简单的类叫做设备 public class Device { public string ID { get; set;} public string Name {get; set;} public string Serial {get; set;} public Device() { } } 我可以用什么变量定义此设备类型 此设备可以是打印机、电话或计算机。。我试过使用字符串: public string DeviceType {get; set;} //e.g : this.D

我有一个简单的类叫做设备

public class Device
{

public string ID { get; set;}
public string Name {get; set;}
public string Serial {get; set;}

public Device()
{
}

}
我可以用什么变量定义此设备类型

此设备可以是打印机、电话或计算机。。我试过使用字符串:

public string DeviceType {get; set;} //e.g : this.DeviceType = "Phone";
但我觉得它并不优雅。我还可以如何为此设备添加类型?使用枚举

enum DeviceType
{
PHONE,
PRINTER,
COMPUTER,
TABLET
}

有什么好办法吗?

你说得对。。。但是,根据你所做的,也考虑其他设备。
enum DeviceType
{
   PHONE,
   PRINTER,
   COMPUTER,
   TABLET,
   APPLE_TV,
   AMAZON_FIRESTICK,
   ROKU, 
   XBOX, 
   PLAYSTATION, 
   MISC_DEVICE    /*Just in case the device being used isn't on the list*/
}

如果你再解释一下你想做什么,我们可以帮你多一点。

嗯,枚举似乎是合理的,但我会使用惯用的名称电话、打印机等,而不是粗制滥造的名称。不清楚你还想找什么。。。或者,如果您想添加行为,您可以将其设置为抽象类并拥有不同的子类。感谢您的回答,我只是在寻找为该设备类实现类型的另一种方法,并避免使用stringSo使用enum,正如您已经决定的那样。不清楚你在问什么-你有一个潜在的解决方案,而且你没有提出任何不使用它的理由…DeviceType应该是int类型。它毕竟是一个枚举,其中的每个项都有一个指定的整数值。e、 g.设备类型。电话应返回1