Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Console.Log, Print_R(), Debug.Trace in C#?

PHP has a function called print_r() and var_dump() that will display all the contents of an item. It makes it very easy to figure out what things are.

Is there something like that in C#?

I know there is a Console.WriteLine("Hello"); in C#, but does this work in the MVC? Can I do some type of debug.trace() like flash does into a debug console while I run the application?

like image 464
JREAM Avatar asked Jan 21 '13 22:01

JREAM


1 Answers

System.Diagnostics.Debug.WriteLine("blah");

and in order to show all variables in the object you would have to override its ToString() method or write a method which returns all the info you need from the object. i.e.

class Blah{

    string mol = "The meaning of life is";
    int a = 42;    

    public override string ToString()
    {
         return String.Format("{0} {1}", mol, a);
    }
}

System.Diagnostics.Debug.WriteLine(new Blah().ToString());

In short there is nothing in built but it can be done.

If you must print ALL the objects info without overriding or adding logic on a class by class level then you are in the realms of reflection to iterate the objects PropertInfo array

like image 73
Paul Sullivan Avatar answered Nov 20 '22 15:11

Paul Sullivan