Refactor之Rename
假如我们要把以下代码中的接口名字IBankAccount改为IBankAccount1
public interface IBankAccount
{
void PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
{
get;
}
}
// this is an extension class of IBankAccount
public class SaverAccount : IBankAccount
{
private decimal balance;
public override string ToString()
{
return "IBankAccount: SaverAccount";
}
public void PayIn(decimal amount)
{
balance += amount;
}
public bool Withdraw(decimal amount)
{
return true;
}
public decimal Balance
{
get
{
return balance;
}
}
}
那么如果按照图示步骤所示:
1. 选取要更改名字的部分
2. 通过菜单或者使用F2快捷键
3. 将新名字填写进去,并且可以根据需要选择:
“Search in comments” 在注释中也进行更改
“Search in strings” 在字符串中也进行更改
4. 点击OK后,可以得到一个预览图
5. 点击Apply后,我们的代码被改为
public interface IBankAccountNew
{
void PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
{
get;
}
}
// this is an extension class of IBankAccountNew
public class SaverAccount : IBankAccountNew
{
private decimal balance;
public override string ToString()
{
return "IBankAccountNew: SaverAccount";
}
public void PayIn(decimal amount)
{
balance += amount;
}
public bool Withdraw(decimal amount)
{
return true;
}
public decimal Balance
{
get
{
return balance;
}
}
}
注意注释和字符串中的匹配也被更改了。