在C#你不能有索引的属性。这就是说,我怎么转换下面的代码从VB.net到C#?

Private _PatchSpectrum(49) As Double

Public ReadOnly  Property GetPatchSpectrum() As Double()
    Get
        Return _PatchSpectrum
    End Get
End Property

Public WriteOnly Default Property PatchSpectrum(idx As Integer) As Double
    Set(ByVal value as Double)
        _PatchSpectrum(idx) = value
    End Set
End Property
有帮助吗?

解决方案

您应该这样做,如:

private double[] _PatchSpectrum = new double[49]

public double[] GetPatchSpectrum
{
    get { return _PatchSpectrum; }
}

public double this[int index]
{
    set { this._PatchSpectrum[index] = value; }
}

其他提示

或者用方法,而不是属性:

double[] _patchSpectrum = new double[49];

public void SetPatchSpectrum(int index, double value) 
{
    _patchSpectrum[index] = value;
}

public double[] GetPatchSpectrum() 
{
    return _patchSpectrum;
}

可以定义一个索引的对象,这是上如何集合类像List工作。 E.g:

public double this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return _PatchSpectrum[i];
        }
        set
        {
            _PatchSpectrum[i] = value;
        }
    }

有关将来的代码转换的检查出 Telerik的代码转换器

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top