如何创建一个属性,是枚举类型 [英] How to create a property that is enum type
问题描述
enum E_Color { red, black };
private E_Color Color
{
get { return Color; }
set { Color = value; }
}
public Card(int color, int num)
{
Color = (E_Color)color;
Number = num;
}
所以这是我的代码,我不知道什么问题,Im相当肯定它在这里。进出口新的C#(我使用C ++编程),所以我不知道该怎么做出头。
当编译但打印运行过程因终止StackOverflowException。
如果我踏进它只是停止debuging和结束时,它得到的构造。
So this is my code and i dont know whats the problem and im pretty sure its here. Im new to c# (i used to program in c++) so i dont know how to do somethings. when run it in compiles but prints "Process is terminated due to StackOverflowException." if i step into it just stops debuging and finishes when it gets to the constructor.
推荐答案
有没有问题与此枚举。问题是你的财产。你是返还财产本身,这会导致一个无限循环(#1
)
There is no problem with your enum here. The problem is in your property. You are returning the property itself, which causes an infinite loop (Stackoverflow
).
改成这样:
private E_Color Color
{
get; set;
}
什么你基本上做的是这样的:
What you are basically doing is this:
private E_Color GetColor()
{
return GetColor();
}
这篇关于如何创建一个属性,是枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!