JetBrains Rider 2025.2 Help

EditorConfig 属性用于 C#: 语法样式

此页面列出了自定义 JetBrains Rider EditorConfig 属性 ,您可以使用它们配置 代码语法样式规则。 请注意,某些属性适用于一种语言,而其他属性同时适用于多种语言。 但是,对于每个多语言属性,都有一个属性可以为特定语言覆盖它,例如, 默认 private 修饰符C# 默认 private 修饰符

声明中的 'var' 用法

对于内置类型

属性名称:

针对 C# 中的内置类型针对内置类型

可能的值:

  • use_var: 使用 'var'

  • use_var_when_evident: 当显而易见时使用 'var'

  • use_explicit_type: 使用显式类型

示例:

use_var

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_var_when_evident

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_explicit_type

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

对于简单类型

属性名称:

用于简单类型的 C# 表示用于简单类型

可能的值:

  • use_var: 使用 'var'

  • use_var_when_evident: 当显而易见时使用 'var'

  • use_explicit_type: 使用显式类型

示例:

use_var

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_var_when_evident

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_explicit_type

int count = 42; StringBuilder builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

其他地方

属性名称:

[resharper_]csharp_for_other_types[resharper_]for_other_types

可能的值:

  • use_var: 使用 'var'

  • use_var_when_evident: 当显而易见时使用 'var'

  • use_explicit_type: 使用显式类型

示例:

use_var

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_var_when_evident

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

use_explicit_type

int count = 42; var builder = new StringBuilder(); int length = builder.Length; Dictionary<string, string> dictionary = new Dictionary<string, string>(); bool hasValue = dictionary.TryGetValue("key", out string value);

为类型证据首选 Roslyn (Visual Studio)逻辑

属性名称:

[resharper_]csharp_use_roslyn_logic_for_evident_types[resharper_]use_roslyn_logic_for_evident_types

可能的值:

true | false

为析构变量首选单独的声明

属性名称:

[resharper_]csharp_prefer_separate_deconstructed_variables_declaration[resharper_]prefer_separate_deconstructed_variables_declaration

可能的值:

true | false

示例:

true

