C# 需要数组接口成员的示例函数实现
我有一个这样的接口成员 AppInterface.csC# 需要数组接口成员的示例函数实现,c#,interface,implementation,C#,Interface,Implementation,我有一个这样的接口成员 AppInterface.cs public ObjLocation[] ArrayLocations { get { return **something**; } } ObjLocation[]数组位置{get;} App.cs public ObjLocation[] ArrayLocations {
public ObjLocation[] ArrayLocations
{
get
{
return **something**;
}
}
ObjLocation[]数组位置{get;}
App.cs
public ObjLocation[] ArrayLocations
{
get
{
return **something**;
}
}
我不知道如何完成它,或者有另一种方法来实现数组成员
然后它可以通过编译器。谢谢。
返回新的ObjLocation[size]
或返回新的ObjLocation[]{objlocationstance1,objlocationstance2,…}
那么,您只需返回一个数组:
public ObjLocation[] ArrLocations
{
get
{
ObjLocation[] locations = new ObjLocation[10];
// Fill in values here
return locations;
}
}
或者,如果您已经有了列表
,您可以执行以下操作:
public ObjLocation[] ArrLocations
{
get
{
return locationList.ToArray();
}
}
当我们不知道该属性的用途或您拥有的数据时,很难知道建议您为该属性体编写什么
<> P>你应该仔细考虑的是如果调用方改变数组内容会发生什么。如果您的类中有一个数组,并且您只返回一个对该数组的引用,那么调用者可能会弄乱您的数据—这可能不是您想要的。没有返回“只读”数组的方法,因为没有这样的概念——这也是它们被忽略的原因之一。(如果接口指定您必须返回IList
或IEnumerable
,那就更好了)
如果这还不足以解决您的问题,请提供更多信息