Skip to content

Commit ad09171

Browse files
authored
Merge pull request aalhour#134 from RyanGrange/issue129
Issue 129: Return primes list as read-only.
2 parents 897712d + e97c2cf commit ad09171

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

DataStructures/Common/PrimesList.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ public int GetPreviousPrime(int number)
184184
}
185185

186186
/// <summary>
187-
/// Returns the list of primes
187+
/// Returns the read-only IList of primes
188188
/// </summary>
189-
public List<int> GetAll
189+
public IList<int> GetAll
190190
{
191-
get { return _primes; }
191+
get { return _primes.AsReadOnly(); }
192192
}
193193

194194
/// <summary>

UnitTest/DataStructuresTests/PrimeListTest.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using DataStructures.Common;
1+
using System;
2+
using DataStructures.Common;
23
using Xunit;
34

45
namespace UnitTest.DataStructuresTests
@@ -11,5 +12,13 @@ public void DoTest()
1112
var instance = PrimesList.Instance;
1213
Assert.Equal(10000, instance.Count);
1314
}
15+
16+
[Fact]
17+
public void PrimesListIsReadOnly()
18+
{
19+
var instance = PrimesList.Instance;
20+
NotSupportedException ex = Assert.Throws<NotSupportedException>(() => instance.GetAll[0] = -1);
21+
Assert.Equal("Collection is read-only.", ex.Message);
22+
}
1423
}
1524
}

0 commit comments

Comments
 (0)