void ApplySetting(KeyValuePair<string, object> setting, KeyValuePair<string, object>? optionalSetting) { (var settingName, var value) = setting; // apply if (optionalSetting is (var optionalSettingName, var optionalSettingValue)) { // apply } }

false

void ApplySetting(KeyValuePair<string, object> setting, KeyValuePair<string, object>? optionalSetting) { var (settingName, value) = setting; // apply if (optionalSetting is var (optionalSettingName, optionalSettingValue)) { // apply } }

将 'var' 关键字用于弃元

属性名称:

[resharper_]csharp_prefer_explicit_discard_declaration[resharper_]prefer_explicit_discard_declaration

可能的值:

true | false

示例:

true

bool RepresentsInteger(string value) { return int.TryParse(value, out var _); }

false

bool RepresentsInteger(string value) { return int.TryParse(value, out _); }

实例成员限定

将 'this.' 限定符用于

属性名称:

使 C# 实例成员使用限定符使实例成员使用限定符

可能的值:

  • 字段

  • 属性

  • event

  • method

  • all

示例:

using System; internal class Person { public Person(string name, int age) { _id = GenerateId(); Name = name; Age = age; AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

字段

using System; internal class Person { public Person(string name, int age) { this._id = GenerateId(); Name = name; Age = age; AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

属性

using System; internal class Person { public Person(string name, int age) { _id = GenerateId(); this.Name = name; this.Age = age; AgeChanged += (_, newAge) => this.Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

event

using System; internal class Person { public Person(string name, int age) { _id = GenerateId(); Name = name; Age = age; this.AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

method

using System; internal class Person { public Person(string name, int age) { _id = this.GenerateId(); Name = name; Age = age; AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

all

using System; internal class Person { public Person(string name, int age) { this._id = this.GenerateId(); this.Name = name; this.Age = age; this.AgeChanged += (_, newAge) => this.Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

将声明的成员限定在

属性名称:

为声明于指定类型中的 C# 实例成员添加限定符为声明于指定类型中的实例成员添加限定符

可能的值:

  • this_class: 相同类

  • base_class: 基类

示例:

this_class

using System; internal class Person { public Person(string name, int age) { _id = GenerateId(); Name = name; Age = age; AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

base_class

using System; internal class Person { public Person(string name, int age) { _id = GenerateId(); Name = name; Age = age; AgeChanged += (_, newAge) => Age = newAge; } private readonly int _id; public string Name { get; } public int Age { get; private set; } public event EventHandler<int> AgeChanged; public int GenerateId() { return 42; } }

静态成员限定

使用以下类型的名称限定:

属性名称:

使用指定方式限定 C# 静态成员使用指定方式限定静态成员

可能的值:

  • current_type: 当前类型

  • declared_type: 声明类型

示例:

current_type

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(Version); GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

declared_type

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(Version); GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

要限定的成员

属性名称:

使 C# 静态成员使用成员限定符使静态成员使用成员限定符

可能的值:

  • 字段

  • 属性

  • event

  • method

  • all

示例:

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(Version); GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

字段

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(WeatherService.Name); Console.WriteLine(Version); WeatherService.GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

属性

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(WeatherService.Version); GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

event

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(Version); GotRequest += (_, _) => { }; } public static string Name = GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

method

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(Name); Console.WriteLine(Version); GotRequest += (_, _) => { }; } public static string Name = WeatherService.GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

all

using System; internal class WeatherService { static WeatherService() { Console.WriteLine(WeatherService.Name); Console.WriteLine(WeatherService.Version); WeatherService.GotRequest += (_, _) => { }; } public static string Name = WeatherService.GetString(); public static Version Version => new(1, 2, 3); public static EventHandler GotRequest; public static string GetString() { return "abc"; } }

内置类型

在局部变量、成员和形参中,首选

属性名称:

C# 内置类型引用样式内置类型引用样式

可能的值:

  • use_keyword: 关键字

  • use_clr_name: CLR 类型名称

示例:

use_keyword

void SetFont(string fontFamily, int fontSize, byte[] fontData, bool useKerning) { if (string.IsNullOrEmpty(fontFamily)) throw new ArgumentException("Invalid font family ", nameof(fontFamily)); if (int.IsNegative(fontSize)) throw new ArgumentOutOfRangeException(nameof(fontSize)); }

use_clr_name

void SetFont(string fontFamily, int fontSize, byte[] fontData, bool useKerning) { if (string.IsNullOrEmpty(fontFamily)) throw new ArgumentException("Invalid font family ", nameof(fontFamily)); if (int.IsNegative(fontSize)) throw new ArgumentOutOfRangeException(nameof(fontSize)); }

在成员访问表达式中,首选

属性名称:

C# 成员访问中内置类型引用样式成员访问中内置类型引用样式

可能的值:

  • use_keyword: 关键字

  • use_clr_name: CLR 类型名称

示例:

use_keyword

void SetFont(string fontFamily, int fontSize, byte[] fontData, bool useKerning) { if (string.IsNullOrEmpty(fontFamily)) throw new ArgumentException("Invalid font family ", nameof(fontFamily)); if (int.IsNegative(fontSize)) throw new ArgumentOutOfRangeException(nameof(fontSize)); }

use_clr_name

void SetFont(string fontFamily, int fontSize, byte[] fontData, bool useKerning) { if (string.IsNullOrEmpty(fontFamily)) throw new ArgumentException("Invalid font family ", nameof(fontFamily)); if (int.IsNegative(fontSize)) throw new ArgumentOutOfRangeException(nameof(fontSize)); }

也适用于原生大小的整数类型

属性名称:

是否在 C# 中将内置类型应用于 native integer是否将内置类型应用于 native integer

可能的值:

true | false

示例:

true

using System; internal struct Buffer { public IntPtr Pointer; public int SizeInBytes; }

false

using System; internal struct Buffer { public IntPtr Pointer; public int SizeInBytes; }

引用限定和 'using' 指令

首选完全限定引用

属性名称:

在 C# 中偏好使用限定引用偏好使用限定引用

可能的值:

true | false

将 'using' 指令添加到最深的作用域

属性名称:

将 C# 中的 using 引入添加到最深作用域将 using 引入添加到最深作用域

可能的值:

true | false

对 'using' 指令排序时先放置 'System.*' 和 'Windows.*' 命名空间

属性名称:

dotnet_sort_system_directives_first在 C# 中首先排序 System using 引入首先排序 System using 引入

可能的值:

true | false

首选在嵌套作用域内使用完全限定名称

属性名称:

在嵌套作用域中使用限定的 C# using 引入在嵌套作用域中使用限定的 using 引入

可能的值:

true | false

使用 using 别名指令解决冲突

属性名称:

C# 允许别名允许别名

可能的值:

true | false

允许使用 'global::' 前缀

属性名称:

C# 中可以使用全局别名可以使用全局别名

可能的值:

true | false

修饰符

为类型成员首选显式/隐式 private 修饰符

属性名称:

C# 默认 private 修饰符默认 private 修饰符

可能的值:

  • 显式: 显式

  • 隐式: 隐式

示例:

格式化之前

格式化后,显式

class C { int a; private int b; }
internal class C { private int a; private int b; }

格式化之前

格式化后,隐式

class C { int a; private int b; }
internal class C { int a; int b; }

为类型首选显式/隐式 internal 修饰符

属性名称:

C# 默认 internal 修饰符默认 internal 修饰符

可能的值:

  • 显式: 显式

  • 隐式: 隐式

示例:

格式化之前

格式化后,显式

namespace N { class C { } internal class D { } }
namespace N; internal class C { } internal class D { }

格式化之前

格式化后,隐式

namespace N { class C { } internal class D { } }
namespace N; class C { } class D { }

修饰符顺序

属性名称:

C# 修饰符顺序修饰符顺序

参数

跳过单一实参

属性名称:

C# 参数略过单个值参数略过单个值

可能的值:

true | false

字面量值

属性名称:

C# 参数为字面量参数为字面量

可能的值:

  • 位置式: 位置参数

  • 命名: 命名参数

示例:

格式化之前

格式化后,位置参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

格式化之前

格式化后,命名参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(x: 1, d, b: true, "abc", Fx.F1, () => Bar(x: 1)); Bar(x: 1, d, b: true, "abc", Fx.F1, action: () => Bar(x: 1)); Bar(x: 1, d, b: true, "abc", e: Fx.F1, action: () => Bar(x: 1)); Bar(x: 1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(x: 1)); Bar(x: 1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(x: 1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(x: 1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(x: 1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

字符串字面量值

属性名称:

C# 参数为字符串字面量参数为字符串字面量

可能的值:

  • 位置式: 位置参数

  • 命名: 命名参数

示例:

格式化之前

格式化后,位置参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

格式化之前

格式化后,命名参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, s: "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, s: "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

命名表达式(变量、属性、方法等)

属性名称:

C# 参数为具名参数参数为具名参数

可能的值:

  • 位置式: 位置参数

  • 命名: 命名参数

示例:

格式化之前

格式化后,位置参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

格式化之前

格式化后,命名参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, y: d, true, "abc", e: Fx.F1, () => Bar(1)); Bar(1, y: d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

匿名方法(委托和 lambda)

属性名称:

C# 参数为匿名函数参数为匿名函数

可能的值:

  • 位置式: 位置参数

  • 命名: 命名参数

示例:

格式化之前

格式化后,位置参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

格式化之前

格式化后,命名参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

其它

属性名称:

C# 其他参数形式其他参数形式

可能的值:

  • 位置式: 位置参数

  • 命名: 命名参数

示例:

格式化之前

格式化后,位置参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

格式化之前

格式化后,命名参数

public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }
public class Arguments { public void Style() { Bar(1, d, true, "abc", Fx.F1, () => Bar(1)); Bar(1, d, true, "abc", Fx.F1, action: () => Bar(1)); Bar(1, d, true, "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); Bar(x: 1, y: d, b: true, s: "abc", e: Fx.F1, action: () => Bar(1)); } public void Bar(int x, int y, bool b, string s, Fx e, Action action = null) { } private enum Fx { F1, F2, F3 } }

圆括号

移除多余的括号

属性名称:

C# 括号冗余样式括号冗余样式

可能的值:

  • 移除: 始终

  • 如未明确运算优先级则移除: 如果不明确优先级

示例:

格式化之前

格式化后,移除

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; }

格式化之前

格式化后,移除_如果不明确优先级

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = ((9 >> (12 + x - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = ((9 >> (12 + a - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; }

添加圆括号以避免不明显的优先级

以下运算的操作数周围

属性名称:

C# 非显式操作中的括号非显式操作中的括号

可能的值:
  • 乘法类运算: * / %

  • 加法类运算: + -

  • 算术运算 :*/%+-

  • Shift: << >>

  • 关系运算: < > <= >=

  • 等值运算: == !=

  • 按位与: &

  • 按位异或: ^

  • 按位或: |

  • 逐位运算: & ^ |

  • 条件与: &&

  • 条件或: ||

  • conditional: && ||

  • 空合并运算符: ??

  • range: ..

示例:

格式化之前

格式化后,无

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,乘法

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - (3 % 4) * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - (3 % 4) * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,加法

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - (3 % 4 * 12); var y = 9 >> (12 + x) - 1 << 2 + 1 & (6 + (4 * 12)) - 1; var z = (2 | 7) + 1 & y; var a = 12 - (3 % 4 * 12); var b = 9 >> (12 + a) - 1 << 2 + 1 & (6 + (4 * 12)) - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,算术

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - ((3 % 4) * 12); var y = 9 >> (12 + x) - 1 << 2 + 1 & (6 + (4 * 12)) - 1; var z = (2 | 7) + 1 & y; var a = 12 - ((3 % 4) * 12); var b = 9 >> (12 + a) - 1 << 2 + 1 & (6 + (4 * 12)) - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,移位

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = (9 >> (12 + x - 1)) << (2 + 1) & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = (9 >> (12 + a - 1)) << (2 + 1) & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,关系

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,相等

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,按位与

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = (9 >> 12 + x - 1 << 2 + 1) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = (9 >> 12 + a - 1 << 2 + 1) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,按位异或

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,按位或

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,按位

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = (9 >> 12 + x - 1 << 2 + 1) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = (9 >> 12 + a - 1 << 2 + 1) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,条件与

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = ((x > 5) && (y < 6)) || z == 7; }

格式化之前

格式化后,条件或

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || (z == 7); }

格式化之前

格式化后,条件

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = ((x > 5) && (y < 6)) || (z == 7); }

格式化之前

格式化后,空合并

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

格式化之前

格式化后,范围

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = 9 >> 12 + x - 1 << 2 + 1 & 6 + 4 * 12 - 1; var z = (2 | 7) + 1 & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = (x > 5 && y < 6) || z == 7; }

当来自以下组的运算嵌套时

属性名称:

C# 非显式操作添加括号分组非显式操作添加括号分组

可能的值:
  • 算术运算: * / % + - << >> & ^ |

  • 关系运算: < > <= >= == !=

  • conditional: && || ??

示例:

格式化之前

格式化后,无

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = ((9 >> (12 + x - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = ((9 >> (12 + a - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }

格式化之前

格式化后,算术

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }
internal void Calculate() { var x = 12 - (3 % 4 * 12); var y = ((9 >> (12 + x - 1)) << (2 + 1)) & (6 + (4 * 12) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - (3 % 4 * 12); var b = ((9 >> (12 + a - 1)) << (2 + 1)) & (6 + (4 * 12) - 1); var c = ((2 | 7) + 1) & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }

格式化之前

格式化后,关系

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = ((9 >> (12 + x - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = ((9 >> (12 + a - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; var d = x > 5 && y < 6 || z == 7; var e = (x > 6) != (y > 6) && x != null == (y != null); }

格式化之前

格式化后,条件

void Calculate() { var x = 12 - ((3 % 4) * 12); var y = ((9 >> ((12 + x) - 1)) << (2 + 1)) & ((6 + (4 * 12)) - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = 9 >> 12 + a - 1 << 2 + 1 & 6 + 4 * 12 - 1; var c = (2 | 7) + 1 & b; var d = x > 5 && y < 6 || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }
internal void Calculate() { var x = 12 - 3 % 4 * 12; var y = ((9 >> (12 + x - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var z = ((2 | 7) + 1) & y; var a = 12 - 3 % 4 * 12; var b = ((9 >> (12 + a - 1)) << (2 + 1)) & (6 + 4 * 12 - 1); var c = ((2 | 7) + 1) & b; var d = (x > 5 && y < 6) || z == 7; var e = x > 6 != y > 6 && x != null == (y != null); }

即使嵌套了相同类型的运算

属性名称:

C# 同类型操作添加括号同类型操作添加括号

可能的值:

true | false

示例:

true

internal void Calculate() { var x = 5 + 3 - 2 * 3 / 4; var e = x != null == (y != null); }

false

internal void Calculate() { var x = 5 + 3 - 2 * 3 / 4; var e = x != null == (y != null); }

大括号

在 'if' 语句中

属性名称:

[resharper_]csharp_braces_for_ifelse[resharper_]braces_for_ifelse

可能的值:

  • 不需要: 不强制

  • 两者都不需要: 如果任何部分需要大括号则强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) c = a + b; else { c = a / b; } }
public void Preview(int a, int b) { int c; if (a > b) c = a * b; else if (a == b) c = a + b; else c = a / b; }

格式化之前

格式化后,双方都不需要

public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) c = a + b; else { c = a / b; } }
public void Preview(int a, int b) { int c; if (a > b) c = a * b; else if (a == b) c = a + b; else c = a / b; }

格式化之前

格式化后,需要

public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) c = a + b; else { c = a / b; } }
public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) { c = a + b; } else { c = a / b; } }

格式化之前

格式化后,需要多行

public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) c = a + b; else { c = a / b; } }
public void Preview(int a, int b) { int c; if (a > b) c = a * b; else if (a == b) c = a + b; else c = a / b; }

格式化之前

格式化后,需要多行语句

public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) c = a + b; else { c = a / b; } }
public void Preview(int a, int b) { int c; if (a > b) { c = a * b; } else if (a == b) { c = a + b; } else { c = a / b; } }

在 'for' 语句中

属性名称:

[resharper_]csharp_braces_for_for[resharper_]braces_for_for

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) c += System.Linq.Enumerable .Range(i, a + b) .Sum(); }

格式化之前

格式化后,需要

public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) { c += System.Linq.Enumerable.Range(i, a + b).Sum(); } for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }

格式化之前

格式化后,需要多行

public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }

格式化之前

格式化后,需要多行语句

public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) c += System.Linq.Enumerable.Range(i, a + b).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; for (var i = a; i < b; i++) { c += System.Linq.Enumerable.Range(i, a + b).Sum(); } for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(i, a + b) .Sum(); } }

在 'foreach' 语句中

属性名称:

[resharper_]csharp_braces_for_foreach[resharper_]braces_for_foreach

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable .Range(a, b) .Sum(); }

格式化之前

格式化后,需要

public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable.Range(a, b).Sum(); } foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }

格式化之前

格式化后,需要多行

public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }

格式化之前

格式化后,需要多行语句

public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) c += System.Linq.Enumerable.Range(a, b).Sum(); foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { var c = 0; foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable.Range(a, b).Sum(); } foreach (var num in System.Linq.Enumerable.Range(a, b)) { c += System.Linq.Enumerable .Range(a, b) .Sum(); } }

在 'while' 语句中

属性名称:

[resharper_]csharp_braces_for_while[resharper_]braces_for_while

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) b += System.Linq.Enumerable .Range(a, b) .Sum(); }

格式化之前

格式化后,需要

public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { while (a > b) { b += System.Linq.Enumerable.Range(a, b).Sum(); } while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }

格式化之前

格式化后,需要多行

public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }

格式化之前

格式化后,需要多行语句

public void Preview(int a, int b) { while (a > b) b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }
public void Preview(int a, int b) { while (a > b) { b += System.Linq.Enumerable.Range(a, b).Sum(); } while (a > b) { b += System.Linq.Enumerable .Range(a, b) .Sum(); } }

在 'do-while' 语句中

属性名称:

[resharper_]csharp_braces_for_dowhile[resharper_]braces_for_dowhile

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }
public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do b += System.Linq.Enumerable .Range(a, b) .Sum(); while (a > b); }

格式化之前

格式化后,需要

public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }
public void Preview(int a, int b) { do { b += System.Linq.Enumerable.Range(a, b).Sum(); } while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }

格式化之前

格式化后,需要多行

public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }
public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }

