Skip to main content

Can inline temporary variable

In This Article

CodeRush Classic shows the Can inline temporary variable code issue if an expression assigned to a variable is used only once.

#Fix

Inline the variable value and delete the variable.

#Purpose

Helps you to find and remove redundant variables to improve the readability of your code.

#Example

public void WriteStringToFile(string data)
{
    string fileName = "MyText.txt";
    StreamWriter sWriter = File.CreateText(fileName);
    sWriter.Write(data);
    sWriter.Dispose();
}

Fix:

public void WriteStringToFile(string data)
{
    StreamWriter sWriter = File.CreateText("MyText.txt");
    sWriter.Write(data);
    sWriter.Dispose();
}