2013-12-14 91 views
3

是否可以只读一个数组。这样数组的设置值将不被允许。是否有可能在c#中声明一个数组为readonly?

这里我所用只读关键字试过声明数组。然后我检查该数组是否只读使用IsReadOnly属性。但它永远不会返回true。

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(); 
     } 
    } 
} 

这里看到我的评论部分。如果我取消注释,我的arr永远不会变为只读。在声明中,我清楚地将arr定义为只读数据{1,2,3}。我不希望这个价值被重新发现。它应该始终只有1,2,3。

+1

http://msdn.microsoft.com/en-us/library/53kysx7b(v=vs.110).aspx –

回答

4

创建阵列的只读副本。在Array类本身上定义的方法Array.AsReadOnly<T>(T[] 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's 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. 

Here你可以找到根据MSDN文档。


或者你可以简单地定义一个吸气剂的方法,如

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

为了使您的阵列只读 - 从它的类外至少?


关于你使用Array.IsReadOnly MSDN文档中说,

此属性始终是所有阵列假的。

这意味着您将不得不使用IList<T>.IsReadOnly而不是arr.IsReadOnly。

here

+0

嗨,谢谢。你可以请一个样品详细说明吗?我只是不希望这被允许用于(INT I = 0;我

+0

我做了更新。 –

+0

你想达到什么目的? –

6

阵列本身是可变的,并得到你想要的,你需要使用包装,ReadOnlyCollection<T>行为。您可以使用arr.AsReadOnly()

+0

嗨。我只想要这个数组(这里是arr)是只读的。可能吗 ? arr.AsReadOnly()将返回一个ReadOnlyCollection 。这很好,但那不是我的数组。那是另一个对象。 –

+2

+1。 @ kumarch1 - 除了“数组本身是可变的”之外,还需要其他什么信息? .Net中只有“只读数组”这样的东西。 –

相关问题