• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Beginners C#query

5.00 star(s)

Scrag Meister

Super poster
Thanks received
38
How does the bolded red line below do what it does in detecting the EOF of the text file?

Coming from a VBA background I'm slightly confused.

I understand the readline assigns to my string field (data) but how is the not equal to null functioning?

I tested and has nothing to do with being a blank row.

Thanks.


using (StreamReader sr = new StreamReader(args[0]))
{
string data =null;
// Assumes all rows are populated
while ((data=sr.ReadLine()) !=null) {
iRows++;
Console.WriteLine(iRows + " - " + data);
}
}
 
Last edited: 16:28
A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is a null reference (Nothing in Visual Basic) if the end of the input stream is reached.

StreamReader.ReadLine Method

ReadLine reads the line untill it gets to the end of the file and then it returns null.

GE
 
(data=sr.ReadLine()) evaluates first, giving a value for data that is either a string or null which is then tested against then != null.

It's not pretty.
 
Thanks for that Gareth, so what is the !=null comparing too?

The string or the success of the assignment to the string?

Edit : So Assigning a value returns the value assigned?

If I have a blank line in the file what is that value if not a null?
 
Last edited: 16:41
Edit : So Assigning a value returns the value assigned?

It returns the object assigned to (which has the same value as you just assigned it).

if( ( data=sr.ReadLine() ) != null )

is exactly the same as:

data=sr.ReadLine();
if( data != null )
{

}

But that doesn't work in a while.

Despite being lambasted in the past for daring to suggest knowing the syntax of the language is important, I would never do that. I've always thought it's a horrible left over from the days of yore (or days of C to be accurate), and it's better in that case to write the code more clearly.
 
Edit : So Assigning a value returns the value assigned?

Yes that's essentially what is happening, C# operates very similarly to other languages here. Making expressions like x = y = z = 10 possible.

From the following link: 7.13.1 Simple assignment (C#)

The = operator is called the simple assignment operator. In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the value of the right operand to the variable, property, or indexer element given by the left operand.

The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value.
 
Terrible, terrible code.

I know exactly why it is done though as they are thinking...

using (StreamReader sr = new StreamReader(args[0]))
{
while ( sr.ReadLine() !=null) {
iRows++;
Console.WriteLine(iRows + " - " + sr.ReadLine() );
}
}

involves 2 method calls and they figure that dumping the contents of the call into a variable and using that next is quicker. All balls of course but they think that is how it works. See that kind of tulip everyday sadly.
 

Not in that context.

iRows is going to be the same with ++ appended or prepended when it comes to the next line

I would have written it...

using (StreamReader sr = new StreamReader(args[0]) {
while ( sr.ReadLine() !=null) {
Console.WriteLine(iRows++ + " - " + sr.ReadLine() );
}

but then I am a maverick who sees every line like a penny spent.

7 rows down to 4.

You can see why my code metrics don't do me any favours in clientCo.
 

Advertisers

Back
Top