![](/img/trans.png)
[英]Resharper - use object initializer refactor - how to retain parentheses on constructor call?
[英]Does ReSharper have the inverse of “move initializer to constructor?”
ReSharper將提供將字段初始化程序移動到構造函數的方法:
我正在尋找一種逆轉過程的方法-將初始化從構造函數移動到字段初始化程序。
也就是說,從以下內容開始:
class C
{
readonly int i;
public C()
{
i = 42;
}
}
結尾為:
class C
{
readonly int i = 42;
public C() { }
}
ReSharper支持嗎?
是的,您可以這樣做。
為了從這個起始位置開始:
class Program
{
public Program()
{
int i = 42;
}
}
將光標放在int i = 42;
的行上int i = 42;
(您可能需要玩一些玩具,我相信只有在嘗試將光標放在i
,它才起作用),轉到Resharper- > Refactor- > Introduce Field 。
選擇字段初始化器,然后將字段設為只讀 。
結果:
class Program
{
private readonly int _i = 42;
public Program()
{
}
}
可以在此處找到更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.