Version Used: Visual Studio 2019 version 16.1
static void GetPath()
{
DirectoryInfo root = null;
var files = GetAllMarkdownFiles(@"C:\\temp", out root);
string fullPath = Path.Combine(root.FullName, @"..\\file.txt");
}
private static object GetAllMarkdownFiles(string inputDirectory, out DirectoryInfo rootDirectory)
{
throw new NotImplementedException();
}
Put your cursor in root in the first line of the GetPath() method, then select “Inline temporary variable” from the light bulb menu.
Expected Behavior: Code that works at run time.
Actual Behavior: It generates:
static void GetPath()
{
DirectoryInfo root = null;
var files = GetAllMarkdownFiles(@"C:\\temp", out root);
string fullPath = Path.Combine(((DirectoryInfo)null).FullName, @"..\\file.txt");
}
((DirectoryInfo)null).FullName does not work.
Thanks @gewarren.
Inline temporary variable takes the variable's assigned value, plugs it in to every usage (edit: using fancy logic to make sure things still bind correctly), and flags any additional writes as conflicts. Note the red border around "root" on the second line of the diff window here (and the "X Conflict(s) detected." message):

This is sometimes useful, but probably not in this case. The more recent out <type> name argument syntax is generally preferred in this situation, and we do offer to "inline variable declaration" here to convert to that syntax. Without making more fundamental changes to inline temporary variable, I'd propose we simply choose not to offer the refactoring if we recognize this pattern.
Perhaps 'inline temporary varable' should be renamed to 'replace variable with initializer value' to better indicate what is going on?
Most helpful comment
Perhaps 'inline temporary varable' should be renamed to 'replace variable with initializer value' to better indicate what is going on?