Question

Do these statements mean the same thing?

int x { get; }
readonly int x;
Was it helpful?

Solution

In answer to your question: There is a difference between readonly and {get; }:

In int x { get; } (which won't compile as there's no way to set x - I think you needed public int x { get; private set; } ) your code can keep changing x

In readonly int x;, x is initialised either in a constructor or inline and then can never change.

OTHER TIPS

readonly int x; declares a readonly field on a class. This field can only be assigned in a constructor and it's value can't change for the lifetime of the class.

int x { get; } declares a readonly auto-implemented property and is, in this form, invalid (because you'd have no way whatsoever to set the value). A normal readonly property does not guarantee to return the same value every time it is called. The value can change throughout the lifetime of the class. For example:

public int RandomNumber
{
    get { return new Random().Next(100); }
}

This will return a different number everytime you call it. (Yes, this is a terrible abuse of properties).

No, the statements do not mean the same thing. The full version of the property will have a backing variable:

private int _x;

public int X
{
    get { return _x; }
}

Another method in the class could modify the backing variable, changing the value of the property:

private void SomeMethod(int someValue)
{
    _x = someValue * 5;
}

The readonly keyword only allows a member variable to be assigned in its declaration or in the constructor:

// Both of these compile

private readonly int _x = 1;

public SomeClass()
{
    _x = 5;
}

// This will not compile

private void SomeMethod(int someValue)
{
    _x = someValue * 5;
}

So a get-only property whose backing variable is marked readonly is a true read-only property.

Other answers are sorta outdated…

In newer versions of C# you can assign a default value to int x { get; } = 33; which changes things.

Basically, it gets compiled down to get-only property with a readonly private backing field. (See https://softwareengineering.stackexchange.com/q/372462/81745 for more details)

Another difference I see is that you can't use the readonly version when using interfaces as you can only define methods and properties.

readonly keyword is making sure that these variables dont change once initialised // it is equalant to making a variable private and setting getter for it. Example.

public class PlayerAuthData 
{
    public readonly string emailId, password, userName;
    private string hello;
    public PlayerAuthData(string emailId, string password, string userName)
    {
        this.emailId = emailId;
        this.password = password;
        this.userName = userName;
    }

    public string Hello 
    {
        get { return hello; }
        set { hello = value; }
    }
}

public class AuthManager
{
    void Start()
    {
        PlayerAuthData pad = new PlayerAuthData("a@a.com", "123123", "Mr.A");
        pad.Hello = "Hi there";
        print(pad.Hello);
        print(pad.password);
        print(pad.emailId);
        print(pad.userName);
    }
}

Literally, there's no big difference because you've declared x to be private (the default). You can always re-compile your class to make x different.

However, if it were public, the definition public int x { get; } allows you to later expand the definition to something like this:

int x { get {
     return DoSomeOperation();
    }
}

You can do that without breaking your clients. The implementation of the getter is private and clients call it without knowing if it is a static value or has an operation inside its get accessor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top