The Visual Studio C # compiler warns of accidentally assigning a variable to itself , but this warning does not apply to C # properties, but only to variables. As described in this other question .
However, I would really like something similar that might warn me at compile time if I assign a property to myself.
I am currently using Visual Studio 2013, but I am fine if the solution works at least in Visual Studio 2015. In addition, I do not use third-party commercial plugins such as ReSharper or CodeRush, so I would prefer a solution that does not involve buying anything but I am open to suggestions.
Do you know how I could do this?
History:
Iβm very used to the constructor embedding pattern , using readonly public βcheckβ to save the resulting dependency.
For example, suppose a class Foo , which depends on the implementation of ILogger . A logger instance is provided to the constructor class, the constructor checks for zeros and stores the dependency in an instance property called logger :
public class Foo { public ILogger Logger { get; private set; } public Foo(ILogger logger) { if(logger == null) throw new ArgumentNullException("logger"); this.Logger = logger; } }
However, I often make an input error when assigning this property to myself, and not to the parameter passed to the constructor.
public class Foo { public ILogger Logger { get; private set; } public Foo(ILogger logger) { if(logger == null) throw new ArgumentNullException("logger"); this.Logger = Logger;
Of course, I always get these errors during testing and debugging, but it has already bitten me several times, and I no longer want to waste time on such a stupid error.
Any suggestions?