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

このオプションは、左中かっこ ({) を前のコードと同じ行に配置するか、新しい行に配置するかに関するものです。 このルールでは、allnone、または methodsproperties などの 1 つ以上のコード要素を指定して、このルールを適用する必要があるタイミングを定義します。 複数のコード要素を指定するには、コンマ (,) で区切ります。

プロパティ 説明
オプション名 csharp_new_line_before_open_brace
該当言語 C#
導入されたバージョン Visual Studio 2017
オプションの値 all 中かっこはすべての式の新しい行に配置する必要があります ("Allman" スタイル)。
none 中かっこはすべての式の同じ行に配置する必要があります ("K&R")。
accessorsanonymous_methodsanonymous_typescontrol_blockseventsindexers
lambdaslocal_functionsmethodsobject_collection_array_initializerspropertiestypes
中かっこは指定されたコード要素の新しい行に配置する必要があります ("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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 csharp_indent_labels
該当言語 C#
導入されたバージョン Visual Studio 2017
オプションの値 flush_left ラベルは左端の列に配置されます
one_less_than_current ラベルは、現在のコンテキストのインデントを 1 つ減らした位置に配置されます
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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 csharp_indent_case_contents_when_block
該当言語 C#
オプションの値 true ブロックの場合、switch ステートメントの case で、ステートメント リストと中かっこをインデントします。
false ブロックの場合、switch ステートメントの case で、ステートメント リストと中かっこをインデントしません。
既定のオプションの値 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 csharp_space_between_parentheses
該当言語 C#
導入されたバージョン Visual Studio 2017
オプションの値 control_flow_statements 制御フロー ステートメントのかっこの間にスペースを配置します。
expressions 式のかっこの間にスペースを配置します。
type_casts 型キャストのかっこの間にスペースを配置します。
false (またはその他の値) かっこの間にスペースを追加しない

このルールを省略するか、control_flow_statementsexpressions、または type_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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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

プロパティ 説明
オプション名 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];

折り返しオプション

折り返し書式設定オプションは、ステートメントとコード ブロックでの 1 行と別の行の使用に関するものです。

.editorconfig ファイルの例:

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

csharp_preserve_single_line_statements

プロパティ 説明
オプション名 csharp_preserve_single_line_statements
該当言語 C#
導入されたバージョン Visual Studio 2017
オプションの値 true 1 行に複数のステートメントとメンバー宣言を表示する
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

プロパティ 説明
オプション名 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;
}

関連項目