Reputation: 1327
If I want to set my property in a Class private, so it should be only possible to use and set this property in this class, what is the better way? This
public string Name { private get; private set }
or
private string Name { get; set }
hmmm and there is also
private string Name { private get; private set }
Upvotes: 7
Views: 50133
Reputation: 5
surely enough, That is the definition of private classifier in OOP: just allow access within methods and scope of the class that owns such private member. Thus if your aim is to disable anyother means of access to a particular member of a class, claiming it as:
private <Type_name> <member_identifier> ;
is the simplest and enough to make it such.
Upvotes: 0
Reputation: 113232
It's worth noting that originally, C# wouldn't let you set different accesses on a getter or setter, so the only possible choices were:
public string Name { get; set; }
protected internal string Name { get; set; }
internal string Name { get; set; }
protected string Name { get; set; }
private string Name { get; set; }
(For that matter, you couldn't have automatic properties and always had to do the writing to and from a backing field yourself, but we'll ignore that just because we'll have shorter examples that way).
It is often useful to have different accesses for the two, most often a more restrictive setter than getter, and so the likes of
public string Name { get; private set; }
was introduced.
Now, by extension of that, it would seem logical enough to allow:
public string Name { private get; private set; }
private string Name { private get; private set; }
However, what are these two expressing?
The second isn't too bad, it's just needlessly repetitious. Still though, it's quite likely that some confused thinking got us there (most likely an incomplete refactoring). Good code is as much about expressing what you are doing as making a computer do something (if anything, more so), better to have it express clearly.
Hence if you end up with the likes of { private get; private set; }
then it'd likely be worth looking at again and thinking about what you really want to say here. Hurrah for it being a compiler error.
The first case is even worse. It says "this property is public, except for the setter that is private, and the getter that is private". That's not an exception, "it's this thing, except for all the time" is no real expression of anything. Doubly hurrah the compiler for not letting us do it.
Upvotes: 18
Reputation: 31
It seems like you want
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
this would allow you to access private string Name
.
Upvotes: 0
Reputation: 754
For a private member, you don't need to define accessors. Just do this:
private string _name;
Upvotes: 0
Reputation: 4285
public string Name { get; private set; }
This is what I think you are wanting to do.
There is no point trying to make the get private when the property is public unless you only want your class to see it. In that situation you should use:
private string Name { get; set; }
Update: On second read, you definitely want the second example.
Upvotes: 1
Reputation: 61912
Have you tried compiling your examples? Only the middle one will translate.
If you want to specify extra accessibility level keyword, you can only do it on one of the accessors (getter/setter), and that level of the accessor must be more restrictive than the accessibility of the entire property.
Here you see the rules: Restricting Accessor Accessibility
Upvotes: 9
Reputation: 62248
The better way depends on what you want:
public string Name { private get; private set }
The property is public
but noone can read or wrote to it, except class itself. That is completely useless, so use just
private string Name { get; set }
.
In general if you view the property like a couple of methods (which actually is)
private string get_Name() { }
private string set_Name(value) { }
The reason of having a possibility to apply that identifiers to a property get/set
becomes evident, I hope.
Upvotes: 0