none
Bug? Expected new StringReader("\n").ReadLine() to return empty string but actually returns null! RRS feed

  • Question

  • I expected new StringReader("\n").ReadLine() to return empty string but actually returns null. The documentation states "The returned value is null if the end of the underlying string has been reached." But the end of the input has not been reached! To make this more explicit note that 

    var sr = new StringReader("\na");
    sr.ReadLine(); // returns null
    sr.ReadLine(); // returns "a"

    So the behavior doesn't match the documentation.

    Thanks,
    Chris

    Monday, August 6, 2012 9:59 PM

Answers

  • That's not the behavior, when I run.

    Try this full program:

    using System;
    using System.IO;
    
    internal class Program
    {
        private static void Main()
        {
            var sr = new StringReader("\na");
            string one = sr.ReadLine(); // returns null
            string two = sr.ReadLine(); // returns "a"
    
            Console.WriteLine("One = \"{0}\" [one==null: {1}, one==string.Empty: {2}]", one, one == null, one == string.Empty);
            Console.WriteLine(two);
    
            Console.WriteLine("Press key to exit:");
            Console.ReadKey();
        }
    }

    This outputs:

    One = "" [one==null: False, one==string.Empty: True]
    a
    Press key to exit:
    
    

    As you can see, it's writing string.Empty.

    Now, if you use new StringReader("\n"), the behavior is a bit different.  Try:

    using System;
    using System.IO;
    
    internal class Program
    {
        private static void Main()
        {
            var sr = new StringReader("\n");
            string one = sr.ReadLine(); // returns null
            string two = sr.ReadLine(); // returns "a"
    
            Console.WriteLine("One = \"{0}\" [one==null: {1}, one==string.Empty: {2}]", one, one == null, one == string.Empty);
            Console.WriteLine("Two = \"{0}\" [two==null: {1}, two==string.Empty: {2}]", two, two == null, two == string.Empty);
    
            Console.WriteLine("Press key to exit:");
            Console.ReadKey();
        }
    }

    Which prints:

    One = "" [one==null: False, one==string.Empty: True]
    Two = "" [two==null: True, two==string.Empty: False]
    Press key to exit:
    
    
    
    
    

    This matches the documentation perfectly....


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kingces95 Monday, August 6, 2012 11:57 PM
    Monday, August 6, 2012 10:46 PM
    Moderator

All replies

  • That's not the behavior, when I run.

    Try this full program:

    using System;
    using System.IO;
    
    internal class Program
    {
        private static void Main()
        {
            var sr = new StringReader("\na");
            string one = sr.ReadLine(); // returns null
            string two = sr.ReadLine(); // returns "a"
    
            Console.WriteLine("One = \"{0}\" [one==null: {1}, one==string.Empty: {2}]", one, one == null, one == string.Empty);
            Console.WriteLine(two);
    
            Console.WriteLine("Press key to exit:");
            Console.ReadKey();
        }
    }

    This outputs:

    One = "" [one==null: False, one==string.Empty: True]
    a
    Press key to exit:
    
    

    As you can see, it's writing string.Empty.

    Now, if you use new StringReader("\n"), the behavior is a bit different.  Try:

    using System;
    using System.IO;
    
    internal class Program
    {
        private static void Main()
        {
            var sr = new StringReader("\n");
            string one = sr.ReadLine(); // returns null
            string two = sr.ReadLine(); // returns "a"
    
            Console.WriteLine("One = \"{0}\" [one==null: {1}, one==string.Empty: {2}]", one, one == null, one == string.Empty);
            Console.WriteLine("Two = \"{0}\" [two==null: {1}, two==string.Empty: {2}]", two, two == null, two == string.Empty);
    
            Console.WriteLine("Press key to exit:");
            Console.ReadKey();
        }
    }

    Which prints:

    One = "" [one==null: False, one==string.Empty: True]
    Two = "" [two==null: True, two==string.Empty: False]
    Press key to exit:
    
    
    
    
    

    This matches the documentation perfectly....


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kingces95 Monday, August 6, 2012 11:57 PM
    Monday, August 6, 2012 10:46 PM
    Moderator
  • Yes, well, that's because I'm a moron. I had my test method as:

            private void ReadLine(TextReader actual, TextReader expected)

    When what I wanted was:

            private void ReadLine(TextReader expected, TextReader actual)

    So sorry about the wild goose chase!


    Monday, August 6, 2012 11:57 PM