Common method for printing arrays and lists of any types










11















Whenever I am debugging a piece of code which involves arrays or lists of ints, doubles, strings, etc/, I prefer printing them over sometimes. What I do for this is write overloaded printArray / printList methods for different types.



for e.g.



I may have these 3 methods for printing arrays of various types



public void printArray(int a);

public void printArray(float b);

public void printArray(String s);


Though this works for me, I still want to know whether it is possible to have a generic method which prints arrays/lists of any types. Can this also be extended to array/list of objects.










share|improve this question




























    11















    Whenever I am debugging a piece of code which involves arrays or lists of ints, doubles, strings, etc/, I prefer printing them over sometimes. What I do for this is write overloaded printArray / printList methods for different types.



    for e.g.



    I may have these 3 methods for printing arrays of various types



    public void printArray(int a);

    public void printArray(float b);

    public void printArray(String s);


    Though this works for me, I still want to know whether it is possible to have a generic method which prints arrays/lists of any types. Can this also be extended to array/list of objects.










    share|improve this question


























      11












      11








      11


      3






      Whenever I am debugging a piece of code which involves arrays or lists of ints, doubles, strings, etc/, I prefer printing them over sometimes. What I do for this is write overloaded printArray / printList methods for different types.



      for e.g.



      I may have these 3 methods for printing arrays of various types



      public void printArray(int a);

      public void printArray(float b);

      public void printArray(String s);


      Though this works for me, I still want to know whether it is possible to have a generic method which prints arrays/lists of any types. Can this also be extended to array/list of objects.










      share|improve this question
















      Whenever I am debugging a piece of code which involves arrays or lists of ints, doubles, strings, etc/, I prefer printing them over sometimes. What I do for this is write overloaded printArray / printList methods for different types.



      for e.g.



      I may have these 3 methods for printing arrays of various types



      public void printArray(int a);

      public void printArray(float b);

      public void printArray(String s);


      Though this works for me, I still want to know whether it is possible to have a generic method which prints arrays/lists of any types. Can this also be extended to array/list of objects.







      c# debugging generics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 11 '12 at 13:23









      M.Babcock

      16.3k34174




      16.3k34174










      asked Mar 11 '12 at 13:15









      shahenshashahensha

      97431939




      97431939






















          5 Answers
          5






          active

          oldest

          votes


















          39














          There is useful String.Join<T>(string separator, IEnumerable<T> values) method. You can pass array or list or any enumerable collection of any objects since objects will be converted to string by calling .ToString().



          int iarr = new int 1, 2, 3;
          Console.WriteLine(String.Join("; ", iarr)); // "1; 2; 3"
          string sarr = new string "first", "second", "third";
          Console.WriteLine(String.Join("n", sarr)); // "firstnsecondnthird"





          share|improve this answer
































            6














            Arrays and generic lists both implement IEnumerable<T> so just use it as your parameter type.



            public void PrintCollection<T>(IEnumerable<T> col)

            foreach(var item in col)
            Console.WriteLine(item); // Replace this with your version of printing






            share|improve this answer






























              1














              public void printArray<T>(IEnumerable<T> a)

              foreach(var i in a)

              Console.WriteLine(i);







              share|improve this answer
































                0














                Here's an extension method appropriate for debugging:



                [Conditional("DEBUG")]
                public static void Print<T>(this IEnumerable<T> collection)

                foreach(T item in collection)

                Console.WriteLine(item);







                share|improve this answer






























                  -1














                  you can make a generic method like this



                   public static void print<T>(T data)

                  foreach (T t in data)
                  Console.WriteLine(t.ToString());







                  share|improve this answer























                  • Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                    – Bonez024
                    Oct 17 '18 at 20:51











                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9655262%2fcommon-method-for-printing-arrays-and-lists-of-any-types%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  39














                  There is useful String.Join<T>(string separator, IEnumerable<T> values) method. You can pass array or list or any enumerable collection of any objects since objects will be converted to string by calling .ToString().



                  int iarr = new int 1, 2, 3;
                  Console.WriteLine(String.Join("; ", iarr)); // "1; 2; 3"
                  string sarr = new string "first", "second", "third";
                  Console.WriteLine(String.Join("n", sarr)); // "firstnsecondnthird"





                  share|improve this answer





























                    39














                    There is useful String.Join<T>(string separator, IEnumerable<T> values) method. You can pass array or list or any enumerable collection of any objects since objects will be converted to string by calling .ToString().



                    int iarr = new int 1, 2, 3;
                    Console.WriteLine(String.Join("; ", iarr)); // "1; 2; 3"
                    string sarr = new string "first", "second", "third";
                    Console.WriteLine(String.Join("n", sarr)); // "firstnsecondnthird"





                    share|improve this answer



























                      39












                      39








                      39







                      There is useful String.Join<T>(string separator, IEnumerable<T> values) method. You can pass array or list or any enumerable collection of any objects since objects will be converted to string by calling .ToString().



                      int iarr = new int 1, 2, 3;
                      Console.WriteLine(String.Join("; ", iarr)); // "1; 2; 3"
                      string sarr = new string "first", "second", "third";
                      Console.WriteLine(String.Join("n", sarr)); // "firstnsecondnthird"





                      share|improve this answer















                      There is useful String.Join<T>(string separator, IEnumerable<T> values) method. You can pass array or list or any enumerable collection of any objects since objects will be converted to string by calling .ToString().



                      int iarr = new int 1, 2, 3;
                      Console.WriteLine(String.Join("; ", iarr)); // "1; 2; 3"
                      string sarr = new string "first", "second", "third";
                      Console.WriteLine(String.Join("n", sarr)); // "firstnsecondnthird"






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 8 '15 at 15:04









                      prosseek

                      63.9k146445751




                      63.9k146445751










                      answered Mar 11 '12 at 13:20









                      KirillKirill

                      2,62711635




                      2,62711635























                          6














                          Arrays and generic lists both implement IEnumerable<T> so just use it as your parameter type.



                          public void PrintCollection<T>(IEnumerable<T> col)

                          foreach(var item in col)
                          Console.WriteLine(item); // Replace this with your version of printing






                          share|improve this answer



























                            6














                            Arrays and generic lists both implement IEnumerable<T> so just use it as your parameter type.



                            public void PrintCollection<T>(IEnumerable<T> col)

                            foreach(var item in col)
                            Console.WriteLine(item); // Replace this with your version of printing






                            share|improve this answer

























                              6












                              6








                              6







                              Arrays and generic lists both implement IEnumerable<T> so just use it as your parameter type.



                              public void PrintCollection<T>(IEnumerable<T> col)

                              foreach(var item in col)
                              Console.WriteLine(item); // Replace this with your version of printing






                              share|improve this answer













                              Arrays and generic lists both implement IEnumerable<T> so just use it as your parameter type.



                              public void PrintCollection<T>(IEnumerable<T> col)

                              foreach(var item in col)
                              Console.WriteLine(item); // Replace this with your version of printing







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 11 '12 at 13:19









                              M.BabcockM.Babcock

                              16.3k34174




                              16.3k34174





















                                  1














                                  public void printArray<T>(IEnumerable<T> a)

                                  foreach(var i in a)

                                  Console.WriteLine(i);







                                  share|improve this answer





























                                    1














                                    public void printArray<T>(IEnumerable<T> a)

                                    foreach(var i in a)

                                    Console.WriteLine(i);







                                    share|improve this answer



























                                      1












                                      1








                                      1







                                      public void printArray<T>(IEnumerable<T> a)

                                      foreach(var i in a)

                                      Console.WriteLine(i);







                                      share|improve this answer















                                      public void printArray<T>(IEnumerable<T> a)

                                      foreach(var i in a)

                                      Console.WriteLine(i);








                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 11 '12 at 14:28









                                      Eranga

                                      29.7k38088




                                      29.7k38088










                                      answered Mar 11 '12 at 14:04









                                      Rohit SharmaRohit Sharma

                                      2,87821433




                                      2,87821433





















                                          0














                                          Here's an extension method appropriate for debugging:



                                          [Conditional("DEBUG")]
                                          public static void Print<T>(this IEnumerable<T> collection)

                                          foreach(T item in collection)

                                          Console.WriteLine(item);







                                          share|improve this answer



























                                            0














                                            Here's an extension method appropriate for debugging:



                                            [Conditional("DEBUG")]
                                            public static void Print<T>(this IEnumerable<T> collection)

                                            foreach(T item in collection)

                                            Console.WriteLine(item);







                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              Here's an extension method appropriate for debugging:



                                              [Conditional("DEBUG")]
                                              public static void Print<T>(this IEnumerable<T> collection)

                                              foreach(T item in collection)

                                              Console.WriteLine(item);







                                              share|improve this answer













                                              Here's an extension method appropriate for debugging:



                                              [Conditional("DEBUG")]
                                              public static void Print<T>(this IEnumerable<T> collection)

                                              foreach(T item in collection)

                                              Console.WriteLine(item);








                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Mar 11 '12 at 14:08









                                              Mike CowanMike Cowan

                                              761410




                                              761410





















                                                  -1














                                                  you can make a generic method like this



                                                   public static void print<T>(T data)

                                                  foreach (T t in data)
                                                  Console.WriteLine(t.ToString());







                                                  share|improve this answer























                                                  • Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                    – Bonez024
                                                    Oct 17 '18 at 20:51
















                                                  -1














                                                  you can make a generic method like this



                                                   public static void print<T>(T data)

                                                  foreach (T t in data)
                                                  Console.WriteLine(t.ToString());







                                                  share|improve this answer























                                                  • Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                    – Bonez024
                                                    Oct 17 '18 at 20:51














                                                  -1












                                                  -1








                                                  -1







                                                  you can make a generic method like this



                                                   public static void print<T>(T data)

                                                  foreach (T t in data)
                                                  Console.WriteLine(t.ToString());







                                                  share|improve this answer













                                                  you can make a generic method like this



                                                   public static void print<T>(T data)

                                                  foreach (T t in data)
                                                  Console.WriteLine(t.ToString());








                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Mar 11 '12 at 13:51









                                                  hagohago

                                                  1,17821215




                                                  1,17821215












                                                  • Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                    – Bonez024
                                                    Oct 17 '18 at 20:51


















                                                  • Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                    – Bonez024
                                                    Oct 17 '18 at 20:51

















                                                  Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                  – Bonez024
                                                  Oct 17 '18 at 20:51






                                                  Your in-parameter T binds you to using Arrays Only. If that is your intent you could validate entry into this method with a generic constraint. In most cases it would be best to use IEnumerable as "Arrays and generic lists both implement IEnumerable<T>" as Babcock has stated above. It's also worth noting that the "ToString" within your WriteLine is typically necessary / redundant as all objects within it are converted to string regardless.

                                                  – Bonez024
                                                  Oct 17 '18 at 20:51


















                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9655262%2fcommon-method-for-printing-arrays-and-lists-of-any-types%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Darth Vader #20

                                                  How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                  Ondo