in Technology by
What is Readonly members in C#.Net?

1 Answer

0 votes
by

You can apply the readonly modifier to members of a struct. It indicates that the member doesn't modify state. It's more granular than applying the readonly modifier to a struct declaration. Consider the following mutable struct:

public struct Point

{

    public double X { get; set; }

    public double Y { get; set; }

    public double Distance => Math.Sqrt(X * X + Y * Y);

    public override string ToString() =>

        $"({X}, {Y}) is {Distance} from the origin";

}

Like most structs, the ToString() method doesn't modify state. You could indicate that by adding the readonly modifier to the declaration of ToString():

public readonly override string ToString() =>

    $"({X}, {Y}) is {Distance} from the origin";

The preceding change generates a compiler warning, because ToString accesses the Distance property, which isn't marked readonly

warning CS8656: Call to non-readonly member 'Point.Distance.get' from a 'readonly' member results in an implicit copy of 'this'

The compiler warns you when it needs to create a defensive copy. The Distance property doesn't change state, so you can fix this warning by adding the readonly modifier to the declaration:

public readonly double Distance => Math.Sqrt(X * X + Y * Y);

The compiler warns you when it needs to create a defensive copy. The Distance property doesn't change state, so you can fix this warning by adding the readonly modifier to the declaration:

public readonly double Distance => Math.Sqrt(X * X + Y * Y);

Notice that the readonly modifier is necessary on a read-only property. The compiler doesn't assume get accessors don't modify state; you must declare readonly explicitly. Auto-implemented properties are an exception; the compiler will treat all auto-implemented getters as readonly, so here there's no need to add the readonly modifier to the X and Y properties.

The compiler does enforce the rule that readonly members don't modify state. The following method won't compile unless you remove the readonly modifier:

public readonly void Translate(int xOffset, int yOffset)

{

    X += xOffset;

    Y += yOffset;

}

This feature lets you specify your design intent so the compiler can enforce it, and make optimizations based on that intent.

Related questions

0 votes
    What is the difference between const and readonly in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    What is Garbage Collection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Reflection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is File Handling in C#.Net?...
asked Apr 1, 2021 in Technology by JackTerrance
0 votes
    What is Fit and finish features in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is Pattern matching enhancements in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is Top-level statements in C#.NET?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is Init only setters C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is Record types in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    What is Default interface methods in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    I'm coding a basic portfolio page in ASP.NET Core Razor Pages. I want to have a contact form at ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    How do I implement a Copy menu item in a Windows application written in C#/.NET 2.0? I want to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Is anyone using the Obout controls in C# .Net? How would you rate these controls, especially the Grid ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I have a navigation bar consisting of list items. Right at the end of the nav bar I want a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
...