Passing params object[] args signatured method's parameters to another -same signatured- method.

  • Thread starter Thread starter The Crow
  • Start date Start date
T

The Crow

code snippet below briefly shows what i need to do :

public void Method1(params object[] args)
{
Method2(args);
}

public void Method2(params object[] args)
{
//Do something.
}


in Method1 i pass all coming parameters to Method2, but i thing it will call
Method2 such a Method2(object[] args)... to be more clear, i would access
parameters in Method2 like args[0][0], args[0][1] ..... how can i prevent
this?
 
The said:
code snippet below briefly shows what i need to do :

public void Method1(params object[] args)
{
Method2(args);
}

public void Method2(params object[] args)
{
//Do something.
}


in Method1 i pass all coming parameters to Method2, but i thing it will
call Method2 such a Method2(object[] args)... to be more clear, i would
access parameters in Method2 like args[0][0], args[0][1] ..... how can i
prevent this?

Where do you get this idea from? Why don't you just try it?


Oliver Sturm
 
The Crow said:
code snippet below briefly shows what i need to do :

public void Method1(params object[] args)
{
Method2(args);
}

public void Method2(params object[] args)
{
//Do something.
}


in Method1 i pass all coming parameters to Method2, but i thing it will
call Method2 such a Method2(object[] args)... to be more clear, i would
access parameters in Method2 like args[0][0], args[0][1] ..... how can i
prevent this?
Did you test it?
It should use the normal form of Method2 since it is applicable.

From the spec:
<quote>
The expanded form of a method is available only if the normal form of the
method
is not applicable and only if a method with the same signature as the
expanded form
is not already declared in the same type.
</quote>

To call the expanded form (i.e. to args of Method1 will be the one element
of args in Method2),
you'll have to call:
Method2((object)args);

Christof
 
ive got this idea from, thinking the type of args as a normal array. but
yes, you are right i should have tried it. it worked as expected to be.
now i wonder, what is the actual type of args? a special type?
i think c# compiler do some trick behind the scenes around there. what do
you think?
 
The Crow said:
ive got this idea from, thinking the type of args as a normal array. but
yes, you are right i should have tried it. it worked as expected to be.
now i wonder, what is the actual type of args? a special type?
i think c# compiler do some trick behind the scenes around there. what do
you think?
the type of args is object[] in both cases.
the paramarray keyword only changes the way it can be called.
If the expanded form is called c# creates an array of the given type and
put's the
parameters in it.
See my other post about when the expanded form is considered by the
compiler.
 
only public static int GetValue2(bool[] args) is called. then what is the
benefit of supplying params keyword before the paremter rather then using
bool[] args? (int this situation of course)





using System;

using System.IO;

using System.Windows.Forms;

using System.Net;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

try

{

GetValue(true, false);

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

public static int GetValue(params bool[] args)

{

return GetValue2(args);

}

public static int GetValue2(bool param1, bool param2)

{

if(param1)

return 1;

else

return 0;

}

public static int GetValue2(bool[] args)

{

if(args[0])

return 1;

else

return 0;

}

}

}
 

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

Similar Threads


Back
Top