格式化之前

格式化后,需要多行语句

public void Preview(int a, int b) { do b += System.Linq.Enumerable.Range(a, b).Sum(); while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }
public void Preview(int a, int b) { do { b += System.Linq.Enumerable.Range(a, b).Sum(); } while (a > b); do { b += System.Linq.Enumerable .Range(a, b) .Sum(); } while (a > b); }

在 'using' 语句中

属性名称:

[resharper_]csharp_braces_for_using[resharper_]braces_for_using

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) c += System.Linq.Enumerable .Range(1, 42) .Sum(); }

格式化之前

格式化后,需要

public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) { c += System.Linq.Enumerable.Range(1, 42).Sum(); } for (var i = a; i < b; i++) c += System.Linq.Enumerable .Range(1, 42) .Sum(); }

格式化之前

格式化后,需要多行

public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) c += System.Linq.Enumerable .Range(1, 42) .Sum(); }

格式化之前

格式化后,需要多行语句

public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) c += System.Linq.Enumerable.Range(1, 42).Sum(); for (var i = a; i < b; i++) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(System.IDisposable disposable) { var c = 0; using (disposable) { c += System.Linq.Enumerable.Range(1, 42).Sum(); } for (var i = a; i < b; i++) c += System.Linq.Enumerable .Range(1, 42) .Sum(); }

