What does it mean? [FROM#]

If we define a property as a public property, and in this property we have a protected getter. what does it mean? if the property is publicly available, what does defining a secure getter mean for this, then? see below code:

  public ISessionFactory SessionFactory { protected get { return sessionFactory; } set { sessionFactory = value; } } 
+4
source share
4 answers

In C # you are allowed getters and setters with access levels (see access modifiers ) that are different from the general property. This is the most common pattern.

 public class FooObject { public String Foo { get; private set; } } 

This allows objects creating an instance of FooObject to retrieve the value of Foo, but not to set its value. A private modifier on the installer means that only this method has only FooObject (not including the use of reflection).

There are two advantages to this:

  • With the addition of automatic properties (no variables require assigning get and set values), this allows you to use the private parameter of the property variable (created for you at compile time) so that this is done without the need to explicitly create the variable. Without this, you could not use the automatic property, if only you always wanted the get and set function to be public, all private, etc.

  • It allows the level of abstraction so that all methods, whether public, private or otherwise, go through a property and not directly access a private variable. See question for details.

In your instance, other objects can set the value of the factory session, but only its inheriting classes can retrieve it. In most cases, if an object can set a value, it can also get it, but there are times when it would be useful not to allow it. This is allowed because the specified event does not allow more access than what was defined for the common property.

The best example I can think of would be if the given object were modified on a given event. For example, if this specified event was set for a connection object, and a connection string was added in the setup event and a connection to the database was opened (in my example, I will probably reorganize the code so that it does not act in this way, but it may occur something like that).

+2
source

This means that getters can only be called by subclasses. The “protected” before the getter, so to speak, overwrites the “public” for the getter part of the property.

+11
source

Protected keyword - membership access modifier. The protected member is accessible from the class in which it is declared, and from any class that is derived from the class that declared this element.

http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx

0
source

Protected get means that this getter property can only be accessed from an inherited class of this class. the collection is considered public, so this property can be published publicly.

0
source

Source: https://habr.com/ru/post/1307225/


All Articles