Stack overflow error in C# - but how to fix it?

前端 未结 5 1152
旧时难觅i
旧时难觅i 2021-01-05 07:50

I\'ve run into a really interesting runtime bug which generates a rogue stack overflow.

I\'ve defined a structure as follows:

public enum EnumDataTy         


        
5条回答
  •  时光说笑
    2021-01-05 07:51

    stack overflow happens because in the setter you are setting the property to a value (Ie you are trying to get something to set itself to something ... which causes an infinite loop) ... which means that it tries to set itself to a value which means its tries to set itself to a value untill boom

    your properties will never get the values you are setting because they always return the same value (not the stored value)

    public enum EnumDataType { Raspberry, Orange, Pear, Apple }; 
    
    public class DataRequest 
    { 
    private long _dataSize = 0;
    private EnumDataType _dataType = EnumDataType.Apple;
    
    public long DataSize { get { return _dataSize ; } set { _dataSixe= value; } } 
    public EnumDataType DataType  { get { return _dataType; } set { _dataType= value; } } 
    } 
    

    is what you really wanted

提交回复
热议问题