在 'lock' 语句中

属性名称:

[resharper_]csharp_braces_for_lock[resharper_]braces_for_lock

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) c += System.Linq.Enumerable .Range(1, 42) .Sum(); }

格式化之前

格式化后,需要

public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(object lockObject) { var c = 0; lock (lockObject) { c += System.Linq.Enumerable.Range(1, 42).Sum(); } lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }

格式化之前

格式化后,需要多行

public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }

格式化之前

格式化后,需要多行语句

public void Preview(object lockObject) { var c = 0; lock (lockObject) c += System.Linq.Enumerable.Range(1, 42).Sum(); lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }
public void Preview(object lockObject) { var c = 0; lock (lockObject) { c += System.Linq.Enumerable.Range(1, 42).Sum(); } lock (lockObject) { c += System.Linq.Enumerable .Range(1, 42) .Sum(); } }

在 'fixed' 语句中

属性名称:

[resharper_]csharp_braces_for_fixed[resharper_]braces_for_fixed

可能的值:

  • 不需要: 不强制

  • 必选: 始终强制

  • 多行时需要: 如果主体是多行则强制

  • 多行语句时需要: 如果语句是多行则强制

示例:

格式化之前

格式化后,不需要

public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) { p[42] = 'c'; } }
public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) p[42] = 'c'; }

