In Pascal, you can declare multiple function arguments as a single type:
procedure TMyClass.Foo(Bar1, Bar2, Bar3 : string; Bar4, Bar5, Bar6 : Integer);
I always enjoyed this because it prevented needless repetition of type declarations. I know in C#, you can declare multiple variables as a single type:
int foo, bar;
But that doesn't appear to work for C# function arguments:
// Compiler doesn't like this because it expects types for all three arguments
public void Foo(int bar1, bar2, bar3) { }
Does C# have a way to shorthand the declaration of multiple arguments with a single type, or is there some reason it's been rejected? I can't seem to find much information on it, I just keep finding information on multiple-type arguments, which is not what I'm looking for.
In Pascal, you can declare multiple function arguments as a single type:
procedure TMyClass.Foo(Bar1, Bar2, Bar3 : string; Bar4, Bar5, Bar6 : Integer);
I always enjoyed this because it prevented needless repetition of type declarations. I know in C#, you can declare multiple variables as a single type:
int foo, bar;
But that doesn't appear to work for C# function arguments:
// Compiler doesn't like this because it expects types for all three arguments
public void Foo(int bar1, bar2, bar3) { }
Does C# have a way to shorthand the declaration of multiple arguments with a single type, or is there some reason it's been rejected? I can't seem to find much information on it, I just keep finding information on multiple-type arguments, which is not what I'm looking for.
Share Improve this question asked Nov 22, 2024 at 11:59 David RahlDavid Rahl 815 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 5No.
I guess I need more words... "No it doesn't, and is unlikely to ever do so". Reasons: because there hasn't been a compelling enough reason to add it.
Bar1, Bar2, Bar3
are related they should probably have their own type. If they are unrelated it seem weird to use the same type declaration. – JonasH Commented Nov 22, 2024 at 12:17params
keyword allowing a varying number of parameters to be passed at the call site:public void Foo(params int[] bar) { }
or use tuples:public void Foo((int, int, int) foo, (string, string, string) bar) { }
– Olivier Jacot-Descombes Commented Nov 22, 2024 at 13:49