Is it possible to declare an array as readonly in C #?

Is it possible to make one array read-only. so the setpoint for the array will not be allowed.

this is what i tried with the readonly keyword to declare an array. Then I check if this array is read-only using the IsReadOnly property. but he never returns.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrivateExposeConsoleApp
{
    class Program
    {
        private static readonly int[] arr = new int[3] { 1,2,3 };

        static void Main(string[] args)
        {
            // Create a read-only IList wrapper around the array.
            IList<int> myList = Array.AsReadOnly(arr);

            try
            {
                // Attempt to change a value of array through the wrapper. 
                arr[2] = 100;
                Console.WriteLine("Array Elements");
                foreach (int i in arr)
                {
                    Console.WriteLine("{0} - {1}", i + "->", i);
                }
                Console.WriteLine("---------------");
                Console.WriteLine("List Elements");
                foreach (int j in myList)
                {
                    Console.WriteLine("{0} - {1}", j + "->", j);
                }
                // Attempt to change a value of list through the wrapper. 
                myList[3] = 50;
            }
            catch (NotSupportedException e)
            {

                Console.WriteLine("{0} - {1}", e.GetType(), e.Message);
                Console.WriteLine();
            }



            //if (arr.IsReadOnly)
            //{
            //    Console.WriteLine("array is readonly");
            //}
            //else
            //{
            //    for (int i = 0; i < arr.Length; i++)
            //    {
            //        arr[i] = i + 1;
            //    }

            //    foreach (int i in arr)
            //    {
            //        Console.WriteLine(i);
            //    }
            //}

            Console.ReadKey();
        }
    }
}

Here are my comments. if I uncomment this, my arr will never be read-only. upon declaration, I explicitly defined arr as readonly with data as {1,2,3}. I do not want this value to be rewritten. it should always be only 1,2,3.

+4
source share
2 answers

, Array.AsReadOnly<T>(T[] array), Array.

(, ) ReadOnlyCollection<T>.

:

// declaration of a normal example array
string[] myArray = new string[] { "StackOverflow", "SuperUser", "MetaStackOverflow" };

// declaration of a new ReadOnlyCollection whose elements are of type string
// the string array is passed through the constructor
// that is where our array is passed into its new casing
// as a matter of fact, the ReadOnlyCollection is a wrapper for ordinary collection classes such as arrays
ReadOnlyCollection<string> myReadOnlyCollection = new ReadOnlyCollection<string>(maArray);

Console.WriteLine(myReadOnlyCollection[0]); // would work fine since the collection is read-only
Console.WriteLine(myReadOnlyCollection[1]); // would work fine since the collection is read-only
Console.WriteLine(myReadOnlyCollection[2]); // would work fine since the collection is read-only

myReadOnlyCollection[0] = "ServerFault"; // the [] accessor is neither defined nor would it be allowed since the collection is read-only.

MSDN.


getter,

public T getElement(int index) { return array[index]; }

- , ?


Array.IsReadOnly MSDN ,

.

, IList<T>.IsReadOnly arr.IsReadOnly.

+4

ReadOnlyCollection<T>. arr.AsReadOnly()

+6

All Articles