Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this field declared as private and also readonly?

In the following code:

public class MovieRepository : IMovieRepository
{
    private readonly IHtmlDownloader _downloader;

    public MovieRepository(IHtmlDownloader downloader)
    {
        _downloader = downloader;
    }

    public Movie FindMovieById(string id)
    {
        var idUri = ...build URI...;

        var html = _downloader.DownloadHtml(idUri);

        return ...parse ID HTML...;
    }

    public Movie FindMovieByTitle(string title)
    {
        var titleUri = ...build URI...;

        var html = _downloader.DownloadHtml(titleUri);

        return ...parse title HTML...;
    }
}

I asked for something to review my code, and someone suggested this approach. My question is why is the IHtmlDownloader variable readonly?

like image 890
Sergio Tapia Avatar asked Jun 13 '10 19:06

Sergio Tapia


People also ask

Why do we use private readonly in C#?

The readonly modifier prevents the field from being replaced by a different instance of the reference type. However, the modifier doesn't prevent the instance data of the field from being modified through the read-only field.

What is the purpose of read-only variables?

Read-only variables can be used to gather information about the current template, the user who is currently logged in, or other current settings. These variables are read-only and cannot be assigned a value.

What is private static readonly in C#?

A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime.

What is readonly?

Read-only is a file attribute which only allows a user to view a file, restricting any writing to the file. Setting a file to “read-only” will still allow that file to be opened and read; however, changes such as deletions, overwrites, edits or name changes cannot be made.


3 Answers

This ensures that the value of _downloader will not be changed after the constructor was executed. Fields marked as readonly can only be assigned a value from within the constructor(s) of a class.

like image 36
M4N Avatar answered Oct 02 '22 11:10

M4N


If it's private and readonly, the benefit is that you can't inadvertently change it from another part of that class after it is initialized. The readonly modifier ensures the field can only be given a value during its initialization or in its class constructor.

If something functionally should not change after initialization, it's always good practice to use available language constructs to enforce that.

On a related note, C# 9 introduces the init accessor method for properties, which indicates the property value can only be set during object construction, e.g.:

class InitExample
{
     private double _seconds;

     public double Seconds
     {
         get { return _seconds; }
         init { _seconds = value; }
     }
}
like image 53
Eric J. Avatar answered Oct 02 '22 12:10

Eric J.


A readonly field is useful for modelling data that should not change after it has been initialized. You can assign a value to a readonly field by using a initializer when you declare it or in a constructor, but thereafter you cannot change it.

like image 2
sree lakshmi kurra Avatar answered Oct 02 '22 13:10

sree lakshmi kurra