odiseh
odiseh

Reputation: 26547

How do I break lines for a method definition with many parameters in C#?

In c#, when we are writing a method which gets for example 6 parameters, and we want to break the parameters in 3 lines, how could we break the lines?

Upvotes: 8

Views: 18156

Answers (8)

Fredrik Mörk
Fredrik Mörk

Reputation: 158349

I guess you can just simply add the line breaks:

private void SomeMethod(int param1, int param2, 
                        int param3, int param4,
                        int param5, int param6)
{
    // do something
}

In C# (unlike VB.NET - at least up to now; this will change in VS2010, check "Implicit Line Continuation" about half way down on the page) you can introduce line breaks in the code pretty much anywhere. You don't need to specify that the code statement continues on the next line; that is taken care of by the syntax.

If you have a method declared as my sample above, this does not set up any requirements on how you call it. The following examples are all valid:

SomeMethod(1, 2, 3, 4, 5, 6);
SomeMethod(1, 2, 3, 4, 5,
    6);
SomeMethod(1
    , 2, 3, 4
    , 5, 6);

Upvotes: 14

Tomas
Tomas

Reputation: 3434

In C# you do not need to specify anything in particular to break a line into several on-screen lines.

So a method which would look like this in vb:

sub someMethod(param1 as String, _
    param2 as Integer, _
    param3 as Boolean)

    doSomething()

end sub

Will look like this in C#

public void someMethod(string param1,
    int param2, 
    bool param3) {

    doSomething();
}

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1502296

If you mean in terms of the layout, there are various options, such as:

Option 1:

public void Foo(int first, int second,
                int third, int fourth,
                int fifth, int sixth)

Option 2:

public void Foo(int first, int second,
    int third, int fourth,
    int fifth, int sixth)

Option 3:

public void Foo(
    int first, int second,
    int third, int fourth,
    int fifth, int sixth)

Option 4:

public void Foo
    (int first, int second,
     int third, int fourth,
     int fifth, int sixth)

Personally I like option 1, but it has two disadvantages:

  • You need to lay the parameters out again if you change the method name
  • You don't get as much space per line, because it's already indented a fair distance

Failing that, I'd generally go with option 2 or 3. Option 4 looks pretty ugly to me.

Upvotes: 6

Martin Harris
Martin Harris

Reputation: 28617

In C# you can break the lines after any parameter name (either before of after the comma). Stylecop (the Microsoft coding style guideline checker) suggests either all parameters on one line, or one per line - nothing in between. Like so:

public void Method(int param1, int param2, int param3, int param4, int param5, int param6)
{

}

public void Method(
    int param1,
    int param2,
    int param3,
    int param4,
    int param5,
    int param6)
{

}

But, there is no requirement to follow these guidelines, you can do whatever suits your internal style.

Upvotes: 21

lc.
lc.

Reputation: 116528

public void Foo(object Parameter0, object Parameter1,
    object Parameter2, object Parameter3,
    object Parameter4, object Parameter5)
{
    //do stuff
}

6 parameters / 3 lines = 2 parameters per line? Although I'd say that looks a little funny because there's extra room on each line.

Note that in C#, you can just use a line break - nothing fancy needed (VB's "_" for instance).

Upvotes: 2

Matt Howells
Matt Howells

Reputation: 41276

You can break the lines using the ENTER key, towards the right side of your keyboard.

Upvotes: 11

Lloyd Powell
Lloyd Powell

Reputation: 18800

What do you mean? like this?

public void MyMethod(int Parameter1, string Parameter2
                   , long Parameter3, float Parameter4
                   , double Parameter5, char Parameter6)
{
    //Method Functionality
}

Upvotes: 2

S M Kamran
S M Kamran

Reputation: 4503

If I understood rightly what you are saying then,,,

public void myMethod ( type parameter1, 
                       type parameter2,
                       .
                       .
                       type parameterN) 
{
     .
     .
     .
}  

Upvotes: 1

Related Questions