Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 998
odiseh Avatar asked Jul 13 '09 10:07

odiseh


3 Answers

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.

like image 135
Martin Harris Avatar answered Nov 18 '22 09:11

Martin Harris


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);
like image 15
Fredrik Mörk Avatar answered Nov 18 '22 10:11

Fredrik Mörk


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

like image 9
Matt Howells Avatar answered Nov 18 '22 09:11

Matt Howells