c# - What does params keyword in method signature really mean -


i'm skimming through troelsen's pro c# 2010 , came across discussion of params keyword method modifier. reading text, msdn, , other tubez sources, seems me thing params ability pass comma delimited list of values method. wrote code prove myself can send arrays of different lengths method not use params keyword, , works fine.

now, i'm in favor of saving keystrokes when makes sense. in case, however, suspect savings illusory, , confusing have maintain code. illusory because i'm never going send hard-coded list of values method (bad form!). confusing because what's maintainer make of method lists bunch of values rather object explains i'm doing?

otoh, since of folks @ ms, , of folks here, smarter am, suspect i'm missing something. please, anyone, enlighten me!

thanks.

have ever used console.writeline?

console.writeline("{0}: value of {1} {2}.", linenumber, foo, bar); 

this sort of call params can useful. having explicitly construct array contain parameters noise makes code harder read without adding value. compare how if params weren't used:

console.writeline("{0}: value of {1} {2}.", new object[] { linenumber, foo, bar }); 

the second version adds no useful information code, , moves values further format string, making harder see going on.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -