11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . Collections . ObjectModel ;
45using System . Globalization ;
56using System . Linq ;
67
@@ -30,7 +31,6 @@ public TextLines()
3031 {
3132 lines = new LinkedList < string > ( ) ;
3233 Count = 0 ;
33- IsReadOnly = false ;
3434 InvalidateLastAccessed ( ) ;
3535 }
3636
@@ -61,7 +61,7 @@ public TextLines(IEnumerable<string> inputLines) : this()
6161 /// <summary>
6262 /// If the object is ReadOnly or not.
6363 /// </summary>
64- public bool IsReadOnly { get ; private set ; }
64+ public bool IsReadOnly => false ;
6565
6666 /// <summary>
6767 /// Sets or gets the element at the given index.
@@ -82,16 +82,12 @@ public string this[int index]
8282 }
8383
8484 /// <summary>
85- /// Creates a readonly shallow copy .
85+ /// Return a readonly collection of the current object .
8686 /// </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 ( )
8989 {
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 ) ;
9591 }
9692
9793 /// <summary>
@@ -108,7 +104,6 @@ public void Add(string item)
108104 /// </summary>
109105 public void Clear ( )
110106 {
111- ValidateReadOnly ( ) ;
112107 lines . Clear ( ) ;
113108 }
114109
@@ -169,7 +164,6 @@ public int IndexOf(string item)
169164 /// </summary>
170165 public void Insert ( int index , string item )
171166 {
172- ValidateReadOnly ( ) ;
173167 ThrowIfNull ( item , nameof ( item ) ) ;
174168 LinkedListNode < string > itemInserted ;
175169 if ( Count == 0 && index == 0 )
@@ -196,7 +190,6 @@ public void Insert(int index, string item)
196190 /// <returns>true if removal is successful, otherwise false.</returns>
197191 public bool Remove ( string item )
198192 {
199- ValidateReadOnly ( ) ;
200193 var itemIndex = IndexOf ( item ) ;
201194 if ( itemIndex == - 1 )
202195 {
@@ -212,7 +205,6 @@ public bool Remove(string item)
212205 /// </summary>
213206 public void RemoveAt ( int index )
214207 {
215- ValidateReadOnly ( ) ;
216208 ValidateIndex ( index ) ;
217209 var node = GetNodeAt ( index ) ;
218210 if ( node . Next != null )
@@ -358,13 +350,5 @@ private static void ThrowIfNull<T>(T param, string paramName)
358350 throw new ArgumentNullException ( paramName ) ;
359351 }
360352 }
361-
362- private void ValidateReadOnly ( )
363- {
364- if ( IsReadOnly )
365- {
366- throw new NotSupportedException ( Strings . TextLinesReadOnlyCollection ) ;
367- }
368- }
369353 }
370354}
0 commit comments