c# - Clean alternatives to passing index by reference -


in code parsing array of bytes. sequentially parse bytes passing around index so:

headerdata = parseheader(bytes, ref index) middledata = parsemiddle(bytes, ref index) taildata   = parsetail  (bytes, ref index) 

without hardcoding amount increment header, there way achieve similar functionality without having pass index reference? 1 of rare cases using ref keyword best solution?

like slaks said, possible solution use stream.

to create stream current bytes array can following:

memorystream stream = new memorystream(bytes); 

to read current byte of stream can use readbyte method, shown below:

byte b = stream.readbyte(); 

the stream keep track of current index in array, new code like:

memorystream stream = new memorystream(bytes); headerdata = parseheader(stream) middledata = parsemiddle(stream) taildata   = parsetail  (stream) 

check out documentation see other methods available memorystream.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -