C# 格式化選項

本文中的格式化選項僅適用於 C# 程式代碼。 這些是程式代碼樣式規則 IDE0055的選項。

新行選項

新行選項涉及使用新行來格式化程序代碼。

Editorconfig 檔案範例︰

#  CSharp formatting rules:
[*.cs]
csharp_new_line_before_open_brace = methods, properties, control_blocks, types
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

csharp_new_line_before_open_brace

此選項會考慮開啟大括號 { 應該放在與上述程式代碼相同的行上,還是放在新行上。 針對此規則,您可以指定 [全部]、[無] 或一或多個程式碼項目,例如方法屬性,來定義應於何時套用此規則。 若要指定多個程式碼項目,請使用逗號 (,) 區隔。

屬性 數值 Description
選項名稱 csharp_new_line_before_open_brace
適用語言 C#
引進的版本 Visual Studio 2017
選項值 all 要求大括弧在所有表達式的新行上(“Allman” 樣式)。
none 要求大括弧在所有表達式的同一行上 (“K&R” )
accessorsanonymous_methodsanonymous_typescontrol_blockseventsindexers
lambdas、、、methodslocal_functionsobject_collection_array_initializers、、 propertiestypes
要求大括號位於指定之程式代碼專案的新行上(“Allman” 樣式)。
默認選項值 all

程式碼範例:

// csharp_new_line_before_open_brace = all
void MyMethod()
{
    if (...)
    {
        ...
    }
}

// csharp_new_line_before_open_brace = none
void MyMethod() {
    if (...) {
        ...
    }
}

csharp_new_line_before_else

屬性 數值 Description
選項名稱 csharp_new_line_before_else
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true else 陳述式置於新行上。
false else 陳述式置於同一行上。
默認選項值 true

程式碼範例:

// csharp_new_line_before_else = true
if (...) {
    ...
}
else {
    ...
}

// csharp_new_line_before_else = false
if (...) {
    ...
} else {
    ...
}

csharp_new_line_before_catch

屬性 數值 Description
選項名稱 csharp_new_line_before_catch
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true catch 陳述式置於新行上。
false catch 陳述式置於同一行上。
默認選項值 true

程式碼範例:

// csharp_new_line_before_catch = true
try {
    ...
}
catch (Exception e) {
    ...
}

// csharp_new_line_before_catch = false
try {
    ...
} catch (Exception e) {
    ...
}

csharp_new_line_before_finally

屬性 數值 Description
選項名稱 csharp_new_line_before_finally
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 要求 finally 陳述式位於右大括號之後的新行上。
false 要求 finally 陳述式位於右大括號的同一行上。
默認選項值 true

程式碼範例:

// csharp_new_line_before_finally = true
try {
    ...
}
catch (Exception e) {
    ...
}
finally {
    ...
}

// csharp_new_line_before_finally = false
try {
    ...
} catch (Exception e) {
    ...
} finally {
    ...
}

csharp_new_line_before_members_in_object_initializers

屬性 數值 Description
選項名稱 csharp_new_line_before_members_in_object_initializers
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 要求物件初始化表達式的成員位於不同的行上
false 要求物件初始化表達式的成員位於同一行
默認選項值 true

程式碼範例:

// csharp_new_line_before_members_in_object_initializers = true
var z = new B()
{
    A = 3,
    B = 4
}

// csharp_new_line_before_members_in_object_initializers = false
var z = new B()
{
    A = 3, B = 4
}

csharp_new_line_before_members_in_anonymous_types

屬性 數值 Description
選項名稱 csharp_new_line_before_members_in_anonymous_types
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 要求匿名類型的成員位於個別行上
false 要求匿名型別的成員位於同一行
默認選項值 true

程式碼範例:

// csharp_new_line_before_members_in_anonymous_types = true
var z = new
{
    A = 3,
    B = 4
}

// csharp_new_line_before_members_in_anonymous_types = false
var z = new
{
    A = 3, B = 4
}

csharp_new_line_between_query_expression_clauses

屬性 數值 Description
選項名稱 csharp_new_line_between_query_expression_clauses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 要求查詢表達式子句的元素位於個別行
false 要求查詢表達式子句的項目位於同一行
默認選項值 true

程式碼範例:

// csharp_new_line_between_query_expression_clauses = true
var q = from a in e
        from b in e
        select a * b;

// csharp_new_line_between_query_expression_clauses = false
var q = from a in e from b in e
        select a * b;

縮排選項

縮排選項涉及使用縮排來格式化程序代碼。

Editorconfig 檔案範例︰

#  CSharp formatting rules:
[*.cs]
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents_when_block = true

csharp_indent_case_contents

屬性 數值 Description
選項名稱 csharp_indent_case_contents
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true switch 案例內容進行縮排
false 不對 switch 案例內容進行縮排
默認選項值 true

程式碼範例:

// csharp_indent_case_contents = true
switch(c) {
    case Color.Red:
        Console.WriteLine("The color is red");
        break;
    case Color.Blue:
        Console.WriteLine("The color is blue");
        break;
    default:
        Console.WriteLine("The color is unknown.");
        break;
}

// csharp_indent_case_contents = false
switch(c) {
    case Color.Red:
    Console.WriteLine("The color is red");
    break;
    case Color.Blue:
    Console.WriteLine("The color is blue");
    break;
    default:
    Console.WriteLine("The color is unknown.");
    break;
}

csharp_indent_switch_labels

屬性 數值 Description
選項名稱 csharp_indent_switch_labels
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 縮排 switch 標籤
false 不要縮排 switch 標籤
默認選項值 true

程式碼範例:

// csharp_indent_switch_labels = true
switch(c) {
    case Color.Red:
        Console.WriteLine("The color is red");
        break;
    case Color.Blue:
        Console.WriteLine("The color is blue");
        break;
    default:
        Console.WriteLine("The color is unknown.");
        break;
}

// csharp_indent_switch_labels = false
switch(c) {
case Color.Red:
    Console.WriteLine("The color is red");
    break;
case Color.Blue:
    Console.WriteLine("The color is blue");
    break;
default:
    Console.WriteLine("The color is unknown.");
    break;
}

csharp_indent_labels

屬性 數值 Description
選項名稱 csharp_indent_labels
適用語言 C#
引進的版本 Visual Studio 2017
選項值 flush_left 標籤放在最左邊的資料行
one_less_than_current 將標籤置於比目前內容的縮排少一個單位的位置
no_change 將標籤置於和目前內容相同縮排的位置
默認選項值 one_less_than_current

程式碼範例:

// csharp_indent_labels= flush_left
class C
{
    private string MyMethod(...)
    {
        if (...) {
            goto error;
        }
error:
        throw new Exception(...);
    }
}

// csharp_indent_labels = one_less_than_current
class C
{
    private string MyMethod(...)
    {
        if (...) {
            goto error;
        }
    error:
        throw new Exception(...);
    }
}

// csharp_indent_labels= no_change
class C
{
    private string MyMethod(...)
    {
        if (...) {
            goto error;
        }
        error:
        throw new Exception(...);
    }
}

csharp_indent_block_contents

屬性 數值 Description
選項名稱 csharp_indent_block_contents
適用語言 C#
選項值 true 縮排區塊內容。
false 請勿縮排區塊內容。
默認選項值 true

程式碼範例:

// csharp_indent_block_contents = true
static void Hello()
{
    Console.WriteLine("Hello");
}

// csharp_indent_block_contents = false
static void Hello()
{
Console.WriteLine("Hello");
}

csharp_indent_braces

屬性 數值 Description
選項名稱 csharp_indent_braces
適用語言 C#
選項值 true 縮排大括弧。
false 不要縮排大括號。
默認選項值 false

程式碼範例:

// csharp_indent_braces = true
static void Hello()
    {
    Console.WriteLine("Hello");
    }

// csharp_indent_braces = false
static void Hello()
{
    Console.WriteLine("Hello");
}

csharp_indent_case_contents_when_block

屬性 數值 Description
選項名稱 csharp_indent_case_contents_when_block
適用語言 C#
選項值 true 如果是區塊,請在 switch 語句中縮排語句清單和大括弧。
false 當它是區塊時,請勿在 switch 語句中縮排語句清單和大括弧。
默認選項值 true

程式碼範例:

// csharp_indent_case_contents_when_block = true
case 0:
    {
        Console.WriteLine("Hello");
        break;
    }

// csharp_indent_case_contents_when_block = false
case 0:
{
    Console.WriteLine("Hello");
    break;
}

間距選項

間距選項涉及使用空格字元來格式化程序代碼。

Editorconfig 檔案範例︰

#  CSharp formatting rules:
[*.cs]
csharp_space_after_cast = true
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_parentheses = control_flow_statements, type_casts
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_parameter_list_parentheses = true
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = true
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_after_comma = true
csharp_space_before_comma = false
csharp_space_after_dot = false
csharp_space_before_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_before_semicolon_in_for_statement = false
csharp_space_around_declaration_statements = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_square_brackets = false

csharp_space_after_cast

屬性 數值 Description
選項名稱 csharp_space_after_cast
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 在轉換和值之間放置空格符
false 拿掉轉換與值之間的空間
默認選項值 false

程式碼範例:

// csharp_space_after_cast = true
int y = (int) x;

// csharp_space_after_cast = false
int y = (int)x;

csharp_space_after_keywords_in_control_flow_statements

屬性 數值 Description
選項名稱 csharp_space_after_keywords_in_control_flow_statements
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將空格元放在控制流程語句中的關鍵詞後面,例如 for 迴圈
false 在控制流程語句中的關鍵詞後面移除空格,例如 for 迴圈
默認選項值 true

程式碼範例:

// csharp_space_after_keywords_in_control_flow_statements = true
for (int i;i<x;i++) { ... }

// csharp_space_after_keywords_in_control_flow_statements = false
for(int i;i<x;i++) { ... }

csharp_space_between_parentheses

屬性 數值 Description
選項名稱 csharp_space_between_parentheses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 control_flow_statements 在控制流程陳述式的括號之間加入空格
expressions 在運算式的括號之間加入空格
type_casts 在類型轉換中的括號之間加入空格
false (或任何其他值) 絕對不要在括弧之間新增空格

如果您略過此規則,或使用 control_flow_statementsexpressionstype_casts 以外的值,即不套用設定。

程式碼範例:

// csharp_space_between_parentheses = control_flow_statements
for ( int i = 0; i < 10; i++ ) { }

// csharp_space_between_parentheses = expressions
var z = ( x * y ) - ( ( y - x ) * 3 );

// csharp_space_between_parentheses = type_casts
int y = ( int )x;

// csharp_space_between_parentheses = false
int y = (int)x;

csharp_space_before_colon_in_inheritance_clause

屬性 數值 Description
選項名稱 csharp_space_before_colon_in_inheritance_clause
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將空格元放在類型宣告中基底或介面的冒號之前
false 拿掉類型宣告中基底或介面冒號之前的空格
默認選項值 true

程式碼範例:

// csharp_space_before_colon_in_inheritance_clause = true
interface I
{

}

class C : I
{

}

// csharp_space_before_colon_in_inheritance_clause = false
interface I
{

}

class C: I
{

}

csharp_space_after_colon_in_inheritance_clause

屬性 數值 Description
選項名稱 csharp_space_after_colon_in_inheritance_clause
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將空格元放在類型宣告中基底或介面的冒號後面
false 在類型宣告中基底或介面的冒號後面移除空格
默認選項值 true

程式碼範例:

// csharp_space_after_colon_in_inheritance_clause = true
interface I
{

}

class C : I
{

}

// csharp_space_after_colon_in_inheritance_clause = false
interface I
{

}

class C :I
{

}

csharp_space_around_binary_operators

屬性 數值 Description
選項名稱 csharp_space_around_binary_operators
適用語言 C#
引進的版本 Visual Studio 2017
選項值 before_and_after 在二元運算子前後插入空格
none 移除二元運算子前後的空格
ignore 忽略二元運算子前後的空格
默認選項值 before_and_after

程式碼範例:

// csharp_space_around_binary_operators = before_and_after
return x * (x - y);

// csharp_space_around_binary_operators = none
return x*(x-y);

// csharp_space_around_binary_operators = ignore
return x  *  (x-y);

csharp_space_between_method_declaration_parameter_list_parentheses

屬性 數值 Description
選項名稱 csharp_space_between_method_declaration_parameter_list_parentheses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將空格字元放在左括號和方法宣告參數清單的右括弧之前
false 拿掉左括弧和方法宣告參數清單右括弧之前的空間字元
默認選項值 false

程式碼範例:

// csharp_space_between_method_declaration_parameter_list_parentheses = true
void Bark( int x ) { ... }

// csharp_space_between_method_declaration_parameter_list_parentheses = false
void Bark(int x) { ... }

csharp_space_between_method_declaration_empty_parameter_list_parentheses

屬性 數值 Description
選項名稱 csharp_space_between_method_declaration_empty_parameter_list_parentheses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 在方法宣告的空白參數清單括弧內插入空格
false 拿掉方法宣告之空白參數清單括弧內的空格
默認選項值 false

程式碼範例:

// csharp_space_between_method_declaration_empty_parameter_list_parentheses = true
void Goo( )
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

// csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_between_method_declaration_name_and_open_parenthesis

屬性 數值 Description
選項名稱 csharp_space_between_method_declaration_name_and_open_parenthesis
適用語言 C#
選項值 true 在方法宣告中放置方法名稱與左括弧之間的空格字元
false 拿掉方法名稱與方法宣告中左括弧之間的空格字元
默認選項值 false

程式碼範例:

// csharp_space_between_method_declaration_name_and_open_parenthesis = true
void M () { }

// csharp_space_between_method_declaration_name_and_open_parenthesis = false
void M() { }

csharp_space_between_method_call_parameter_list_parentheses

屬性 數值 Description
選項名稱 csharp_space_between_method_call_parameter_list_parentheses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將空格符放在左括弧和方法呼叫的右括弧之前
false 在方法呼叫的左括號和右括弧之前移除空格字元
默認選項值 false

程式碼範例:

// csharp_space_between_method_call_parameter_list_parentheses = true
MyMethod( argument );

// csharp_space_between_method_call_parameter_list_parentheses = false
MyMethod(argument);

csharp_space_between_method_call_empty_parameter_list_parentheses

屬性 數值 Description
選項名稱 csharp_space_between_method_call_empty_parameter_list_parentheses
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 在空白引數清單括號內插入空格
false 拿掉空白自變數清單括弧內的空格
默認選項值 false

程式碼範例:

// csharp_space_between_method_call_empty_parameter_list_parentheses = true
void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo( );
}

