Search code examples
c#outputconsole.writeline

Console.WriteLine() output truncated messages or in a wrong order/incomplete


Well, I don't know why this is happening, but this looks curious because I didn't find anything similar:

...

As you can see inspectioning the full string we can see the entire output, but in console it looks truncated. Is the same as:

...

Do you notice it? Look on the first 4 lines. And look in the inspector. there are missing some tuple.Item1 and yuple.Item2.

There you can see is a problem on the LINQ select. But I can't figure out this why it's happening.

...

There you can see the entire code:

    private const string ClassName = "ClassName",
                         InheritedClass = "InheritedClass";

    private static void Main(string[] args)
    {
        var files = Directory.GetFiles(StaticPath, "*.cs", SearchOption.AllDirectories);

        var classes = files.Select(file => GetClassesInFile(file)).SelectMany(c => c).ToList();

        string fullMsg = string.Join(Environment.NewLine, classes.Select(tuple => $"{tuple.Item1}{(tuple.Item2.HasContent(string.Empty, " ") ? " : " + string.Join(", ", tuple.Item2) + " " : " ")}(on {Path.GetFileName(tuple.Item3)})"));
        Console.WriteLine(fullMsg);

        var unsealedClasses = classes.Where(c => !classes.Any(subClass => subClass.Item2.Contains(c.Item1))).ToList();

        string msg = $"Unsealed classes count: {unsealedClasses.Count} ({string.Join(", ", unsealedClasses.Select(c => c.Item1))})";
        Console.WriteLine(msg);

        Console.Read();
    }

    private static IEnumerable<Tuple<string, string[], string>> GetClassesInFile(string file)
    {
        return Regex.Matches(File.ReadAllText(file), $"(public|internal|private) class (?<{ClassName}>(.+?))( : (?<{InheritedClass}>(.+?))|)\n")
            .Cast<Match>()
            .Select(m => new Tuple<string, string[], string>(GetGroupValue(m, ClassName), GetGroupValue(m, InheritedClass).CleanLineBreak().Split(','), file))
            .Where(t => !string.IsNullOrEmpty(t.Item1));
    }

    private static string GetGroupValue(Match m, string groupName)
    {
        return m.Groups[groupName].Value.ToString();
    }
  • GetGroupValue outputs a string from the Regex Group indexer.
  • GetClassesInFile outputs a IEnumerable of Tuple, where Item1 is the class name, Item2 is the classes/interfaces it depends on, and the third Item is the file path of this class.

Note: As you can see I'm also filtering null or empty strings on tuple.Item1.

I don't know what is happening here. But this is so weird and I saw this for first time.

EDIT:

I look at the output height as mentioned by a user-comment (in reference to: Is there a limit on the output of Console Window?), and my output is over 9001:

...


Solution

  • Game, MovieClip and SpriteTex class names have carriage return char \r at the end. Try to call Trim() in GetGroupValue method.

    private static string GetGroupValue(Match m, string groupName)
    {
        return m.Groups[groupName].Value.ToString().Trim();
    }