Nano HE
Nano HE

Reputation: 9307

Need a sample function implement for array interface member

I have a interface member like this.

AppInterface.cs

ObjLocation[] ArrayLocations { get; }

App.cs

public ObjLocation[] ArrayLocations
        {
            get
            {
                 return **something**;
            }
        }

I dont know how to complete it, Or there is another way to implement the array members.

Then It can pass the compiler. thank you.

Upvotes: 1

Views: 219

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500385

Well, you'd just return an array:

public ObjLocation[] ArrLocations 
{
    get
    {
         ObjLocation[] locations = new ObjLocation[10];
         // Fill in values here
         return locations;
    }
}

Or if you've already got a List<ObjLocation>, you might do:

public ObjLocation[] ArrLocations 
{
    get
    {             
         return locationList.ToArray();
    }
}

It's hard to know what to suggest you write for the property body when we don't know what the property is meant to do or what data you have.

One thing you should consider very carefully is what happens if the caller changes the array contents. If you have an array within your class and you just return a reference to that, then the caller is able to mess with your data - which probably isn't what you want. There's no way of returning a "read only" array, because there's no such concept - which is one reason they're considered somewhat harmful. (It would be nicer if the interface specified that you had to return an IList<ObjLocation> or IEnumerable<ObjLocation>.)

If this isn't enough to solve your problem, please give more information.

Upvotes: 2

roufamatic
roufamatic

Reputation: 18485

return new ObjLocation[size] or return new ObjLocation[] { objLocationInstance1, objLocationInstance2, ... }

Upvotes: 1

Related Questions