By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

console.writeline overload question

P: n/a
djc
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.


Apr 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Apr 10 '06 #2

P: n/a
djc
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

Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.