@@ -30,6 +30,7 @@ public TextLines()
3030 {
3131 lines = new LinkedList < string > ( ) ;
3232 Count = 0 ;
33+ IsReadOnly = false ;
3334 InvalidateLastAccessed ( ) ;
3435 }
3536
@@ -60,7 +61,7 @@ public TextLines(IEnumerable<string> inputLines) : this()
6061 /// <summary>
6162 /// If the object is ReadOnly or not.
6263 /// </summary>
63- public bool IsReadOnly => false ;
64+ public bool IsReadOnly { get ; private set ; }
6465
6566 /// <summary>
6667 /// Sets or gets the element at the given index.
@@ -80,6 +81,19 @@ public string this[int index]
8081 }
8182 }
8283
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+
8397 /// <summary>
8498 /// Adds the given string to the end of the list.
8599 /// </summary>
@@ -94,6 +108,7 @@ public void Add(string item)
94108 /// </summary>
95109 public void Clear ( )
96110 {
111+ ValidateReadOnly ( ) ;
97112 lines . Clear ( ) ;
98113 }
99114
@@ -154,6 +169,7 @@ public int IndexOf(string item)
154169 /// </summary>
155170 public void Insert ( int index , string item )
156171 {
172+ ValidateReadOnly ( ) ;
157173 ThrowIfNull ( item , nameof ( item ) ) ;
158174 LinkedListNode < string > itemInserted ;
159175 if ( Count == 0 && index == 0 )
@@ -180,6 +196,7 @@ public void Insert(int index, string item)
180196 /// <returns>true if removal is successful, otherwise false.</returns>
181197 public bool Remove ( string item )
182198 {
199+ ValidateReadOnly ( ) ;
183200 var itemIndex = IndexOf ( item ) ;
184201 if ( itemIndex == - 1 )
185202 {
@@ -195,6 +212,7 @@ public bool Remove(string item)
195212 /// </summary>
196213 public void RemoveAt ( int index )
197214 {
215+ ValidateReadOnly ( ) ;
198216 ValidateIndex ( index ) ;
199217 var node = GetNodeAt ( index ) ;
200218 if ( node . Next != null )
@@ -340,5 +358,13 @@ private static void ThrowIfNull<T>(T param, string paramName)
340358 throw new ArgumentNullException ( paramName ) ;
341359 }
342360 }
361+
362+ private void ValidateReadOnly ( )
363+ {
364+ if ( IsReadOnly )
365+ {
366+ throw new NotSupportedException ( Strings . TextLinesReadOnlyCollection ) ;
367+ }
368+ }
343369 }
344370}
0 commit comments