4

Creating an Append-Only List Wrapper in C#

 3 years ago
source link: https://ilyana.dev/blog/2021-06-21-append-only-list/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Creating an Append-Only List Wrapper in C#

Date Published: June 21, 2021

Creating an Append-Only List Wrapper in C#

Sometimes, you need a list, but you don't want people messing with the things you've added once they're added.

I was working on a project recently where I needed exactly this functionality. Towards that end, I created a AppendOnlyStringList wrapper class. There are other ways I could have done this (and if you have other ideas on how to implement this, let me know in the chat below!), but as I'm comfortable with using Lists I decided to take the following approach:

My AppendOnlyStringList has a private List<string> _messages, which can be added to using the public Append(string message) method:

public void Append(string message)
{
  _messages.Add(message);
}

AppendOnlyStringList also needed to implement IEnumerable<string> in order for things to "just work" so I could pass around and test my class just as I would if using a List<string>. The implementation of IEnumerable required two methods, which both used _messages to implement them.

The full class ended up looking like this:

public class AppendOnlyStringList : IEnumerable<string>
{
  private List<string> _messages = new();

  public IEnumerator<string> GetEnumerator()
  {
    return _messages.GetEnumerator();
  }

  public void Append(string message)
  {
    _messages.Add(message);
  }

  IEnumerator IEnumerable.GetEnumerator()
  {
    return _messages.GetEnumerator();
  }
}

As you can see, this was very easy to implement, as all the methods just used the List<string> functionality.

For good measure, because testing is important, I added a quick test to make sure my class is doing what it's supposed to do and works as expected:

[Fact]
public void AppendsStringsToList()
{
  var list = new AppendOnlyStringList();
  var testString = "I am a test string";
  var anotherTestString = "Look! I'm another test string!";
  var oneMoreTestString = "Wow, this is probably overkill, but no matter!";

  list.Append(testString);
  list.Append(anotherTestString);
  list.Append(oneMoreTestString);

  Assert.Contains(testString, list);
  Assert.Contains(anotherTestString, list);
  Assert.Contains(oneMoreTestString, list);
}

Thanks for reading! I hope you find this and other articles here at ilyanaDev helpful! Be sure to follow me on Twitter @ilyanaDev.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK