console.writeline overload question

D

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.
 
J

Jon Skeet [C# MVP]

djc said:
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.
 
D

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 said:
djc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top