Please explain how the properties of C # work?

I have been studying C # for a while, and I found properties in my C # book (Head First C #). I honestly do not understand why they are used, and why I should use them. I searched for them several times, but still cannot understand how I understand them.

Can someone explain this foreign concept to me?

Thanks,

Varmitharen

+8
c # properties visual-studio
source share
5 answers

Properties are used to enrich the concept of encapsulation of object-oriented programming.

i.e. They encapsulate a member of the field and allow you (the developer) to control the setting / retrieval of this variable. Example?

public class Person { private int m_age; public int Age { set { if(value < 18) m_age = 18; else m_age = value; } get { return m_age; } } } 

Cm? using the Age property, we ensured that the minimum age setting is 18.

+4
source share

Properties provide controlled access to data; at its most basic, it can only mean field encapsulation (public fields are not recommended), which the compiler can do for you easily:

 public int Foo {get;set;} // the compiler handles the field for you 

However, you can use the property to provide logic or handle side effects:

 private int foo; public int Foo { get { return foo; } set { if(value < 0) throw new ArgumentOutOfRangeException(); if(value != foo) { foo = value; OnFooChanged(); // fire event notification for UI bindings } } } 

Other common options are lazy loading, calculated elements, proxied elements, etc.

You can also change the availability, for example:

 public int Foo { get; protected set; } 

which can only be assigned by type and subclasses and not by unrelated code. It can also only have get or set.

In principle, the properties act as a more formal version of the get / set pair, which greatly facilitates the conversation about "Foo" rather than "get_Foo" / "set_Foo", etc. (for two-way binding).

Unlike fields, properties can also be advertised on interfaces, which is necessary for interface-based code.

+18
source share

While the other answers are pretty good, they are all very strongly related to the property mechanism. It is also helpful to understand the philosophy of properties.

In OO programming, we spend a lot of time creating models of semantic domains. When you say that you have the "Animal" class and the "Tiger" derived class, you model in the computer sphere a fact about the real world: that of all things in the world, some of them are animals and those animals, some of them are tigers .

But you must understand that the mechanism and semantics are different. No one says "hey, let go to the zoo and see how zookeeper calls invoke methods in IFeedable on tiger instances."

The field is a mechanism and therefore should be a private element of the class implementation; he does not describe part of the model. The property is part of the semantic model. Each tiger has a birthday, so "Birthday" should be a property of the "Tiger" class. This part of the “semantic model” of tigers, so disclose it as a property. As part of the implementation, the birthday can be stored in a private field, access to which belongs to this property.

It makes sense? In short: use public properties to describe the semantic properties of the things you model; use private fields as implementation mechanisms.

+18
source share

Do you know with fields? If so, in terms of the code that your class consumes, the properties exactly correspond to the fields. Inside your class, however, the property looks like a couple of methods, one of which concerns the return of the value to the consumer and one method that concerns the updating of the value. These methods are commonly called getters and setters .

A good reason to use a property instead of a field is that it gives you better control over the values ​​that are passed and exited from the property.

+1
source share

Look at this link Properties (C # Programming Guide)

also this one Why do we need properties in C #

0
source share

All Articles