@@ -30,6 +30,7 @@ public TextLines()
30
30
{
31
31
lines = new LinkedList < string > ( ) ;
32
32
Count = 0 ;
33
+ IsReadOnly = false ;
33
34
InvalidateLastAccessed ( ) ;
34
35
}
35
36
@@ -60,7 +61,7 @@ public TextLines(IEnumerable<string> inputLines) : this()
60
61
/// <summary>
61
62
/// If the object is ReadOnly or not.
62
63
/// </summary>
63
- public bool IsReadOnly => false ;
64
+ public bool IsReadOnly { get ; private set ; }
64
65
65
66
/// <summary>
66
67
/// Sets or gets the element at the given index.
@@ -80,6 +81,19 @@ public string this[int index]
80
81
}
81
82
}
82
83
84
+ /// <summary>
85
+ /// Creates a readonly shallow copy.
86
+ /// </summary>
87
+ /// <returns>A readonly shallow copy of the current object.</returns>
88
+ public IList < string > ReadOnly ( )
89
+ {
90
+ var ret = new TextLines ( ) ;
91
+ ret . IsReadOnly = true ;
92
+ ret . Count = this . Count ;
93
+ ret . lines = this . lines ;
94
+ return ret ;
95
+ }
96
+
83
97
/// <summary>
84
98
/// Adds the given string to the end of the list.
85
99
/// </summary>
@@ -94,6 +108,7 @@ public void Add(string item)
94
108
/// </summary>
95
109
public void Clear ( )
96
110
{
111
+ ValidateReadOnly ( ) ;
97
112
lines . Clear ( ) ;
98
113
}
99
114
@@ -154,6 +169,7 @@ public int IndexOf(string item)
154
169
/// </summary>
155
170
public void Insert ( int index , string item )
156
171
{
172
+ ValidateReadOnly ( ) ;
157
173
ThrowIfNull ( item , nameof ( item ) ) ;
158
174
LinkedListNode < string > itemInserted ;
159
175
if ( Count == 0 && index == 0 )
@@ -180,6 +196,7 @@ public void Insert(int index, string item)
180
196
/// <returns>true if removal is successful, otherwise false.</returns>
181
197
public bool Remove ( string item )
182
198
{
199
+ ValidateReadOnly ( ) ;
183
200
var itemIndex = IndexOf ( item ) ;
184
201
if ( itemIndex == - 1 )
185
202
{
@@ -195,6 +212,7 @@ public bool Remove(string item)
195
212
/// </summary>
196
213
public void RemoveAt ( int index )
197
214
{
215
+ ValidateReadOnly ( ) ;
198
216
ValidateIndex ( index ) ;
199
217
var node = GetNodeAt ( index ) ;
200
218
if ( node . Next != null )
@@ -340,5 +358,13 @@ private static void ThrowIfNull<T>(T param, string paramName)
340
358
throw new ArgumentNullException ( paramName ) ;
341
359
}
342
360
}
361
+
362
+ private void ValidateReadOnly ( )
363
+ {
364
+ if ( IsReadOnly )
365
+ {
366
+ throw new NotSupportedException ( Strings . TextLinesReadOnlyCollection ) ;
367
+ }
368
+ }
343
369
}
344
370
}
0 commit comments