I basically want to do something like this
public string Password
{
set { password = value; }
}
internal string Password
{
get { return password; }
}
But without having to call my second function something else (the code above will not compile as I used the name Password twice). I could have setPassword and getPassword, but that seems a bit, well, rubbish 🙁
My scenario is that I’ve a separate assembly (class library) that contains the core business logic. I would not want any projects that use the class library to be able to view the password once set.
It’s as simple as:
It’s worth noting that this is an unusual thing to see, and would violate many people’s expectations about properties. While this is possible, it would be preferable to have a public method
SetPasswordand either an internal field, or an internalGetPasswordmethod. People simply don’t expect to come across a property they can set but not get (even though the reverse is common). This is probably one of the few valid use cases for it, but just to follow the “least surprise” methodology, consider using methods instead.