Cheung
Cheung

Reputation: 15552

ASP.NET ,equivalent to PHP Print_r Function?

PHP Print_r is useful to print out array and dictionary collection. is asp.net build in this function??

Upvotes: 3

Views: 10929

Answers (6)

Piseth Sok
Piseth Sok

Reputation: 1819

You can try:

string[] weekDays4 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
foreach (var item in weekDays4)
{
  Response.Write(item.ToString() + " test<br />");
}

Upvotes: 0

Jacob Phan
Jacob Phan

Reputation: 732

You can achieve it by JavaScriptSerializer

var json = new JavaScriptSerializer().Serialize(yourObject);
Response.Write("yourObject:" + json + "<br/>");

Upvotes: 4

A.D.K
A.D.K

Reputation: 25

No there is no function available in .net as print_r(). But you can use your custom function and print array or IEnumerable items in response. For E.g:

if (val is IEnumerable)
        {
            var e = val as IEnumerable;
            foreach (var i in e)
            {
                Response.Write(i.ToString() + "<br />");
            }
        }

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

As others have said: you can use the debugger to get a better view if that is what you need. If you want to show the data in the html you can bind the object to a control, which is a much more powerful and flexible way to handle the output.

Upvotes: 1

rgz
rgz

Reputation: 362

What you are looking for is called "pretty printing". Google ".NET pprint" you might have some luck.

But really, use the debugger.

Upvotes: -3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039308

There's no such function in the framework base class library. If it is for debugging purposes you could set brake points in the code and analyze the values in your collection. If it is for printing it on the screen you could override the ToString method for each object. Yet another option would be to serialize the array to XML if this format is acceptable.

Upvotes: 1

Related Questions