Common method for printing arrays and lists of any types










12















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




























    12















    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


























      12












      12








      12


      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.4k34275




      16.4k34275










      asked Mar 11 '12 at 13:15









      shahenshashahensha

      1,00932140




      1,00932140






















          5 Answers
          5






          active

          oldest

          votes


















          40














          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









                  40














                  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





























                    40














                    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



























                      40












                      40








                      40







                      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

                      65.2k151451755




                      65.2k151451755










                      answered Mar 11 '12 at 13:20









                      KirillKirill

                      2,64211735




                      2,64211735























                          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.4k34275




                              16.4k34275





















                                  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.8k38088




                                      29.8k38088










                                      answered Mar 11 '12 at 14:04









                                      Rohit SharmaRohit Sharma

                                      2,97921433




                                      2,97921433





















                                          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

                                              761411




                                              761411





















                                                  -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

                                                  𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                  Crossroads (UK TV series)

                                                  ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế