C# 从VB.NET到C的转换#
在C#中,不能有索引属性。也就是说,如何将以下代码从VB.net转换为C# 您可以这样做:C# 从VB.NET到C的转换#,c#,vb.net-to-c#,C#,Vb.net To C#,在C#中,不能有索引属性。也就是说,如何将以下代码从VB.net转换为C# 您可以这样做: private double[] _PatchSpectrum = new double[49] public double[] GetPatchSpectrum { get { return _PatchSpectrum; } } public double this[int index] { set { this._PatchSpectrum[index] = value; } }
private double[] _PatchSpectrum = new double[49]
public double[] GetPatchSpectrum
{
get { return _PatchSpectrum; }
}
public double this[int index]
{
set { this._PatchSpectrum[index] = value; }
}
您可以在对象上定义一个集合类,如List
的工作方式。例如:
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;
}
}
有关将来的代码转换,请查看。或使用方法而不是属性:
double[] _patchSpectrum = new double[49];
public void SetPatchSpectrum(int index, double value)
{
_patchSpectrum[index] = value;
}
public double[] GetPatchSpectrum()
{
return _patchSpectrum;
}
“在C#中,您不能将参数传递给集合”,是的,您可以。它只是默认为返回类型的name值。我要指出的是,返回数组通常不是最好的主意,而且
GetPatchSpectrum
属性的名称不是很好(听起来很像一个方法)。不幸的是,代码转换器无法正确处理这种情况。我在代码的其余部分使用了Telerik,但失败了,因为C#不接受带参数的属性。我想返回整个arrayAh是的,这只是索引器位。如上所述,返回数组没有任何技巧,只需创建一个属性来返回它。如果你不想使用get{}
方法,你可以省略它。dsolimano,谢谢,这是我第一次回答这篇文章,但看起来我无法标记多个帖子的答案。尽管这很有帮助。谢谢
double[] _patchSpectrum = new double[49];
public void SetPatchSpectrum(int index, double value)
{
_patchSpectrum[index] = value;
}
public double[] GetPatchSpectrum()
{
return _patchSpectrum;
}