开发者

How to use properties to expose a private array as read-only? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

What does the following code do?

class MyClass {
    private int[] myPrivates;
    public int[] GetMyPrivates
    {
      get { return myPrivates; }
    }

    protected int[] SetMyPrivates
    {
      set { myPrivates开发者_运维问答 = value; }
    }
}

Is there a better way of protecting the array myPrivates? Is it possible to make it reat-only?


Your question is extremely unclear.

You may be trying to write

public int[] MyArray { 
    get { return data; }
    protected set { data = value; }
}

If you want to prevent people from modifying the array (eg, instance.MyArray[5] = 42), you'll need to expose a ReadOnlyCollection<int> instead.


You could replace your getters and setters with a property this way:

class MyClass {
    public int[] MyValues { get; protected set; }

    public MyClass() {
        MyValues = new [] {1, 2, 3, 4, 5};
    }

    public void foo {
        foreach (int i in MyValues) {
            Trace.WriteLine(i.ToString());
        }
    }
}

MyOtherClass {
    MyClass myClass;

    // ...
    void bar {
        // You can access the MyClass values in read outside of MyClass, 
        // because of the public property, but not in write because 
        // of the protected setter.
        foreach (int i in myClass.MyValues) {
            Trace.WriteLine(i.ToString());
        }
    }
}

You can add pretty much any protection level that is less that the one of the property to getters and setters.


One of the best things you could do would be to use .NET's auto-implemented properties. This is a feature that allows you to declare a property without a backing field. The backing field is automatically generated by .NET.

So, your code could be translated to:

class MyClass
{
   public int[] GetMyPrivates
   {
      get;
      protected set;
   }
}

Now, as to your more generic question, "what can you do inside a class". Well... that's a really, really open question, and deserves a long explanation. But, just skimming the surface, you can:

  • Define objects (that is what a class does)
  • Describe the characteristics for your object through fields (a simple variable) and properties (a method that looks like a field).
  • Describe the behaviors for your object through methods (this is the object-oriented lingo for a function)
  • Define whether your items (objects, characteristics and/or behaviors) are static or instance. Simply put, instance means that you're working on a specific instance of the object/characteristic/behavior; static means that you're working on the whole family of objects
  • Define access control for your objects/characteristics/behaviors (e.g., internal, public, protected, private)

There is so much more that can be said, but that should give you an idea of the power you have at your disposal when you use classes in .NET


If you are coming from Java background (your brackets hint so), you might need to know that .NET has a concept called properties, so you can say:

class MyClass {
    private int[] myPrivates;
    public int[] MyPrivates {
      get { return myPrivates; }
      set {myPrivates = value;}
    }
}

Or even auto-properties:

class MyClass {
    public int[] MyPrivates {get; set;}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