Which code segment should you use?

You are writing a method that accepts a string parameter named message.
Your method must break the message parameter into individual lines of text and pass each line to a second method named Process.
Which code segment should you use?

You are writing a method that accepts a string parameter named message.
Your method must break the message parameter into individual lines of text and pass each line to a second method named Process.
Which code segment should you use?

A.
StringReader reader = new StringReader(message);
Process(reader.ReadToEnd());
reader.Close();

B.
StringReader reader = new StringReader(message);
while (reader.Peek() != -1) {
string line = reader.Read().ToString();
Process(line);
}
reader.Close();

C.
StringReader reader = new StringReader(message);
Process(reader.ToString());
reader.Close();

D.
StringReader reader = new StringReader(message);
while (reader.Peek() != -1) {
Process(reader.ReadLine());
}
reader.Close();

Explanation:
StringReader.ReadLine() allows for lines to be read line by line.
A ReadToEnd() will read the entire stream.
B Read() will not read the line but only the next character.
C will not read from the message but will just give a string representation of the reader.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


seenagape

seenagape

Correct answer is D