473,509 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unknown parameters

Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}
Jun 15 '06 #1
7 3656

"Howard" <ho*******@yahoo.com> skrev i en meddelelse
news:uG**************@TK2MSFTNGP02.phx.gbl...
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}


Can you use the "params" keyword?
http://msdn2.microsoft.com/en-us/library/w5zay9db.aspx
Jun 15 '06 #2
"Howard" <ho*******@yahoo.com> wrote:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}


You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Jun 15 '06 #3
You can simulate this *for a single parameter type* by using "params":

public static string MyMethod(params string[] values) {
return string.Join(",", values);
}
public static void Examples() {
Debug.WriteLine(MyMethod("a", "b", "c"));
string[] data = new string[] { "d", "e", "f" };
Debug.WriteLine(MyMethod(data));
}

Internally (to the method) "values" acts just like a string-array; the
difference is to the caller, who can now pass in *either* an array, *or* an
inline series of (correctly-typed) values.

If you can't predict the types, then "params object[]" is an option; note
that for value-types this will incur boxing; to get around this you could
use generics (in 2.0):
public static string MyMethod<T>(params T[] values) {
// do something interesting
}

Note that this use of generics won't allow you to do anything exciting (as
all we know is that T : object), but it avoids the box. If the types change
per-parameter, then object[] is the only route.

Marc
Jun 15 '06 #4
is there going to be any performance or security problems with this method?
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:lr********************************@4ax.com...
"Howard" <ho*******@yahoo.com> wrote:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}


You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/

Jun 15 '06 #5
When sending value types as objects they will be boxed. Boxing should
generally be avoided as it is slower and uses more memory resources than
using value types directly, but on the other hand it's a feature that is
very useful in some cases.

There is no security problems with this. The type information follows
the object, so there is no risk of accidentally casting a value to the
wrong type.

Howard wrote:
is there going to be any performance or security problems with this method?
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:lr********************************@4ax.com...
"Howard" <ho*******@yahoo.com> wrote:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}

You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/


Jun 15 '06 #6
Say I have something like this
string a = "123";

int number = (int)a;
int number = int.parse(a);

is the unboxing method int number = (int)a faster in this case?
thanks

"Göran Andersson" <gu***@guffa.com> wrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
When sending value types as objects they will be boxed. Boxing should
generally be avoided as it is slower and uses more memory resources than
using value types directly, but on the other hand it's a feature that is
very useful in some cases.

There is no security problems with this. The type information follows the
object, so there is no risk of accidentally casting a value to the wrong
type.

Howard wrote:
is there going to be any performance or security problems with this
method?
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:lr********************************@4ax.com...
"Howard" <ho*******@yahoo.com> wrote:

Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}
You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/


Jun 15 '06 #7
"Howard" <ho*******@yahoo.com> wrote:
Say I have something like this
string a = "123";

int number = (int)a;
int number = int.parse(a);

is the unboxing method int number = (int)a faster in this case?
thanks


This will not compile (try it). If the type of "a" was object (or if was
passed to the method I provided above), the cast would fail at runtime
with an InvalidCastException.

You can discover the type with a.GetType() or by testing "a is string"
etc.

The unboxing overhead relates to this:

object a = 4; // boxes an int

4 is a value type, native to the CPU. It isn't stored on the garbage
collected heap. All reference types are stored on the heap, however. In
order to make a "4" value fit into a reference type such as the most
general "System.Object" (object is an alias for System.Object), space
has to be made available on the heap, and the value needs to be copied
into this space. This process is called boxing. The reverse:

int i = (int) a; // where a is the object above

.... is called unboxing, and is the process of copying the value from the
heap back into a value type, where it can be optimized to (say) a CPU
register, for more efficient operations.

The overhead of this copying is the downside to the params object[]
approach. This overhead is the cost of allocating and initializing a
handful of bytes (around 8-12). In other words, it's relatively
insignificant, unless you're doing it in the middle of a very tight
loop.

-- Barry

--
http://barrkel.blogspot.com/
Jun 16 '06 #8

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

Similar topics

2
6240
by: Abe | last post by:
I have a strange Perl problem I don't understand. I've written the following program to scan different disks on a Windows server to look for directory files. Works fine until it gets to 'e:' when...
0
817
by: DavidPfahler | last post by:
I am using the xml documentation(/doc) for c++ and have noticed that the method signatures are not correctly capturing the parameters. I have defined an enumeration in my project as well as other...
10
2847
by: ndm | last post by:
Hi, Just wondering if any one knows a way to pass and enumerate an unknown number of parameter to a VB function. I want to create a Min/Max functions that can: a) take an variable number of...
9
2959
by: Christian Christmann | last post by:
Hi, I was just going through this exercise http://www.cas.mcmaster.ca/~franek/books/membook-answers/ch4/answers-ch4-3.html and I'am confused about the answer. It says: "... the compiler...
3
2182
by: Eric | last post by:
Hi, I wonder if it is possible to import and use an unknown dll in a C# program? I.e. get to know what public variables, methods, properties etc. the dll contains and get to know what parameters to...
11
3628
by: jarod1701 | last post by:
Hi, i'm currently trying to replace an unknown string using regular expressions. For example I have: user_pref("network.proxy.http", "server1") What do I have to do to replace the...
9
3990
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the...
5
1857
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
1
4093
by: zufie | last post by:
I am trying to run a report, but I get the Microsoft Visual Basic Run- Time Error '2471': Unknown I googled the error and found that the Run-Time Error 2471 points to an "Item in query". Here...
0
7234
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
7136
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...
1
7069
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
7505
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...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.