格式化之前

格式化后,需要

public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) { p[42] = 'c'; } }
public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) { p[42] = 'c'; } fixed (char* p = str) { p[42] = 'c'; } }

格式化之前

格式化后,需要多行

public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) { p[42] = 'c'; } }
public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) { p[42] = 'c'; } }

格式化之前

格式化后,需要多行语句

public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) p[42] = 'c'; fixed (char* p = str) { p[42] = 'c'; } }
public unsafe void Preview(string str) { var c = 0; fixed (char* p = str) { p[42] = 'c'; } fixed (char* p = str) { p[42] = 'c'; } }

移除冗余大括号

属性名称:

[resharper_]csharp_braces_redundant[resharper_]braces_redundant

可能的值:

true | false

代码主体

方法和运算符

属性名称:

[resharper_]csharp_method_or_operator_body[resharper_]method_or_operator_body

可能的值:

  • expression_body: 表达式主体

  • block_body: 块主体

示例:

格式化之前

格式化后,表达式主体

class Preview { int Add(int a, int b) { return a + b; } void SideEffect(DB db) { Drop(db); } static bool operator true(Preview p) { return false; } }
internal class Preview { private int Add(int a, int b) => a + b; private void SideEffect(DB db) { Drop(db); } static bool operator true(Preview p) => false; }

