Try agent mode in VS Code!
Dismiss this update
Visual Studio Code gives you many ways to refactor your source code as well as Quick Fixes to generate code and fix issues while you're coding. To access them, click on the 'light bulb' icon that appears or use the command Quick Fix command ⌘. (Windows, Linux Ctrl+.) to display a list of Quick Fixes and refactoring options. You can also right-click the editor and select Refactor ⌃⇧R (Windows, Linux Ctrl+Shift+R) to only display refactoring options.
await
DebuggerDisplay
attributeusings
/ importsas
expressionfor
loop and foreach
statementif
and switch
statementsusing
statementif
for
statementif
statementsWhat: Adds await
keyword to a function call.
When: When you're calling a function within an asynchronous method.
How-to:
await
.What: Generate a new constructor with parameters based on selected class members.
When: You introduce a new constructor and want to properly declare it automatically with all the correct parameters.
Why: You could declare the constructor before using it, however this feature generates it automatically.
How-to:
What: The DebuggerDisplay Attribute controls how an object, property, or field is displayed in the debugger variable windows.
When: You want to pin properties within the debugger programmatically in your code.
Why: Pinning properties allows you to quickly inspect objects by their properties by bubbling up that property to the top of the object's property list within the debugger.
How-to:
DebuggerDisplay
attribute.DebuggerDisplay
attribute is added along with an auto method that returns the default ToString()
.What: Lets you automatically add an explicit cast to an expression, based on usage.
When: You need to add an explicit cast to an expression and want to properly assign it automatically.
Why: You could add an explicit cast to an expression manually, however this feature adds it automatically based on the code context.
How-to:
What: Add file headers to existing files, projects, and solutions using an EditorConfig.
When: You want to easily add a file header to files, projects, and solutions.
Why: Your team requires you to include a file header for copyright purposes.
How-to:
file_header_template
.{fileName}
as a placeholder for the file name.What: Lets you immediately add the necessary imports or using directives for copy-and-pasted code.
When: It's common practice to copy code from different places in your project or other sources and paste it in to new code. This Quick Action finds missing imports directives for copy-and-pasted code and then prompts you to add them. This code fix can also add references from project to project.
Why: Because the Quick Action automatically adds necessary imports, you don't need to manually copy the using directives that your code needs.
How-to:
What: Append a named argument to the specified parameter value in a function call.
When: If you have a method with a lot of parameters, you can add named arguments to make your code more readable.
How-to:
What: Convert an anonymous type to class.
When: You have an anonymous type that you want to continue to build on in a class.
Why: Anonymous types are useful if you're only using them locally. As your code grows, it's nice to have an easy way to promote them to a class.
How-to:
var
) type.What: Convert between an auto-implemented property to a full property.
When: The logic of the property has changed.
Why: You can convert between an auto-implemented property to a full property manually, however this feature will automatically do the work for you.
How-to:
Select Convert to full property.
Select Use auto property.
What: Convert a variable between a regular cast and a try cast using the as
keyword.
When: When you expect the cast to fail under certain scenarios (as
) or if you never expect the cast to fail (direct cast).
How-to:
Select Change to cast.
Select Change to as
expression.
What: If you have a for loop in your code, you can use this refactoring to convert it to a foreach statement.
Why: Reasons you might want to convert a for loop to a foreach statement include:
Reasons you might want to convert a foreach statement to a for loop include:
How-to:
foreach
or for
keyword.Select Convert to for
.
Select Convert to foreach
.
What: Lets you convert a Get method into a property (and optionally your Set method).
When: You have a Get method that does not contain any logic.
How-to:
What: Lets you convert a property to a Get method
When: You have a property that involves more than immediately setting and getting a value
How-to:
What: Convert an if
statement to a switch statement or to the C# 8.0 switch expression.
When: You want to convert an if
statement to a switch
statement or a switch
expression and vice versa.
Why: If you are using an if
statement, this refactoring enables an easy transition to switch statements or switch expressions.
How-to:
if
keyword.Select Convert to switch
statement.
Select Convert to switch
expression.
Select Convert to if
statement.
What: Lets you convert between regular string and verbatim string literals.
When: You either want to save space or provide more clarity in your code.
Why: Converting a verbatim string literal to a regular string literal can help save space. Converting a regular string literal to a verbatim string literal can provide more clarity.
How-to:
Select Convert to regular string.
Select Convert to verbatim string.
What: Convert your class to a C# record.
When: When you want to quickly change your class to a record, which is tailored for storing data and immutability.
How-to:
What: Convert a local function to a method.
When: You have a local function that you want to define outside your current local context.
Why: You want to convert a local function into a method so that you can call it outside your local context. You might want to convert to a method when your local function is getting too long. When you define the function in a separate method, your code is easier to read.
How-to:
What: Convert a number between a hexadecimal, binary, or decimal number.
When: Use when you want to automatically convert a number to the desired base without having to manually calculate the conversion.
How-to:
Select Convert to decimal.
Select Convert to hex.
Select Convert to binary.
What: Convert a String.Format
formatted result string (or placeholder) to an interpolated string.
When: Use when you want to an interpolated string quickly.
Why: Interpolated strings can give you a more readable version of String.Format
and can let you access your variable name directly.
How-to:
String.Format
placeholder.What: Change a regular string to an interpolated string.
When: Use when you want to clean up your code and make it more readable.
How-to:
What: Convert your tuple to a struct
When: Use when want to quickly change your tuple to a struct
and want to have fixed data you want to access multiple times.
How-to:
Place your cursor on the tuple.
Press ⌘. (Windows, Linux Ctrl+.)to trigger the Quick Actions and Refactorings menu.
Select one of the following options:
struct
-> updating usages in containing memberstruct
-> updating usages in containing typestruct
-> updating usages in containing projectstruct
-> updating usages in in dependent projectsWhat: Lets you turn a field into a property, and update all usages of that field to use the newly created property.
When: You want to move a field into a property, and update all references to that field.
Why: You want to give other classes access to a field, but don't want those classes to have direct access. By wrapping the field in a property, you could write code to verify the value being assigned, for example.
How-to:
Select Encapsulate field:
Select Encapsulate field:
What: Lets you generate Comparison operators for types that implement IComparable
.
When: You have a type that implements IComparable
we will automatically add the comparison operators.
Why: If you are implementing a value type, you should consider overriding the Equals
method to gain increased performance over the default implementation of the Equals
method on ValueType
.
How-to:
What: Lets you immediately generate the code for a new default constructor on a class.
When: You introduce a new default constructor and want to properly declare it automatically.
Why: You could declare the constructor before using it, however this feature generates it automatically.
How-to:
What: Automatically generates a method parameter.
When: You reference a variable in a method that doesn't exist in the current context and receive an error; you can generate a parameter as a code fix.
Why: You can quickly modify a method signature without losing context.
How-to:
What: Define your interface's methods explicitly in a class. An explicit interface implementation is a class member that is only called through the specified interface.
When: Use when:
How-to:
What: Define your interface's methods implicitly in a class. An implicit interface implementation is when an interface's methods and properties are directly added to the class as public methods.
How-to:
What: Inline method refactoring.
When: You want to replace usages of a static, instance, and extension method within a single statement body with an option to remove the original method declaration.
Why: This refactoring provides a clearer syntax.
How-to:
Select Inline
Select Inline and keep
What: Lets you remove a temporary variable and replace it with its value instead.
When: The use of the temporary variable makes the code harder to understand.
Why: Removing a temporary variable may make the code easier to read.
How-to:
What: Lets you immediately generate a local variable to replace an existing expression.
When: You have code that could be easily reused later if it were in a local variable.
Why: You could copy and paste the code multiple times to use it in various locations, however it would be better to perform the operation once, store the result in a local variable, and use the local variable throughout.
How-to:
Select Introduce local -> Introduce local for
Select Introduce local -> Introduce local for all occurrences of
What: Lets you immediately generate a new parameter to replace an existing expression.
When: You have code that could be easily reused later if it were in a parameter.
Why: You could copy and paste the code multiple times to use it in various locations, however it would be better to perform the operation once, store the result in a parameter, and use the parameter throughout.
How-to:
Select Introduce parameter for
Select Introduce parameter for
Select Introduce parameter for
using
statementWhat: Add a using
statement / code block to your IDisposable
instance.
When: You have an IDisposable
instance that you want to ensure is acquired, used, and disposed correctly.
How-to:
using
statement.What: Lets you invert a conditional expression or a conditional and
\ or
operator.
When: You have a conditional expression or conditional and
\ or
operator that would be better understood if inverted.
Why: Inverting an expression or conditional and
\ or
operator by hand can take much longer and possibly introduce errors. This code fix helps you do this refactoring automatically.
How-to:
and
\ or
operator.&&
with ||
What: Lets you invert an if
or if else
statement without changing the meaning of the code.
When: When you have an if
or if else
statement that would be better understood when inverted.
Why: Inverting an if
or if else
statement by hand can take much longer and possibly introduce errors. This code fix helps you do this refactoring automatically.
How-to:
if
or if else
statement.if
.What: Make a member static.
When: You want a non-static member to be static.
Why: Static members improve readability: knowing that specific code is isolated makes it easier to understand, reread, and reuse.
How-to:
What: Lets you move variable declarations closer to their usage.
When: You have variable declarations that can be in a narrower scope.
Why: You could leave it as it is, but that may cause readability issues or information hiding. This is a chance to refactor to improve readability.
How-to:
What: Lets you move the selected type to a separate file with the same name.
When: You have multiple classes, structs, interfaces, etc. in the same file that you want to separate.
Why: Placing multiple types in the same file can make it difficult to find these types. By moving types to files with the same name, code becomes more readable and easier to navigate.
How-to:
What: Lets you invert a for
statement.
When: Use when you want to reverse the meaning of a for
statement and how it iterates.
Why: Inverting a for
statement by hand can take much longer and possibly introduce errors. This code fix helps you do this refactoring automatically.
How-to:
for
statement.for
statement.What: Split or merge if
statements.
When: You want to split an if
statement that uses the &&
or ||
operators into a nested if
statement, or merge an if
statement with an outer if
statement.
Why: It's a matter of style preference.
How-to:
If you want to split the if
statement:
if
statement by the &&
or ||
operator.if
statements.If you want to merge the inner if
statement with the outer if
statement:
if
keyword.if
statement.What: Use this refactoring to replace var
in a local variable declaration with an explicit type.
Why: To improve the code's readability or when you don't want to initialize the variable in the declaration.
However, var must be used when a variable is initialized with an anonymous type and the properties of the object are accessed at a later point. For more information, see Implicitly typed local variables (C#).
How-to:
var
keyword.var
.What: Use this refactoring to replace an explicit type in a local variable declaration with var
.
Why: To fit your personal coding conventions and to have less code displayed. Var must be used when a variable is initialized with an anonymous type and the properties of the object are accessed at a later point. For more information, see Implicitly typed local variables (C#).
How-to:
What: Lets you refactor a lambda expression to use an expression body or a block body.
When: You prefer lambda expressions to use either an expression body or a block body.
Why: Lambda expressions can be refactored to improve readability according to your user preference.
How-to:
Select Use block body for lambda expressions.
Select Use expression body for lambda expressions.
What: Converts a code block to using a recursive pattern. This refactoring works with switch statements, property pattern matching, tuple pattern matching, and positional pattern matching.
When: Using recursive patterns can make your code more readable / cleaner.
How-to:
Select Convert switch
statement to expression.
Select Use recursive patterns.
What: Lets you wrap and align chains of method calls.
When: You have a long chain consisting of several method calls in one statement.
Why: Reading a long list is easier when they're wrapped or indented according to user preference.
How-to:
What: Lets you wrap, indent, and align parameters or arguments.
When: You have a method declaration or call that has multiple parameters or arguments.
Why: Reading a long list of parameters or arguments is easier when they're wrapped or indented according to user preference.
How-to:
Select Wrap every parameter -> Align wrapped parameters
Select Wrap every parameter -> Indent all parameters
Select Wrap every parameter -> Indent wrapped parameters
What: Lets you wrap binary expressions.
When: You have a binary expression.
Why: Reading a binary expression is easier when it is wrapped to user preference.
How-to: