Const Arrays ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stu Banter

    #1

    Const Arrays ?

    I have an array filled with const Int32, right now it is defined just as a
    public variable with an initializer.

    // as it is now
    public int [] myarr = new int [] {1,2,3,4,5}

    // as it should be logically but doesn't work.
    public const int [] myarr = new int [] {1,2,3,4,5}

    If that is not possible, I considered making it a private var with a get
    accessor only, but how do I make one for an array or one of its elements ?

    Thanks for your replies

    Stu


  • Mattias Sjögren

    #2
    Re: Const Arrays ?

    Stu,
    [color=blue]
    >// as it should be logically but doesn't work.
    >public const int [] myarr = new int [] {1,2,3,4,5}[/color]

    You can do

    public static readonly int [] myarr = new int [] {1,2,3,4,5};

    That makes the array reference read-only, but does not prevent someone
    from changing the array content.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Bret Mulvey

      #3
      Re: Const Arrays ?

      You could encapsulate the array in a class to protect the array elements:

      class ReadOnlyIntArra y
      {
      private int[] array;

      public ReadOnlyIntArra y(int[] array)
      {
      this.array = array;
      }

      public int this[int index]
      {
      get { return array[index]; }
      }
      }

      class MyApp
      {
      static void Main()
      {
      ReadOnlyIntArra y myarr = new ReadOnlyIntArra y(new int[] {1, 2, 3, 4,
      5});
      int x = myarr[3]; // ok
      myarr[3] = 42; // won't compile
      }
      }

      In this example the array was allocated an initialized inside the
      constructor call. If it had been done outside the call, e.g.

      int[] temp = {1, 2, 3, 4, 5};
      ReadOnlyIntArra y myarr = new ReadOnlyIntArra y(temp);

      then changes to temp[] would modify the array contents. If you want to
      prevent that, you could modify the constructor to say

      this.array = (int[]) array.Clone();

      to make a copy of the array instead of just copying the reference.


      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:#4tV2hehDH A.2748@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Stu,
      >[color=green]
      > >// as it should be logically but doesn't work.
      > >public const int [] myarr = new int [] {1,2,3,4,5}[/color]
      >
      > You can do
      >
      > public static readonly int [] myarr = new int [] {1,2,3,4,5};
      >
      > That makes the array reference read-only, but does not prevent someone
      > from changing the array content.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      Working...