格式化之前

格式化后,块主体

class Preview { int Add(int a, int b) { return a + b; } void SideEffect(DB db) { Drop(db); } static bool operator true(Preview p) { return false; } }
internal class Preview { private int Add(int a, int b) { return a + b; } private void SideEffect(DB db) { Drop(db); } static bool operator true(Preview p) { return false; } }

局部函数

属性名称:

[resharper_]csharp_local_function_body[resharper_]local_function_body

可能的值:

  • expression_body: 表达式主体

  • block_body: 块主体

示例:

格式化之前

格式化后,表达式主体

class Demo { public void Preview() { int CalculateTheAnswer() { return 42; } int answer = CalculateTheAnswer(); Output(answer); } }
internal class Demo { public void Preview() { int CalculateTheAnswer() => 42; int answer = CalculateTheAnswer(); Output(answer); } }

格式化之前

格式化后,块主体

class Demo { public void Preview() { int CalculateTheAnswer() { return 42; } int answer = CalculateTheAnswer(); Output(answer); } }
internal class Demo { public void Preview() { int CalculateTheAnswer() { return 42; } int answer = CalculateTheAnswer(); Output(answer); } }

构造函数和析构函数

属性名称:

[resharper_]csharp_constructor_or_destructor_body[resharper_]constructor_or_destructor_body

