Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the StringCollection is read-only in C#
The StringCollection class in C# represents a collection of strings and provides an IsReadOnly property to check if the collection is read-only. A read-only collection cannot be modified after creation, meaning you cannot add, remove, or modify elements.
By default, a StringCollection created using the parameterless constructor is not read-only, allowing modifications like adding or removing strings.
Syntax
Following is the syntax for checking if a StringCollection is read-only −
bool isReadOnly = stringCollection.IsReadOnly;
Return Value
The IsReadOnly property returns a bool value −
true− The StringCollection is read-only and cannot be modified.false− The StringCollection is not read-only and can be modified.
Using IsReadOnly with Numeric Strings
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Is the StringCollection read-only? = " + stringCol.IsReadOnly);
Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("800"));
}
}
The output of the above code is −
Array elements... 100 200 300 400 500 Is the StringCollection read-only? = False Does the specified string is in the StringCollection? = False
Using IsReadOnly with Name Strings
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "John", "Tim", "Kevin", "Bradman", "Katie", "Tom", "Nathan" };
Console.WriteLine("String array elements...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Is the StringCollection read-only? = " + stringCol.IsReadOnly);
Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("Tim"));
}
}
The output of the above code is −
String array elements... John Tim Kevin Bradman Katie Tom Nathan Is the StringCollection read-only? = False Does the specified string is in the StringCollection? = True
How It Works
The StringCollection class implements the IList interface, which includes the IsReadOnly property. When you create a new StringCollection instance using the default constructor, it creates a mutable collection where IsReadOnly returns false.
In both examples above, the collections are created normally and can be modified, so IsReadOnly returns false. The examples also demonstrate using Contains() method to check if a specific string exists in the collection.
Conclusion
The IsReadOnly property of StringCollection allows you to determine if the collection can be modified. By default, newly created StringCollection instances are not read-only, enabling you to add, remove, or modify elements as needed.
