Reputation: 5000
Is there an accepted idiom or a quick and simple way to get a stream representation of a string?
Or is my best bet a new MemoryStream(Encoding.Unicode.GetBytes(myStr))
where myStr
is some string variable?
Upvotes: 3
Views: 65
Reputation: 1062770
Assuming you want UTF8, then yes the code shown in the question is entirely correct.
The only thing I would change is: consider whether your API should actually be talking about TextReader
rather than Stream
. If so, new StringReader(myStr)
would do nicely.
But for arbitrary binary Stream
usage, your code is correct as shown (especially if you add a using
, although in reality that is moot since for MemoryStream
it is a no-op Dispose()
; but I'm fussy ;p)
Upvotes: 5