TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Array.AsReadOnly Method (ObjectModel)

Use the Array.AsReadOnly method to get a ReadOnlyCollection from an array.
Array.AsReadOnly. An array itself cannot be read-only. This is not possible with basic array types. But with the ReadOnlyCollection and the AsReadOnly method on the Array class, we add another layer of indirection to eliminate unwanted writes.ArrayReadOnlyCollection
Example. Let's start by looking at this program. The System.Collections.ObjectModel namespace is included. This is because the ReadOnlyCollection is found there. We call Array.AsReadOnly and pass the int array as the argument.Int Array

Finally: We display the Count. If you try to change an element in the ReadOnlyCollection, your program won't compile.

C# program that uses Array.AsReadOnly using System; using System.Collections.ObjectModel; class Program { static void Main() { int[] array = { 1, 5, 3 }; ReadOnlyCollection<int> result = Array.AsReadOnly(array); Console.WriteLine("Count: " + result.Count); for (int i = 0; i < result.Count; i++) { // Can't touch this. Console.WriteLine(result[i]); } } } Output Count: 3 1 5 3
Internals. What does Array.AsReadOnly do internally? Does it copy the array? In my investigation, I found that AsReadOnly simply called into the ReadOnlyCollection constructor. In that constructor, the array is received as an IList instance.

Then: A reference to the original array is stored as a field in ReadOnlyCollection. Little extra memory is used when this method is called.

IList
Summary. We demonstrated Array.AsReadOnly and also ventured into the System.Collections.ObjectModel namespace to see the ReadOnlyCollection type. Arrays cannot be made immutable in the C# language. Elements can always be assigned.

Thus: Array.AsReadOnly makes it possible to restrict access to arrays by client code.

But: This extra level of indirection has a performance cost. And it introduces a name change: the Length property becomes Count.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf