3

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.

7
  • 2
    No. It looks ugly, sorry. 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. 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. Commented Nov 22, 2024 at 13:17
  • 1
    Visual Basic got the best compiler: Dim i, j As Integer (here only j becomes integer) 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) { } Commented Nov 22, 2024 at 13:49

1 Answer 1

6

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.

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

3 Comments

It might be worth adding that none of the C-style languages (C, C++, Java) have this. So I'm assuming it isn't in great demand really.
Even VB, which is based off Pascal, doesn't allow this
@Charlieface. Wasn't VB based on Basic (which dates from the early 1960s) and not Pascal (early 1970s). Of course plain old Basic variable naming, declaration and typing were all pretty feeble (uh, basic) back in those days

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.