Back to Devexpress

Convert to/Decompose Initializer

coderushforroslyn-115436-refactoring-assistance-convert-to-decompose-initializer.md

latest1.5 KB
Original Source

Convert to/Decompose Initializer

  • Aug 03, 2020

Purpose

Converts a one-by-one initialization of class fields or properties to an object initializer and vice versa. The single-line initializers make your code more compact, although the one-by-one initialization can improve code readability.

Availability

Available when the cursor is on the name of the initialized object.

Usage

  1. Place the caret on the name of the initialized object.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Convert to Initializer from the menu ( Decompose Initializer if you are decomposing the initializer).

After execution, the Refactoring moves the fields/properties initialization to the object initializer or converts the inline initializer to the one-by-one initialization.

csharp
Customer Andrew = new Customer() { FirstName = "Andrew", LastName = "Fuller", Age = 48 };
vb
Dim Andrew As New Customer() With {.FirstName = "Andrew", .LastName = "Fuller", .Age = 48}

See Also

Use Expression Body

Compress to/Expand Ternary Expression

Collapse/Expand Method

ForEach to Linq