String are immutable in C#. Every concatenation or string function creates a new string.
Strings (C# Programming Guide)
$ - string interpolation (C# reference)
@ (C# Reference)
If the application use case requires path, name, and extension in then it makes more sense to leave these items as separate properties. Use a backing field for the FullName property so the concatenation is done once.
Properties (C# Programming Guide)
class Program
{
static void Main(string[] args)
{
File file = new File()
{
Path = @"C:\temp",
Name = "myfile",
Extension = "txt"
};
Console.WriteLine(file.FullName);
}
}
public class File
{
public string Path { get; init; }
public string Name { get; init; }
public string Extension { get; init; }
private string _fullName;
public string FullName
{
get
{
if(String.IsNullOrEmpty(_fullName))
{
_fullName = @$"{Path}\{Name}.{Extension}";
}
return _fullName;
}
}
}