ASP.NET ,equivalent to PHP Print_r Function?

前端 未结 6 1851
忘了有多久
忘了有多久 2021-01-14 17:50

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

相关标签:
6条回答
  • 2021-01-14 18:20

    You can try:

    string[] weekDays4 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    foreach (var item in weekDays4)
    {
      Response.Write(item.ToString() + " test<br />");
    }
    
    0 讨论(0)
  • 2021-01-14 18:23

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

    But really, use the debugger.

    0 讨论(0)
  • 2021-01-14 18:23

    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.

    0 讨论(0)
  • 2021-01-14 18:33

    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.

    0 讨论(0)
  • 2021-01-14 18:36

    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 />");
                }
            }
    
    0 讨论(0)
  • 2021-01-14 18:42

    You can achieve it by JavaScriptSerializer

    var json = new JavaScriptSerializer().Serialize(yourObject);
    Response.Write("yourObject:" + json + "<br/>");
    
    0 讨论(0)
提交回复
热议问题