4

This is my code:

 private static string AddURISlash(string remotePath) { if (remotePath.LastIndexOf("/") != remotePath.Length - 1) { remotePath += "/"; } return remotePath; } 

But I need something like

AddURISlash("http://foo", "bar", "baz/", "qux", "etc/"); 

If I recall correctly, string.format is somehow like that...

String.Format("{0}.{1}.{2}.{3} at {4}", 255, 255, 255, 0, "4 p.m."); 

Is there something in C# that allows me to do so?

I know I could do

private static string AddURISlash(string[] remotePath) 

but that's not the idea.

If this is something in some framework can be done and in others not please specify and how to resolve it.

Thanks in advance

1

4 Answers 4

6

I think you want a parameter array:

private static string CreateUriFromSegments(params string[] segments) 

Then you implement it knowing that remotePath is just an array, but you can call it with:

string x = CreateUriFromSegments("http://foo.bar", "x", "y/", "z"); 

(As noted in comments, a parameter array can only appear as the last parameter in a declaration.)

Sign up to request clarification or add additional context in comments.

9 Comments

It's also important to mention that it has to be the last parameter in the method signature.
Would this allow me both writing formats or should I overload? Does params keyword exist in 1.1? CreateUriFromSegments and UriFromSegments mean the same function?
@apacay Why are you using 1.1?
I've been with C# for a while and never knew of that functionality. Thanks.
@Scott Enterprise restrictions. In some cases I'm only allowed to use either 1.1, 2.0 or 3.5
|
5

You can use params, which lets you specify any amount of arguments

private static string AddURISlash(params string[] remotePaths) { foreach (string path in remotePaths) { //do something with path } } 

Note that params will impact the performance of your code, so use it sparingly.

3 Comments

Would this allow me both writing formats (array and parameters) or should I overload? Does params keyword exist in 1.1?
You should be able to use params in 1.1 without any problem. :)
@Msonic "not string arrays" -> to call AddURISlash(new string[] { "foo", "bar", "baz" }); is valid for this signature
3

Try

private static string AddURISlash(params string[] remotePath) 

That will allow you to pass a string[] as a number of separate parameters.

Comments

3

This might be what you're looking for (note the params keyword):

private static string AddURISlash(params string[] remotePath) { // ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.