可能的值:

  • expression_body: 表达式主体

  • block_body: 块主体

示例:

格式化之前

格式化后,表达式主体

class Preview { public Preview(string message) { Message = message; } ~Preview() { throw new Exception(); } }
internal class Preview { public Preview(string message) => Message = message; ~Preview() => throw new Exception(); }

格式化之前

格式化后,块主体

class Preview { public Preview(string message) { Message = message; } ~Preview() { throw new Exception(); } }
internal class Preview { public Preview(string message) { Message = message; } ~Preview() { throw new Exception(); } }

属性、索引器和事件

属性名称:

[resharper_]csharp_accessor_owner_body[resharper_]accessor_owner_body

可能的值:

  • expression_body: 表达式主体

  • accessors_with_expression_body: 带表达式主体的访问器

  • accessors_with_block_body: 带块主体的访问器

示例:

格式化之前

格式化后,表达式主体

class Preview { int PropertyA { get { return field; } set { field = value; } } int PropertyB => field; int this[int i] { get { return array[i]; } set { array[i] = value; } } event EventHandler MissionComplete { add { action += value; } remove { action -= value; } } }
internal class Preview { private int PropertyA { get => field; set => field = value; } private int PropertyB => field; private int this[int i] { get => array[i]; set => array[i] = value; } private event EventHandler MissionComplete { add => action += value; remove => action -= value; } }

格式化之前

格式化后,带表达式主体的访问器

class Preview { int PropertyA { get { return field; } set { field = value; } } int PropertyB => field; int this[int i] { get { return array[i]; } set { array[i] = value; } } event EventHandler MissionComplete { add { action += value; } remove { action -= value; } } }
internal class Preview { private int PropertyA { get => field; set => field = value; } private int PropertyB { get => field; } private int this[int i] { get => array[i]; set => array[i] = value; } private event EventHandler MissionComplete { add => action += value; remove => action -= value; } }

格式化之前

格式化后,带块主体的访问器

class Preview { int PropertyA { get { return field; } set { field = value; } } int PropertyB => field; int this[int i] { get { return array[i]; } set { array[i] = value; } } event EventHandler MissionComplete { add { action += value; } remove { action -= value; } } }
internal class Preview { private int PropertyA { get { return field; } set { field = value; } } private int PropertyB { get { return field; } } private int this[int i] { get { return array[i]; } set { array[i] = value; } } private event EventHandler MissionComplete { add { action += value; } remove { action -= value; } } }

命名空间

属性名称:

[resharper_]csharp_namespace_body[resharper_]namespace_body

可能的值:

  • file_scoped: 文件范围

  • block_scoped: 块范围

示例:

格式化之前

格式化后,文件范围

namespace PreviewNamespace { public class C { } }
namespace PreviewNamespace; public class C { }

格式化之前

格式化后,块范围

namespace PreviewNamespace { public class C { } }
namespace PreviewNamespace { public class C { } }

应用样式启发

属性名称:

[resharper_]csharp_use_heuristics_for_body_style[resharper_]use_heuristics_for_body_style

可能的值:

true | false

示例:

格式化之前

格式化之后,真

