Bob
Bob

Reputation: 217

Is there a difference between readonly and { get; }

Do these statements mean the same thing?

int x { get; }
readonly int x;

Upvotes: 19

Views: 5291

Answers (7)

Akshay
Akshay

Reputation: 7

Propery can have backing variable that can be set using any method of that class

private int a;
public int A{get;}

public void ChangeAMethod(int value){
    a=value;
}

However readonly fields can only be assigend in constructor or in-line.

Upvotes: -1

Anand Gupta
Anand Gupta

Reputation: 1

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("[email protected]", "123123", "Mr.A");
        pad.Hello = "Hi there";
        print(pad.Hello);
        print(pad.password);
        print(pad.emailId);
        print(pad.userName);
    }
}

Upvotes: 0

jeromej
jeromej

Reputation: 11588

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.

Upvotes: 3

Johannes Rudolph
Johannes Rudolph

Reputation: 35741

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).

Upvotes: 12

John Warlow
John Warlow

Reputation: 2992

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.

Upvotes: 25

Bryan Watts
Bryan Watts

Reputation: 45445

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.

Upvotes: 3

David Gladfelter
David Gladfelter

Reputation: 4213

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.

Upvotes: -1

Related Questions