out of all the overloads that pop up via intellisense for
console.writeline() the following one confuses me:
Console.WriteLine (string format, params object[] arg)
1) whats the deal with 'params' there? I am used to this pattern: type var,
type var, type var etc... like: string str, int i, double x. This
console.writeline overload says 'params object[] arg' ?
2) I noticed that the console.writeline( string, object, object, object,
object) overload (from the help file - not intellisense) is not CLS
compliant. It says to use console.writeline(string, object[]). I am going to
write a class to assist me in some common console output tasks and would
like to make it as generic and flexible as possible so for example, I will
not know ahead of time the number of objects I may pass to a
console.writeline call. I have mostly seen the console.writeline used like
so:
console.writeline("some text: {0}, {1}, {2}", var1, var2, var3)
I see it with varying numbers of parameters... however if I am understanding
this correctly then if I wind up passing 4 parameters like so:
console.writeline("some text: {0}, {1}, {2}, {3}", var1, var2, var3, var4)
then it matches the console.writeline( string, object, object, object,
object) overload and will NOT be CLS compliant?
2b) and does this also mean that there is a limit of 4 parameters using this
pattern? for instance the following would not work?
console.writeline("some text: {0}, {1}, {2}, {3}, {4}", var1, var2, var3,
var4, var5)
3) if my assumptions above are correct and my intent is to be able to
utilize a console.writeline method inside a class which will need to work on
varying numbers of parameters is it true then that I should use
console.writeline( string, object[])?
hopefully my questions are clear enough... their probably not.
any input would be appreciated. Thanks. 2 2792
djc <no***@nowhere.com> wrote: out of all the overloads that pop up via intellisense for console.writeline() the following one confuses me:
Console.WriteLine (string format, params object[] arg)
1) whats the deal with 'params' there? I am used to this pattern: type var, type var, type var etc... like: string str, int i, double x. This console.writeline overload says 'params object[] arg' ?
That's a parameter array. It lets you specify as many or as few
parameters as you like, and it's converted to an array by the compiler.
It's really handy :) See the C# spec for more details.
2) I noticed that the console.writeline( string, object, object, object, object) overload (from the help file - not intellisense) is not CLS compliant. It says to use console.writeline(string, object[]). I am going to write a class to assist me in some common console output tasks and would like to make it as generic and flexible as possible so for example, I will not know ahead of time the number of objects I may pass to a console.writeline call. I have mostly seen the console.writeline used like so:
console.writeline("some text: {0}, {1}, {2}", var1, var2, var3)
I see it with varying numbers of parameters... however if I am understanding this correctly then if I wind up passing 4 parameters like so:
console.writeline("some text: {0}, {1}, {2}, {3}", var1, var2, var3, var4) then it matches the console.writeline( string, object, object, object, object) overload and will NOT be CLS compliant?
Hmm... I'm not sure *why* that's not CLS-compliant.
I must admit I don't have an awful lot of experience with CLS-
compliance, but is it a big deal for you to call a non-CLS-compliant
method? In most situations CLS-compliance isn't an issue, but you might
be in a different position. (I'm not sure whether calling a non-CLS-
compliant method makes your code non-CLS-compliant - I wouldn't have
thought so, but as I say I don't know much about it.)
2b) and does this also mean that there is a limit of 4 parameters using this pattern? for instance the following would not work? console.writeline("some text: {0}, {1}, {2}, {3}, {4}", var1, var2, var3, var4, var5)
That would be compiled into a call to
WriteLine(string, params object[])
3) if my assumptions above are correct and my intent is to be able to utilize a console.writeline method inside a class which will need to work on varying numbers of parameters is it true then that I should use console.writeline( string, object[])?
Unless you *naturally* have an array, I'd just list the parameters and
let the compiler turn it into an array if it needs to.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
ok, thanks Jon. I also read about the param keyword in the help docs so
between that and your reply I think I got it.
thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m... djc <no***@nowhere.com> wrote: out of all the overloads that pop up via intellisense for console.writeline() the following one confuses me:
Console.WriteLine (string format, params object[] arg)
1) whats the deal with 'params' there? I am used to this pattern: type var, type var, type var etc... like: string str, int i, double x. This console.writeline overload says 'params object[] arg' ?
That's a parameter array. It lets you specify as many or as few parameters as you like, and it's converted to an array by the compiler. It's really handy :) See the C# spec for more details.
2) I noticed that the console.writeline( string, object, object, object, object) overload (from the help file - not intellisense) is not CLS compliant. It says to use console.writeline(string, object[]). I am going to write a class to assist me in some common console output tasks and would like to make it as generic and flexible as possible so for example, I will not know ahead of time the number of objects I may pass to a console.writeline call. I have mostly seen the console.writeline used like so:
console.writeline("some text: {0}, {1}, {2}", var1, var2, var3)
I see it with varying numbers of parameters... however if I am understanding this correctly then if I wind up passing 4 parameters like so:
console.writeline("some text: {0}, {1}, {2}, {3}", var1, var2, var3, var4) then it matches the console.writeline( string, object, object, object, object) overload and will NOT be CLS compliant?
Hmm... I'm not sure *why* that's not CLS-compliant.
I must admit I don't have an awful lot of experience with CLS- compliance, but is it a big deal for you to call a non-CLS-compliant method? In most situations CLS-compliance isn't an issue, but you might be in a different position. (I'm not sure whether calling a non-CLS- compliant method makes your code non-CLS-compliant - I wouldn't have thought so, but as I say I don't know much about it.)
2b) and does this also mean that there is a limit of 4 parameters using this pattern? for instance the following would not work? console.writeline("some text: {0}, {1}, {2}, {3}, {4}", var1, var2, var3, var4, var5)
That would be compiled into a call to WriteLine(string, params object[])
3) if my assumptions above are correct and my intent is to be able to utilize a console.writeline method inside a class which will need to work on varying numbers of parameters is it true then that I should use console.writeline( string, object[])?
Unless you *naturally* have an array, I'd just list the parameters and let the compiler turn it into an array if it needs to.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by jabailo |
last post: by
|
1 post
views
Thread by Oz |
last post: by
|
7 posts
views
Thread by shawnk |
last post: by
|
12 posts
views
Thread by newsgroupie |
last post: by
|
2 posts
views
Thread by Thomas M. |
last post: by
|
5 posts
views
Thread by Publicjoe |
last post: by
|
17 posts
views
Thread by MumboJumbo |
last post: by
|
4 posts
views
Thread by eduardorp1 |
last post: by
|
1 post
views
Thread by John Wright |
last post: by
| | | | | | | | | | |