// csharp_space_between_method_call_empty_parameter_list_parentheses = false
void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_between_method_call_name_and_opening_parenthesis

屬性 數值 Description
選項名稱 csharp_space_between_method_call_name_and_opening_parenthesis
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 在方法呼叫名稱與左括弧之間插入空格
false 拿掉方法呼叫名稱與左括號之間的空格
默認選項值 false

程式碼範例:

// csharp_space_between_method_call_name_and_opening_parenthesis = true
void Goo()
{
    Goo (1);
}

void Goo(int x)
{
    Goo ();
}

// csharp_space_between_method_call_name_and_opening_parenthesis = false
void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_after_comma

屬性 數值 Description
選項名稱 csharp_space_after_comma
適用語言 C#
選項值 true 在逗號後面插入空格
false 拿掉逗號後面的空格
默認選項值 true

程式碼範例:

// csharp_space_after_comma = true
int[] x = new int[] { 1, 2, 3, 4, 5 };

// csharp_space_after_comma = false
int[] x = new int[] { 1,2,3,4,5 };

csharp_space_before_comma

屬性 數值 Description
選項名稱 csharp_space_before_comma
適用語言 C#
選項值 true 在逗號之前插入空格
false 拿掉逗號之前的空格
默認選項值 false

程式碼範例:

