C# getter vs readonly

前端 未结 9 2167
遥遥无期
遥遥无期 2020-12-29 19:23

Is there any difference between the following?

class C
{
    // One:
    public static readonly int ValueAsAMember = 42;

    // Two:
    public static int V         


        
相关标签:
9条回答
  • 2020-12-29 19:58

    readonly is nice to use on things that can only be changed in your constructor. Examples of this is typical services as interfaces when you are following the TDD pattern.

    In your example const is best, it's a constant after all.

    readonly

    const

    Cheers

    0 讨论(0)
  • 2020-12-29 19:59

    The main advantage for me is with readonly you are allowed to declare it anywhere in your code. But, you will get a chance to set it only once. With the setter, you declare and set in one stroke.

    0 讨论(0)
  • 2020-12-29 20:00

    The way I see it, using the first way describes the intention of the value better - which is that it is immutable. When a person is looking at the class' interface, he will see that the value is read-only, and won't have to wonder whether it can be changed later (since in the second case he can't see the property's implementation).

    An important thing to note about const declarations (I don't believe it's true for readonly) is that changing the field's value constitutes an API change, even if you're just changing the value from 42 to 41. The reason is that for consts, the value is determined during compile time, which means that if I compile a module that uses your constant, and you later change it, I will still be using the old value until I recompile my module with your new version.

    0 讨论(0)
  • 2020-12-29 20:05

    There are two major differences:

    The first is that fields cannot be on interfaces, whereas properties can. So if you want to use this in an interface, you have to use the property.

    The second, more interesting, is that readonly fields CAN be modified, while the object is being constructed. Take the following code:

    public class MyTestClass
    {
        public readonly int MyInt = 1;
    
        public MyTestClass()
        {
            MyInt = 2;
        }
    }
    

    If a caller does

    new MyTestClass().MyInt
    

    they will get 2. The same goes for static constructors for a static readonly field.

    0 讨论(0)
  • 2020-12-29 20:06

    I think the first line making something constant or rather readonly using readonly keyword.

    and the second line is making use of a property to implement readonly. Both do the same but if you compare with the IL the property would add few extra lines of code to the dll.

    0 讨论(0)
  • 2020-12-29 20:06

    Yes, there's a difference between the two.

    A readonly field can only be set in the constructor.

    A {get; private set;} or just {get;} (for short), can be set at anytime from within the class.

    Example:

    public class Car
    {
        public readonly string Name;
    
        public string color {get; private set;}
    
        public Car()
        {
            Name = "Car";
            Color = "Red";
        }
    
        // will fail compilation
        public void ModifyName()
        {
            Name = "Subaru"
        }
    
        // perfectly ok
        public void ModifyColor()
        {
            Color = "Green"
        }
    }
    
    0 讨论(0)
提交回复
热议问题