Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between having a private setter OR only defining a getter?

I need to make a class that wraps two dictionaries together, so that their values can be retrieved by a key of either an int or a string.

Properties seem to be the best approach here, but is there a difference between these two implementations?

public class Atlas<TValue>
{
    private Dictionary<int, TValue> _byIndex;
    private Dictionary<string, TValue> _byName;

    public Dictionary<int, TValue> ByIndex
    {
        get { return _byIndex; }
    }

    public Dictionary<string, TValue> ByName
    {
        get { return _byName; }
    }
}

And

public class Atlas<TValue>
{
    public Dictionary<int, TValue> ByIndex { get; private set; }
    public Dictionary<string, TValue> ByName { get; private set; }
}

In either case, the dictionary object is immutable and the elements can freely be changed, which is what I want. However, trying to change the dictionary object will result in either a ~ cannot be assigned to -- it is read only or a ~ cannot be used in this context because the set accessor is inaccessible. I realize the compiler will fluff out my auto properties into something similar to the top block of code anyways...

Does it actually matter which compiler error is raised?

like image 221
Kyle Baran Avatar asked Oct 11 '14 01:10

Kyle Baran


People also ask

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

Is it important to always define setters and getters for all the private variables?

It is not necessary to write getter or setter for all private variables. It is just a good practice. But without any public function you can not access the private data(variable) of the class.

Should getter methods be private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.

Can getter setter be private?

It simply sets the field to this value. The generated getter/setter method will be public unless you explicitly specify an AccessLevel , as shown in the example below. Legal access levels are PUBLIC , PROTECTED , PACKAGE , and PRIVATE . You can also put a @Getter and/or @Setter annotation on a class.


1 Answers

The only difference is that in the second case the setter is inaccessible, but it is there, while in the firs case there is no accessor at all. This means that a program that uses reflection could potentially access the properties of the second example, while in case of the first example you would need to access fields instead.

As far as non-reflective use is concerned, there is no difference between the two code snippets: outside classes will not be able to set the dictionaries.

You may want to go further and hide the presence of the dictionaries from the users of your classes. Rather than providing two properties of Dictionary type, you may want to hide this detail of implementation from the users of your class by hiding it behind a pair of method:

public class Atlas<TValue> {
    public bool TryGetByIndex(int index, out TValue val);
    public void Add(int index, TValue val);
    public bool TryGetByName(string name, out TValue val);
    public void Add(string name, TValue val);
    public TValue this[string name] { get ... set ...}
    public TValue this[int index] { get ... set ...}
    // You may want to add more methods or properties here, for example to iterate atlas elements
}
like image 110
Sergey Kalinichenko Avatar answered Oct 23 '22 02:10

Sergey Kalinichenko