// csharp_space_before_comma = true
int[] x = new int[] { 1 , 2 , 3 , 4 , 5 };

// csharp_space_before_comma = false
int[] x = new int[] { 1, 2, 3, 4, 5 };

csharp_space_after_dot

屬性 數值 Description
選項名稱 csharp_space_after_dot
適用語言 C#
選項值 true 在點之後插入空格
false 拿掉點後面的空格
默認選項值 false

程式碼範例:

// csharp_space_after_dot = true
this. Goo();

// csharp_space_after_dot = false
this.Goo();

csharp_space_before_dot

屬性 數值 Description
選項名稱 csharp_space_before_dot
適用語言 C#
選項值 true 在點之前插入空格
false 拿掉點前的空間
默認選項值 false

程式碼範例:

// csharp_space_before_dot = true
this .Goo();

// csharp_space_before_dot = false
this.Goo();

csharp_space_after_semicolon_in_for_statement

屬性 數值 Description
選項名稱 csharp_space_after_semicolon_in_for_statement
適用語言 C#
選項值 true 在語句中 for 每個分號後面插入空格
false 在語句中 for 每個分號後面移除空格
默認選項值 true

程式碼範例:

// csharp_space_after_semicolon_in_for_statement = true
for (int i = 0; i < x.Length; i++)

