C#-এ Array.AsReadOnly(T[]) মেথডটি নির্দিষ্ট অ্যারেতে একটি শুধুমাত্র পঠনযোগ্য (read-only) র্যাপার ফেরত দেয়, যা হলো ReadOnlyCollection<T>।
সিনট্যাক্স
public static System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly<T> (T[] array);
এখানে, T হল অ্যারেতে উপাদানগুলির প্রকার, আর array হল এক-মাত্রিক শূন্য-ভিত্তিক অ্যারে।
উদাহরণ
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
String[] arr = { "John", "Tom", "Katie", "Brad" };
// শুধুমাত্র পঠনযোগ্য IList র্যাপার
IList<String> list = Array.AsReadOnly( arr );
// শুধুমাত্র পঠনযোগ্য IList-এর মান প্রদর্শন
Console.WriteLine( "Initial read-only IList..." );
display( list );
// এখন শুধুমাত্র পঠনযোগ্য র্যাপার পরিবর্তন করার চেষ্টা
try {
list[0] = "Kevin";
list[1] = "Bradley";
}
catch ( NotSupportedException e ) {
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message );
Console.WriteLine();
}
Console.WriteLine( "After changing two elements, the IList remains the same since it is read-only..." );
display( list );
}
public static void display( IList<String> list ) {
for ( int i = 0; i < list.Count; i++ ) {
Console.WriteLine(list[i] );
}
Console.WriteLine();
}
}
আউটপুট
Initial read-only IList... John Tom Katie Brad System.NotSupportedException Collection is read-only. After changing two elements, the IList remains the same since it is read-only... John Tom Katie Brad