Safer C# Constructor-based Dependency Injection with readonly
Most people would have used the const keyword while doing C# development, but I had never heard of the readonly keyword before. Well, this weekend I had a friend point out this keyword and the first usage that came to mind was constructor-based dependency injection.
Here is the clarification of the differences between readonly and const from MSDN:
The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants e.g. public static readonly DateTime _creationTime = DateTime.Now;
So now I can create a class with a dependency (reference) that is injected in the constructor and I can be sure that no method in the class will change that injected dependency.
private readonly MovieFinder finder;
public MovieLister(MovieFinder finder)
this.finder = finder;
And if anyone tries to change that readonly local member variable in a method like...
public class MovieLister...
public void MethodThatWontCompile()
this.finder = null; //Error because a readonly field cannot be assigned to
...they'll get an error on compilation: 'A readonly field cannot be assigned to (except in a constructor or a variable initializer)'
So in summary, the readonly keyword provides us with a facility for ensuring that our constructor injected dependencies are not tampered with by other methods in the class.