Does Console.Write ever fail?

.net, c#, console.writeline, visual-studio

Solution

Based on your comments, I think either your `firstEnumerable` or `secondEnumerable` objects are `null`. But since you did not post how you obtain these objects, I can't comment on why they are `null` or how to fix it.

It's okay for them to contain `null` entries, or even be fully empty, but they themselves cannot be `null` or it will throw an `ArgumentNullException` when you call `.ToArray()` on them. This corresponds with the "value cannot be null" exception message you're seeing.

The reason why it's not crashing and burning hard is because you are swallowing (and logging?) the exceptions within your iteration loop with a `try/catch` block that was not posted in your code sample.

I'm guessing your actual code is something like this:

foreach (Contact contact in Contact.LoadWithPredicate(getAll))
{
    try
    {
        ...

        object[] firstEnumerable = null;
        object[] secondEnumerable = null;

        //some logic gates here which under some circumstances do not 
        //assign a valid instance to firstEnumerable or secondEnumerable

        ...
        Console.WriteLine(i); //this prints every time

        //Turn the Enumerables into strings for printing
        string firstEnumAsString = String.Join(", ", firstEnumerable.ToArray()); //exception here
        string secondEnumAsString = String.Join(", ", secondEnumerable.ToArray()); //or exception here

        Console.WriteLine("Email: " + firstString+ ", correspondance: " + secondString+ ", PAM addresses: " + firstEnumAsString+ ", famousPeople: " + secondEnumAsString);
        ...
    }
    catch
    {
        //maybe some logging? maybe not?
    }
}

This will print out the `i` value each time. But when it attempts to create `firstEnumAsString` or `secondEnumAsString` it throws an exception and never hits your second `Console.WriteLine("Email: " + ...);` thus producing the output you're seeing. It's not that the `Console.WriteLine` is failing, it's that you're never calling it in the first place.

Problem

I am testing my run() function to make sure it has all the correct things populated by the end of the method. I populate 4 fields from our databases, two strings, and two IEnumerable(string)'s. The idea is to print them all out for all the people I am pulling from the database. When I only print the string fields, everything works fine. However, when I try to add the Enumerables as well, nothing prints at all. I am also printing a counter so I know the program is still running and working. Does Console ever decide to not print anything at all due to space or something else? Here's the code that does it: ``` int i = 0; foreach (Contact contact in Contact.LoadWithPredicate(getAll)) { -------other code to populate fields--------- i ++; Console.WriteLine(i); //Turn the Enumerables into strings for printing string firstEnumAsString = String.Join(", ", firstEnumerable.ToArray()); string secondEnumAsString = String.Join(", ", secondEnumerable.ToArray()); Console.WriteLine("Email: " + firstString+ ", correspondance: " + secondString+ ", PAM addresses: " + firstEnumAsString+ ", famousPeople: " + secondEnumAsString); } ``` So, as my output, whenever I run the code above, I get an output like this: ``` 1 2 Email: foo@bar.com, correspondance: John, PAM addresses: bar@foo.com, famousPeople: foo, bar, foo 3 4 etc ``` Problem is, only two people out of the total (~425) show up, the rest of the lines are just numbers. Shouldn't I at least get the strings "Email", and ", correspondence"? It seems like Console is just deciding not to do anything. And I know that the code must reach it because it prints out the integer i just before I call that Console.WriteLine(). On the other hand, whenever I just ask for the two string fields to be printed, the Console displays both fields for all 425 users, right after their corresponding integer. Does anyone know what's happening here? TIA

Original source