coderushforroslyn-116039-code-style-assistance-programming-style-rules.md
You can define a set of programming style rules that CodeRush applies to your code when you refactor, expand templates, and run code cleanup.
Open the Editor | C# (Visual Basic) | Programming Style options page to configure your code preferences.
You can configure the following code style aspects:
Defines whether to add visibility modifiers ( private , public , etc.) or remove them from places where they do not change code behavior.
private class MyClass // Explicit visibility modifier
{
private int myField; // Explicit visibilty modifier
int MyProperty { get; set; } // Implicit visibilty modifier
}
Friend Class C 'Explicit visibility member
Private MyField As Integer 'Explicit visibility member
Property MyProperty As String 'Implicit visibility member
Public Sub MySub()
End Sub
End Class
Use the Apply visibility style code cleanup rule.
Defines the order of a member’s modifiers.
static internal protected void HelloWorld() => Console.WriteLine("Hello World"); // Violates the default style
protected internal static void HelloWorld() => Console.WriteLine("Hello World"); // Follows the default style
This option is available for C# only because Visual Studio automatically sorts modifiers in Visual Basic.
Use the Sort modifiers code cleanup rule to change the existing code according to this style.
Defines the namespace references declaration style. The examples below show differences between options.
// Use fully qualified type names (without adding new namespace import/using references)
namespace CRRDemo {
class Person : DemoInterfaces.IPerson {
public string FullName { get; set; }
}
//...
}
// Place namespace import/using references at the top of the file
using DemoInterfaces;
namespace CRRDemo {
class Person : IPerson {
public string FullName { get; set; }
}
//...
}
// Place namespace import/using references inside the active namespace
namespace CRRDemo {
using DemoInterfaces;
class Person : IPerson {
public string FullName { get; set; }
}
//...
}
' Use fully qualified type names without adding new namespace references
Namespace CRRDemo
Class Person
Implements DemoInterfaces.IPerson
Public Property FullName() As String
End Class
'...
End Namespace
' Add namespace references to the top of the file
Imports DemoInterfaces
Namespace CRRDemo
Class Person
Implements IPerson
Public Property FullName() As String
End Class
'...
End Namespace
' Add namespace references into namespaces
Namespace CRRDemo
Imports DemoInterfaces
Class Person
Implements IPerson
Public Property FullName() As String
End Class
'...
End Namespace
Defines the style for insignificant braces around blocks with a single statement.
// Never use braces for single statements
if (statement1)
DoThings();
if (statement2)
return 42;
// Use braces only for statements which don't control program execution flow
if (statement1) {
DoThings();
}
if (statement2)
return 42;
// Always use braces for single statements
if (statement1) {
DoThings();
}
if (statement2) {
return 42;
}
Use the Apply ‘Braces in statements’ style code cleanup rule.
Defines where CodeRush should explicitly add or omit optional parenthesis.
[Foo()] // Explicit parenthesis in attributes
[Bar] // Omit parenthesis in attributes
public class C {
public int Foo { get; set; }
public static C[] Create() {
return new[] {
new C() { // Explicit parenthesis in constructor calls
Foo = (8 * 42) + 11 // Explicit parenthesis in arithmetic operators
},
new C { // Omit parenthesis in constructor calls
Foo = -3 * 42 + 11 // Omit parenthesis in arithmetic operators
}
};
}
}
<Foo()> ' Explicit parenthesis in attributes
<Bar> ' Omit parenthesis in attributes
Public Class C
Property Foo As Integer
Sub Bar()
End Sub
Sub Baz()
Dim c1 = New C() ' Explicit parenthesis in constructor calls
Dim c2 = New C()
Bar() 'Explicit parenthesis in method calls
Dim f1 = Foo() ' Explicit parenthesis in property calls
Dim f2 = Foo()
End Sub
Sub MyMethod(x As Integer, y As Integer)
Console.WriteLine((x * 5) + 3) ' Explicit parenthesis in arithmetic operators
Console.WriteLine(x * 5 + 3) ' Omit parenthesis in arithmetic operators
End Sub
End Class
Use one of the following code cleanup rules to change the existing code according to this style:
Defines the style for member attributes when attributes are applied simultaneously.
// Prefer single attribute list for all attributes
[FirstAttribute,
SecondAttribute]
void DoThings() { /* ... */}
// Put each attribute in its own attribute list
[FirstAttribute]
[SecondAttribute]
void DoThings() { /* ,., */}
Public Class TestClass
' Prefer single attribute list for all attributes
<Attribute1,
Attribute2>
Sub MySub()
End Sub
' Put each attribute in its own attribute list
<Attribute1>
<Attribute2>
Sub MyOtherSub()
End Sub
End Class
Use the Apply ‘Attribute lists’ style or Apply ‘Attribute lists’ style to assembly-level attributes code cleanup rule.
using System.Reflection;
// Prefer single attribute list for all attributes
[assembly: AssemblyTitle("Assembly Title"),
AssemblyDescription("Assembly Description"),
AssemblyConfiguration("Assembly Configuration")]
// Put each attribute in its own attribute list
[assembly: AssemblyTitle("Assembly Title")]
[assembly: AssemblyDescription("Assembly Description")]
[assembly: AssemblyConfiguration("Assembly Configuration")]
Imports System.Reflection
' Prefer single attribute list for all attributes
<Assembly: AssemblyTitle("Assembly Title"), Assembly: AssemblyDescription("Assembly Description"),
Assembly: AssemblyConfiguration("Assembly Configuration")>
' Put each attribute in its own attribute list
<Assembly: AssemblyTitle("Assembly Title")>
<Assembly: AssemblyDescription("Assembly Description")>
<Assembly: AssemblyConfiguration("Assembly Configuration")>
The following code style settings are disabled on the Editor | C# (Visual Basic) | Programming Style page of the CodeRush options dialog:
You can use the corresponding Visual Studio code style preferences and settings in the EditorConfig file to configure the code style in CodeRush:
Defines whether to use expression bodies in methods, constructors, operators, properties, indexers, accessors, lambdas, and local functions.
To change these settings in Visual Studio 2017 or later :
choose Tools | Options | Text Editor | C# | Code Style | Expression preferences.
To change these settings in Visual Studio 2015 :
open the Editor | C# | Programming Style CodeRush options page.
public class MyClass {
public MyClass() {
MyMethod();
}
// Do not use expression body in properties
public virtual string MyProperty {
get { return string.Empty; }
}
// Use expression body on a single line in accessors
public virtual string MyProperty {
get => string.Empty;
}
// Use expression body on a single line in properties
public virtual string MyProperty => string.Empty;
// Do not use expression body in methods
public virtual string MyMethod() {
return string.Empty;
}
// Use expression body in methods when possible
public virtual string MyMethod() => string.Empty;
}
}
These code style settings are used by Declaration Providers, the Declare Menu, Templates, and other features that generate new members.
You can also use the Apply expression body styles code cleanup rule.
Applies the Visual Studio namespace declaration preferences and corresponding settings from the .editorconfig file in code cleanup. This preference defines file-scoped or block-scoped namespace body styles for C# 10.
using System;
namespace MyNamespace // Block-scoped namespace body style
{
public class MyClass
{
}
}
using System;
namespace MyNamespace; // File-scoped namespace body style
public class MyClass
{
}
To change this setting in Visual Studio: choose Tools | Options | Text Editor | C# | Code Style | General | Code block preferences: namespace declarations.
To modify the existing code according to this style:
Defines whether to use explicitly-typed or implicitly-typed variable declarations.
var T = new Temperature(); // Prefer 'var'
Temperature T = new Temperature(); // Prefer explicit type
Use the Apply variable declaration style code cleanup rule to change the existing code according to this style.
Defines whether to use CLR types and/or their keyword aliases.
int ID; // Prefer predefined type
Int32 ID; // Prefer framework type
Dim ID As Integer ' VB keyword
Dim ID As Int32 ' CLR type
Use the Apply built-in type style code cleanup rule.
Defines whether to add or remove the ‘this.’/‘Me.’ Qualifier in places where it does not change code behavior.
// Use 'this' qualifier for properties
public class Temperature {
public double Celsius {
get { return (this.Fahrenheit - 32) * (5.0 / 9); }
set { this.Fahrenheit = (value * 9.0 / 5) + 32; }
}
public double Fahrenheit { get; set; }
}
// Do not use 'this' qualifier for properties
public class Temperature {
public double Celsius {
get { return (Fahrenheit - 32) * (5.0 / 9); }
set { Fahrenheit = (value * 9.0 / 5) + 32; }
}
public double Fahrenheit { get; set; }
}
' Use 'Me' qualifier for properties
Public Class Temperature
Public Property Celsius() As Double
Get
Return (Me.Fahrenheit - 32) * (5.0 \ 9)
End Get
Set(ByVal value As Double)
Me.Fahrenheit = (value * 9.0 / 5) + 32
End Set
End Property
Public Property Fahrenheit() As Double
End Class
' Do not use 'Me' qualifier for properties
Public Class Temperature
Public Property Celsius() As Double
Get
Return (Fahrenheit - 32) * (5.0 \ 9)
End Get
Set(ByVal value As Double)
Fahrenheit = (value * 9.0 / 5) + 32
End Set
End Property
Public Property Fahrenheit() As Double
End Class
Use the Apply ‘this’/‘Me’ qualifier style code cleanup rule.
You can configure the “Do not replace the existing copyright header during code cleanup” option.
This option allows you to save the existing copyright header after CodeRush applies the Add copyright header code cleanup rule.
Consider the following code:
//-----------------------------------------------------------------------
// <copyright file="D:\Projects\CodeRushDemo\CodeRushDemo\Program.cs" company="My Company">
// Author: User
// Copyright (c). All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
// ...
'-----------------------------------------------------------------------
' <copyright file="D:\Projects\CodeRushDemo\CodeRushDemo\MainModule.vb" company="My Company">
' Author: User
' Copyright (c). All rights reserved.
' </copyright>
'-----------------------------------------------------------------------
Imports System
Imports System.Collections.Generic
' ...
To see the “Do not replace the existing copyright header during code cleanup” option in action, perform the following steps:
Enable the “Do not replace the existing copyright header during code cleanup” option.
Run the Add copyright header code cleanup rule.
CodeRush does not change the existing copyright header.
Refer to the following example for more information: How to Add a License to Code Files.
This option adds new Blazor ‘using’ statements to the _Imports.razor file after CodeRush applies the Extract Razor Component code provider. Disable this option to add Blazor ‘using’ statements to the original .razor file.
The following image shows the “@using BlazorApp.Components” line added to the NavMenu.razor file:
See Also