// csharp_space_after_semicolon_in_for_statement = false
for (int i = 0;i < x.Length;i++)

csharp_space_before_semicolon_in_for_statement

屬性 數值 Description
選項名稱 csharp_space_before_semicolon_in_for_statement
適用語言 C#
選項值 true 在語句中的每個 for 分號之前插入空格
false 拿掉語句中 for 每個分號之前的空格
默認選項值 false

程式碼範例:

// csharp_space_before_semicolon_in_for_statement = true
for (int i = 0 ; i < x.Length ; i++)

// csharp_space_before_semicolon_in_for_statement = false
for (int i = 0; i < x.Length; i++)

csharp_space_around_declaration_statements

屬性 數值 Description
選項名稱 csharp_space_around_declaration_statements
適用語言 C#
選項值 ignore 請勿移除宣告語句中的額外空格字元
false 拿掉宣告語句中的額外空格字元
默認選項值 false

程式碼範例:

// csharp_space_around_declaration_statements = ignore
int    x    =    0   ;

// csharp_space_around_declaration_statements = false
int x = 0;

csharp_space_before_open_square_brackets

屬性 數值 Description
選項名稱 csharp_space_before_open_square_brackets
適用語言 C#
選項值 true 在左方括弧之前插入空格 [
false 在左方括弧之前移除空格 [
默認選項值 false

程式碼範例:

// csharp_space_before_open_square_brackets = true
int [] numbers = new int [] { 1, 2, 3, 4, 5 };

// csharp_space_before_open_square_brackets = false
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

csharp_space_between_empty_square_brackets

屬性 數值 Description
選項名稱 csharp_space_between_empty_square_brackets
適用語言 C#
選項值 true 在空白方括弧之間插入空格 [ ]
false 拿掉空白方括弧之間的空格 []
默認選項值 false

程式碼範例:

// csharp_space_between_empty_square_brackets = true
int[ ] numbers = new int[ ] { 1, 2, 3, 4, 5 };

// csharp_space_between_empty_square_brackets = false
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

csharp_space_between_square_brackets

屬性 數值 Description
選項名稱 csharp_space_between_square_brackets
適用語言 C#
選項值 true 在非空白方括弧中插入空格字元 [ 0 ]
false 拿掉非空白方括號中的空格字元 [0]
默認選項值 false

程式碼範例:

// csharp_space_between_square_brackets = true
int index = numbers[ 0 ];

// csharp_space_between_square_brackets = false
int index = numbers[0];

包裝選項

換行格式選項涉及使用單行與語句和程式代碼區塊的個別行。

Editorconfig 檔案範例︰

#  CSharp formatting rules:
[*.cs]
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true

csharp_preserve_single_line_statements

屬性 數值 Description
選項名稱 csharp_preserve_single_line_statements
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將陳述式和成員宣告保留在同一行上
false 將陳述式和成員宣告保留在不同的行上
默認選項值 true

程式碼範例:

//csharp_preserve_single_line_statements = true
int i = 0; string name = "John";

//csharp_preserve_single_line_statements = false
int i = 0;
string name = "John";

csharp_preserve_single_line_blocks

屬性 數值 Description
選項名稱 csharp_preserve_single_line_blocks
適用語言 C#
引進的版本 Visual Studio 2017
選項值 true 將程式代碼區塊保留在單行
false 將程式代碼區塊保留在個別行上
默認選項值 true

程式碼範例:

//csharp_preserve_single_line_blocks = true
public int Foo { get; set; }

//csharp_preserve_single_line_blocks = false
public int MyProperty
{
    get; set;
}

另請參閱