0

Id like to firstly apologise for what may appear to be a stupid question but im confused regarding the following.

Im writting a class library which will not be running on the UI thread. Inside the CL i need an array which im going populate with data received from a stored procedure call. I then need to pass this data back to the UI thread via an event.

Originally i was going to write the following.

public class ColumnInformation
{
    public string[] columnHeaderNames;
    public string[] columnDataTypes;
}

but im pretty sure that would be frowned upon and i instead should be using properties.

public class ColumnInformation
{
    public string[] columnHeaderNames {get; set;}
    public string[] columnDataTypes {get; set;}
}

but then i came across the following. MSDN

so am i correct in assuming that i should actually declare this as follows:

public class ColumnInformation    
{        
    private string[] _columnHeaderNames;         

    public Names(string[] headerNames)        
    {            
        _columnHeaderNames = headerNames;        
    }         

    public string[] GetNames()        
    {            
        // Need to return a clone of the array so that consumers            
        // of this library cannot change its contents            
        return (string[])_columnHeaderNames.Clone();        
    }    
}

Thanks for your time.

1
  • Mithrandir, your post was helpful! Commented May 17, 2012 at 9:37

1 Answer 1

1

If your concern is the guideline CA1819: Properties should not return arrays,
It will be same whether you are exposing Array as a Public Field, or Property (making readonly does not matter here). Once your original Array is exposed, its content can be modified.

To avoid this, as the link suggest, make Field private, and return Clone from the Getter. However major concern is that there may be multiple copies of your array if retrieved many times. It is not good for performance and synchronization.

Better solution is ReadOnlyCollection.

Using ReadOnlyCollection, you can expose the collection as read only which cannot be modified. Also any changes to underlying collection will be reflected.

Sign up to request clarification or add additional context in comments.

4 Comments

(purpose of SProc is just to get a list of SProc's and names of inputs. names will be used in a combo box) Ok so if i have understood you correctly, i need to create an array, populate it with the data from the datatable and then create the read only collection and pass that back to the UI thread? I was only planning to read this list once, and also have the option to completely update the list.
If you are sure, array will not be modified outside, you can safely expose Array as public field or Property. In case you want to protect the array from any modification then only ReadOnlyCollection is required.
first block is also frowned upon. It also does not protect modification of array content.
so as long as i dont need to write protect, 2nd block is ok? If i need to ensure its write protected then i use the readonlycollection you mentioned above?? or am i still not gettting this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.