class Preview { void VoidReturningMethod(DB db) { Drop(db); } int Assignment(int value) { return Property = value; } Action ReturnStatementBodiedLambda() { return () => { Foo(); }; } }
internal class Preview { private void VoidReturningMethod(DB db) { Drop(db); } private int Assignment(int value) { return Property = value; } private Action ReturnStatementBodiedLambda() { return () => { Foo(); }; } }

格式化之前

格式化之后,假

class Preview { void VoidReturningMethod(DB db) { Drop(db); } int Assignment(int value) { return Property = value; } Action ReturnStatementBodiedLambda() { return () => { Foo(); }; } }
internal class Preview { private void VoidReturningMethod(DB db) { Drop(db); } private int Assignment(int value) { return Property = value; } private Action ReturnStatementBodiedLambda() { return () => { Foo(); }; } }

属性

在部分中联接或分隔特性

属性名称:

[resharper_]csharp_force_attribute_style[resharper_]force_attribute_style

可能的值:

  • 结合: 合并

  • separate: 分隔

示例:

格式化之前

格式化后,合并

namespace N { [Attr1, Attr2] [Attr3] class C { } }
namespace N; [Attr1, Attr2, Attr3] internal class C { }

格式化之前

格式化后,分离

namespace N { [Attr1, Attr2] [Attr3] class C { } }
namespace N; [Attr1] [Attr2] [Attr3] internal class C { }

尾随逗号

多行列表中的新行前

属性名称:

[resharper_]csharp_trailing_comma_in_multiline_lists[resharper_]trailing_comma_in_multiline_lists

可能的值:

true | false

示例:

true

var myArray = new[] { item1, item2, };

false

var myArray = new[] { item1, item2 };

当最后一个元素后面不跟新行时

属性名称:

[resharper_]csharp_trailing_comma_in_singleline_lists[resharper_]trailing_comma_in_singleline_lists

可能的值:

true | false

示例:

true

var myArray = new[] { item1, item2, };

false

var myArray = new[] { item1, item2 };

对象创建

在可以从用法明显看出创建的类型时

属性名称:

[resharper_]csharp_object_creation_when_type_evident[resharper_]object_creation_when_type_evident

可能的值:

  • target_typed: 省略类型:'new()'

  • explicitly_typed: 指定类型:'new T()'

示例:

target_typed

using System; internal class MyService : ServiceBase { public override Version Version => new Version(1, 2, 3); }

explicitly_typed

using System; internal class MyService : ServiceBase { public override Version Version => new Version(1, 2, 3); }

在无法从用法明显看出创建的类型时

属性名称:

[resharper_]csharp_object_creation_when_type_not_evident[resharper_]object_creation_when_type_not_evident

可能的值:

  • target_typed: 省略类型:'new()'

  • explicitly_typed: 指定类型:'new T()'

示例:

target_typed

using System; Version GetVersion() { // many lines of code // ... return new Version(1, 2, 3); }

explicitly_typed

using System; Version GetVersion() { // many lines of code // ... return new Version(1, 2, 3); }

默认值

在可以从用法明显看出类型时

属性名称:

[resharper_]csharp_default_value_when_type_evident[resharper_]default_value_when_type_evident

可能的值:

  • default_literal: 省略类型:'default'

  • default_expression: 指定类型:'default(T)'

示例:

default_literal

using System; internal class MyService : ServiceBase { public override Guid Guid => default(Guid); }

default_expression

using System; internal class MyService : ServiceBase { public override Guid Guid => default(Guid); }

在无法从用法明显看出类型时

属性名称:

[resharper_]csharp_default_value_when_type_not_evident当类型不明显时的默认值

可能的值:

  • default_literal: 省略类型:'default'

  • default_expression: 指定类型:'default(T)'

示例:

default_literal

using System; Guid GetGuid() { // many lines of code // ... return default(Guid); }

default_expression

using System; Guid GetGuid() { // many lines of code // ... return default(Guid); }

模式

null 检查模式样式

属性名称:

C# 空值检查模式样式空值检查模式样式

可能的值:

  • empty_recursive_pattern: '{ }' 模式

  • not_null_pattern: 'not null' 模式

示例:

empty_recursive_pattern

public void Log(string? text) { if (text is not null) Console.WriteLine(text); }

not_null_pattern

public void Log(string? text) { if (text is not null) Console.WriteLine(text); }
最后修改日期: 2025年 9月 26日