Brian Triplett
Brian Triplett

Reputation: 3532

String stream object in .NET library

Is anyone aware of a .NET class for encapsulating a collection of objects (strings in my case) which allow for Stream-like reading, seeking, etc. Essentially I need a List that has a GetNext method that will return the next object and update the current reading position.

This wouldn't be hard to impliment (possibly with extension methods) but I wanted to leverage any currently developed .NET classes that may already exist.

EDIT: I want to add that the data will always be accessed in a forward manner (i.e. no need to seek to a spedific position) or just reset to zero. So it seems like an IEnumerator may work.

Upvotes: 2

Views: 252

Answers (3)

Michael Bray
Michael Bray

Reputation: 15265

BinaryReader/BinaryWriter has functions for serializing all of the primitive types. As long as you know what type to expect, this should work for you. Seeking will be hard if you expect it to maintain string boundaries, but will work great for forward-only reading of all primitives.

Upvotes: -1

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

There's always IEnumerable. Of course, the limitation is that you can't seek backwards.

Upvotes: 2

codekaizen
codekaizen

Reputation: 27419

This is what IEnumerable/IEnumerator does. Just call GetEnumerator() on your list and use the IEnumerator's Current/MoveNext() members.

If you want more functionality in terms of moving the current record pointer, you might find what you need just using the Take() extension method. Implementing an IEnumerable which maintains internal state is also farily easy.

Upvotes: 5

Related Questions