Array property syntax in C#

后端 未结 6 622
旧巷少年郎
旧巷少年郎 2020-12-18 23:27

I have a a class that has an integer array property and I am trying to figure out the right syntax for it. The integer array gets instantiated in the class constructor.

相关标签:
6条回答
  • 2020-12-18 23:50
    class DemoClass
    {
        private int[] myNumbers;
        public int[] MyNumbers
        {
            get { return myNumbers; }
            set { myNumbers = value;}
        }
    
        public DemoClass(int elements)
        {
            // Here, the array should get instantiated using the elements.
            MyNumbers = new int[5] { 1, 2, 3, 4, 5};
        }
    }
    
    0 讨论(0)
  • 2020-12-18 23:52

    CA1819: Properties should not return arrays

    http://msdn.microsoft.com/en-us/library/0fss9skc.aspx

    Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.

    To fix a violation of this rule, either make the property a method or change the property to return a collection instead of an array

    0 讨论(0)
  • 2020-12-18 23:53

    Are you looking for:

    class DemoClass
    {
        public int[] MyNumbers { get; private set; }
    
        public DemoClass(int elements)
        {
            MyNumbers = new int[elements];
        }
    }
    

    As for normal properties that do nothing except publicize a private field (as you seem to want):

    private int[] myNumbers;
    public int[] MyNumbers
    {
        get { return myNumbers; }
        set { myNumbers = value; }
    }
    
    0 讨论(0)
  • 2020-12-19 00:01
     class DemoClass
        {
            private int[] myNumbers;
            public int[] MyNumbers
            {
                get { return myNumbers; }
                set { myNumbers = value; }
            }
    
            public DemoClass(int[] elements)
            {
                myNumbers = elements;
                // Here, the array should get instantiated using the elements.
            }
        }
    
    0 讨论(0)
  • 2020-12-19 00:02

    It is called Auto-Implemented Properties . So if you have syntax like

    public int[] MyNumbers { get; set; }
    

    C# compiler will automatically create for you backing field. This feature was introduced in C# 3.0, and before that you always had to implement property with backing field.

    You can read more at: New C# "Orcas" Language Features: Automatic Properties, Object Initializers, and Collection Initializers

    0 讨论(0)
  • 2020-12-19 00:04

    If the number of element in the array is fixed, I would only provide a getter for the array and leave off the setter. You will still be able to assign values to individual elements in the array, but this will prevent someone from swapping the whole array out from under you (or setting it to null. The code would look like this:

    class DemoClass
    {
        public int[] MyNumbers
        { get; private set; }
    
        public DemoClass(int elements)
        {
            MyNumbers = new int[elements];
        }
    }
    

    If the number of elements are not fixed, then you should use a List<int> rather than an array, and then you definitely want a property with no setter.

    0 讨论(0)
提交回复
热议问题