Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP var_dump in C# to dump array or objects? [duplicate]

Tags:

c#

I need to dump the content of arrays or objects and I am interested to know if in C# we have something like PHP instruction var_dump.

The objective is to not build a loop to use every property or content of array or object and print with Console.WriteLine.

like image 448
David Avatar asked May 23 '14 08:05

David


People also ask

What is Var_dump () used for?

The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

What does Var_dump return?

@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .

Why Var_dump () is preferable over Print_r ()?

It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans. Example: Say we have got the following array and we want to display its contents.


3 Answers

The closest thing would probably be string.Join:

Console.WriteLine(string.Join(", ", myEnumOfObjects));

It would not automatically include "every property or content of array or object" into the output, though - if you want that to happen, you need to override the ToString method of the object being printed:

class MyObject {
    public string Name {get;set;}
    public DateTime Dob {get;set;}
    public override string ToString() {
        return string.Format("{0} - {1}", Name, Dob);
    }
}
like image 198
Sergey Kalinichenko Avatar answered Oct 20 '22 09:10

Sergey Kalinichenko


I think there aren't direct equivalent of var_dump php function.

You must use reflection to write an equivalent function.

If you search in web, you can easily find code which do it.

For example : http://ruuddottech.blogspot.fr/2009/07/php-vardump-method-for-c.html

like image 3
binard Avatar answered Oct 20 '22 10:10

binard


When you insert a break point you can easily view the contents of an array by hovering your mouse over it.

or any of these:

You are probably using Console.WriteLine for printing the array.

int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
    Console.WriteLine(item.ToString());
}

If you don't want to have every item on a separate line use Console.Write:

int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
    Console.Write(item.ToString());
}

or string.Join (in .NET Framework 4 or later):

int[] array = new int[] { 1, 2, 3 };
Console.WriteLine(string.Join(",", array));

from this question: How to print contents of array horizontally?

like image 3
Stijn Bernards Avatar answered Oct 20 '22 10:10

Stijn Bernards