473,669 Members | 2,420 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 3660

"Howard" <ho*******@yaho o.com> skrev i en meddelelse
news:uG******** ******@TK2MSFTN GP02.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*******@yaho o.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>(par ams 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.c om...
"Howard" <ho*******@yaho o.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.c om...
"Howard" <ho*******@yaho o.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.co m> wrote in message
news:eA******** ******@TK2MSFTN GP02.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.c om...
"Howard" <ho*******@yaho o.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*******@yaho o.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 InvalidCastExce ption.

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.Obj ect" (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
6250
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 I get this warning: Can't stat e:: Unknown file or directory (If I don't have the "use warnings" in the code I get no message.) There are folders and files on e:. I don't understand what the
0
825
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 classes. Anytime I use one of these non-native types in a signature it lists <unknown type> in the xml file. This scenario does not happen in all cases, so I am wondering if anyone knows what could cause this problem. Maybe the fact that I am using...
10
2865
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 parameters b) determine how many parameter are passed to it c) loop through parameters PS. I can't use an array or collection
9
2976
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 actually does not "know" the signature of malloc(), hence it assumes by default, that it returns int..." How can the function call of 'malloc' work at all if it is unknown?
3
2191
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 call the methods with, what the properties return etc. Does it have to be a .net dll? Thanks!
11
3648
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 "server1" part (which could be
9
4001
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 object - a variable (B) of the type "Type" which contains the type of the object in (A) (A should be of the type B) - a method which should return the object (A) as type (B)
5
1869
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 runtime. All of the examples I have seen use casting of known custom attributes to get these values. Thanks,
1
4114
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 is the code for my query for this report: SELECT .*, .QualityAssuarnce, .CallDate FROM INNER JOIN ON . = .
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8809
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.