1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . ObjectModel ;
4
5
using System . Globalization ;
5
6
using System . Linq ;
6
7
@@ -30,7 +31,6 @@ public TextLines()
30
31
{
31
32
lines = new LinkedList < string > ( ) ;
32
33
Count = 0 ;
33
- IsReadOnly = false ;
34
34
InvalidateLastAccessed ( ) ;
35
35
}
36
36
@@ -61,7 +61,7 @@ public TextLines(IEnumerable<string> inputLines) : this()
61
61
/// <summary>
62
62
/// If the object is ReadOnly or not.
63
63
/// </summary>
64
- public bool IsReadOnly { get ; private set ; }
64
+ public bool IsReadOnly => false ;
65
65
66
66
/// <summary>
67
67
/// Sets or gets the element at the given index.
@@ -82,16 +82,12 @@ public string this[int index]
82
82
}
83
83
84
84
/// <summary>
85
- /// Creates a readonly shallow copy .
85
+ /// Return a readonly collection of the current object .
86
86
/// </summary>
87
- /// <returns>A readonly shallow copy of the current object.</returns>
88
- public IList < string > ReadOnly ( )
87
+ /// <returns>A readonly collection of the current object.</returns>
88
+ public ReadOnlyCollection < string > ReadOnly ( )
89
89
{
90
- var ret = new TextLines ( ) ;
91
- ret . IsReadOnly = true ;
92
- ret . Count = this . Count ;
93
- ret . lines = this . lines ;
94
- return ret ;
90
+ return new ReadOnlyCollection < string > ( this ) ;
95
91
}
96
92
97
93
/// <summary>
@@ -108,7 +104,6 @@ public void Add(string item)
108
104
/// </summary>
109
105
public void Clear ( )
110
106
{
111
- ValidateReadOnly ( ) ;
112
107
lines . Clear ( ) ;
113
108
}
114
109
@@ -169,7 +164,6 @@ public int IndexOf(string item)
169
164
/// </summary>
170
165
public void Insert ( int index , string item )
171
166
{
172
- ValidateReadOnly ( ) ;
173
167
ThrowIfNull ( item , nameof ( item ) ) ;
174
168
LinkedListNode < string > itemInserted ;
175
169
if ( Count == 0 && index == 0 )
@@ -196,7 +190,6 @@ public void Insert(int index, string item)
196
190
/// <returns>true if removal is successful, otherwise false.</returns>
197
191
public bool Remove ( string item )
198
192
{
199
- ValidateReadOnly ( ) ;
200
193
var itemIndex = IndexOf ( item ) ;
201
194
if ( itemIndex == - 1 )
202
195
{
@@ -212,7 +205,6 @@ public bool Remove(string item)
212
205
/// </summary>
213
206
public void RemoveAt ( int index )
214
207
{
215
- ValidateReadOnly ( ) ;
216
208
ValidateIndex ( index ) ;
217
209
var node = GetNodeAt ( index ) ;
218
210
if ( node . Next != null )
@@ -358,13 +350,5 @@ private static void ThrowIfNull<T>(T param, string paramName)
358
350
throw new ArgumentNullException ( paramName ) ;
359
351
}
360
352
}
361
-
362
- private void ValidateReadOnly ( )
363
- {
364
- if ( IsReadOnly )
365
- {
366
- throw new NotSupportedException ( Strings . TextLinesReadOnlyCollection ) ;
367
- }
368
- }
369
353
}
370
354
}
0 commit comments