473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Parameter Array Question

Hello,
I'm studying parameter arrays now and see what looks like a contradiction in
my book.
First it says that to pass any number of say object types to a method you
say:

int myMethod (params object [] O) {...}
//so basically you need to use the 'params' keyword.
But then later it says to pass any number of string arguements to function
main you say:

static void Main (string [] args) {...}

Did they make a typo where it should have a 'params' in there?
What if you pass it 2 string arguements? Would this one error out?
Thanks much,

Jeff
Nov 16 '05 #1
3 1441
imo the second one should not really say "args" as only one string[] is
passed. It could have been int[], object[], uint[], etc. But it is still
only one ref to an object (the array is the object which contains 0 or more
elements - the ref could also be null, but assume we know that.) You will
notice that "static void Main(string[] args)" passes command line args like
that. It does not need the params because that is the contract it defines.
It will take arguments at the cmdline and make a string[] from those. Using
params is really just a bit of sugar for the *caller to allow seperating
values via commas instead putting into an array first. Actually I don't
find a need to use params that much (if ever) because the mixmash between
the method signature and what the caller defines and you don't get the full
intellisense to explain and type each parm. Normally, you can find a way
around needing this or just use an array or other collection to begin with.
That said, people do use this as I am sure others will attest to.

The first one is little more interesting. If the caller supplies a bunch of
strings in the parms, then the clr will bunch them up into an string array
(using example below) and pass that. So it converts the arguments to one
array before the call, same as if you build the array yourself and passed
that. If the parm is not a valid type for the array or type can not be auto
converted to target type, then it will not even compile (see below.) hth

private void button50_Click(object sender, System.EventArgs e)
{
UseParams("one", "two", "three");
string[] myArray = new string[]{"four", "five"};
UseParams(myArray);
//UseParams("one", "two", "three", 4); //Does not compile.
UseParams("one", "two", "three", (4).ToString() );
UseParams2(1, 2, 3);
UseParams2((ushort)4, (ushort)5, (byte)Convert.ToUInt32("6"));
}

public static void UseParams(params string[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
Console.WriteLine();
}

public static void UseParams2(params int[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
Console.WriteLine();
}

--
William Stacey, MVP

"z_learning_tester" <so*****@microsoft.com> wrote in message
news:Sd2Bc.77193$HG.52376@attbi_s53...
Hello,
I'm studying parameter arrays now and see what looks like a contradiction in my book.
First it says that to pass any number of say object types to a method you
say:

int myMethod (params object [] O) {...}
//so basically you need to use the 'params' keyword.
But then later it says to pass any number of string arguements to function
main you say:

static void Main (string [] args) {...}

Did they make a typo where it should have a 'params' in there?
What if you pass it 2 string arguements? Would this one error out?
Thanks much,

Jeff


Nov 16 '05 #2
Hi Jeff
There is no contradiction here at all
With param keyword you can send any number of parameter of the same type
they don't have to be members or array
In a clear definition , with param you are sending a variable number of
input parameters that are of the same type

So if you have a function with this signature myfun( params string[])
You can call it this way
String one ;
String two;
String three;
myfun(one); // call with one parameter
myfun(one, two); // call with two parameters
myfun(one, two, three); // call with three parameters

but if your function were myfun( string[])
mean is has ONLY ONE input parameter , but since it is of type array and
the array is passed by referece this ONE array could have any number of
strings
so you can call it this way
String[] one ;
String[] two;
String[] three;
myfun(one); // only one input param
myfun(two)// another one input param ( but notice that the lenth of the
array one may /deffer from array two
but you CAN'T call it this way
myfun(one, two)
hope this was clear
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3
> In a clear definition , with param you are sending a variable number of
input parameters that are of the same type


If we explore this definition, I think what your really doing is just
sending an array ref to the called method, same as if "params" was not used
at all ( from perspective of the "Called" method.) The clr munges the
1-many parms into the array for the "Caller". In some ways, this no more
then some sugar.

--
William Stacey, MVP
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: petermichaux | last post by:
Hi, What does the "=array()" mean? It works with or without it in PHP5. The code was written for pre-PHP5. public function getPhrase($a_stName, $a_astValues = array()) Thanks, Peter
6
by: Dylan Nicholson | last post by:
Is there any way of declaring the parameter "array" below to be const, so that the function as written will not compile: void foo(int array /*const*/) { array = array; // should not be allowed...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
5
by: Philippe Bertrand | last post by:
Using C#, I want to send a byte array to an unmanaged function with the minimum amount of copies. The array is input only and won't be modified (its copied on the unmanaged side). I'm currently...
13
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
2
by: SM | last post by:
Hello, Such a simple question and i can't find the answer(see code below). Here it is: I have a function: ini() In that function, i call a function (load_CDThumbnail) that creates an array and...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.