admin管理员组

文章数量:1122846

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
  • 2 No. It looks ugly, sorry. – AVTUNEY Commented Nov 22, 2024 at 12:02
  • 1 Are you sure you that you should not be using some higher level type? If 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:17
  • 1 If you think you need this presumably recheck your overall coding style. You shouldn't regularly need many parameters in clean code. – Ralf Commented Nov 22, 2024 at 13:17
  • 1 Visual Basic got the best compiler: Dim i, j As Integer (here only j becomes integer) – Thomas Koelle Commented Nov 22, 2024 at 13:40
  • 1 You can also use the params 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
 |  Show 2 more comments

1 Answer 1

Reset to default 5

No.

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.

本文标签: In Ccan you declare multiple function arguments as a single typeStack Overflow