Common method for printing arrays and lists of any types
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
add a comment |
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
add a comment |
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
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
c# debugging generics
edited Mar 11 '12 at 13:23
M.Babcock
16.3k34174
16.3k34174
asked Mar 11 '12 at 13:15
shahenshashahensha
97431939
97431939
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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"
add a comment |
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
add a comment |
public void printArray<T>(IEnumerable<T> a)
foreach(var i in a)
Console.WriteLine(i);
add a comment |
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);
add a comment |
you can make a generic method like this
public static void print<T>(T data)
foreach (T t in data)
Console.WriteLine(t.ToString());
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
add a comment |
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"
add a comment |
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"
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"
edited Feb 8 '15 at 15:04
prosseek
63.9k146445751
63.9k146445751
answered Mar 11 '12 at 13:20
KirillKirill
2,62711635
2,62711635
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 11 '12 at 13:19
M.BabcockM.Babcock
16.3k34174
16.3k34174
add a comment |
add a comment |
public void printArray<T>(IEnumerable<T> a)
foreach(var i in a)
Console.WriteLine(i);
add a comment |
public void printArray<T>(IEnumerable<T> a)
foreach(var i in a)
Console.WriteLine(i);
add a comment |
public void printArray<T>(IEnumerable<T> a)
foreach(var i in a)
Console.WriteLine(i);
public void printArray<T>(IEnumerable<T> a)
foreach(var i in a)
Console.WriteLine(i);
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
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Mar 11 '12 at 14:08
Mike CowanMike Cowan
761410
761410
add a comment |
add a comment |
you can make a generic method like this
public static void print<T>(T data)
foreach (T t in data)
Console.WriteLine(t.ToString());
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
add a comment |
you can make a generic method like this
public static void print<T>(T data)
foreach (T t in data)
Console.WriteLine(t.ToString());
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
add a comment |
you can make a generic method like this
public static void print<T>(T data)
foreach (T t in data)
Console.WriteLine(t.ToString());
you can make a generic method like this
public static void print<T>(T data)
foreach (T t in data)
Console.WriteLine(t.ToString());
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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