What is the difference between a property and a variable

I have confusion about understanding properties and variables

public class ABC() { public int A; public int B { get; set; } } 

What is the difference between A and B?

+59
c #
Nov 10 '10 at 9:20
source share
9 answers

As many have pointed out, A is a field, B is a property.

The real question is: why do you need this and what to use?

I refer to Jonathan Aney's blog post :

(Its in VB, but this also applies to C #;))

So, why use properties over fields, 5 reasons:

1. Fields cannot be used in interfaces

You cannot ensure the existence of a field in a public contract of objects through an interface. For properties though it works great.

2. Verification

While your application may not require any validation logic to set a specific value, changing business requirements may require adding this logic later. At the same time, the point of changing the field to a property is a change for consumers your API. (For example, if someone was checking your class using reflection).

3. Binary serialization

Changing a field for a property is if you use binary serialization code. By the way, this is one of the reasons VB10s automatically implemented properties have a “bound” background field (that is, you can express the name of the support field in the code) - so if you change the automatically implemented property to the extended property, you can still maintain compatibility with serializing by storing the name of the support field is the same (in C # you have to change it because it generates support fields with non-transferable names).

4. Most of the .NET data binding infrastructure is bound to properties, but not to fields

Ive heard arguments from both sides, for whether it is good or not, but the reality is how it works right now. (Mark me: WPF bindings work on properties)

5. Open Field Detection - FxCop Violation

For many reasons listed above :)

There may be more reasons.

I would also like to point out a Jeff Atwood blog post and complete a quote from it:

It is very important to clean it here to avoid writing code that does not matter. And property wrappers around public variables are the very essence of meaningless code.

+50
Nov 10 '10 at 9:30
source share

A is a field, B is a property. A property is basically syntactic sugar for getters and setters. The class you define will be compiled something like this:

 public class ABC() { public int A; private int backing_B; public void set_B(int value) { backing_B = value; } public int get_B() { return backing_B; } } 

Please note that this type of conversion is valid for all C # properties - calls to ABC.B will be converted to method calls. Properties basically provide the illusion of a "variable", but are actually just a mentally disguised pair of methods.

This is important because it allows you to declare your own get and set method, which can check values ​​or do other interesting things:

 private int b; public int B { get { return b; } set { if (value < 0) throw new ArgumentOutOfRangeException("value"); b = value; } } 

Note that most properties will use the field to store their value. Properties rarely exist on their own, except for fields.

+20
Nov 10 '10 at 9:23
source share

A property is a kind of short getter and / or setter. You can add logic to the set or get properties of the properties or make them private, which means that they are inaccessible from the outside, where the variable is always available (if it is public).

+4
Nov 10 '10 at 9:24
source share

A variable is a variable.

A property is a special type of method that provides this variable. And since this is a method, so you can do other things with it, except how to expose a variable to a variable.

From MSDN:

The Property declaration introduces the property declaration. A property can have a Get procedure (read only), a Set procedure (write only), or both (read and write). You can omit the Get and Set procedure when using the automatically implemented property. For more information, see "Automatically Implemented Properties" (Visual Basic).

You can use the property only at the class level. This means that the declaration context for a property must be a class, structure, module or interface and cannot be a source file, namespace, procedure or block. For more information, see Declaration Contexts and Default Access Levels.

By default, properties use public access. You can adjust the level of access to properties using the access modifier in the Property statement, and you can optionally set one of your property procedures to a more restrictive level of access.

+3
Nov 10 '10 at 9:22
source share

In C #, any "variable" that has a getter and setter is called a property. A variable does not have getters and setters or what text books say.

My programming instructor made us have getters and setters for almost every variable we created in Java. Even indexing variables, he forced us to use getter and setter if they were declared in a global class scope. I think this may have been excessive, but it made me create getters and setters.

The real thing about getters and setters is that they are more than likely doing more than just setting an internal variable. Most setters are going to do some type of data validation to make sure that the data can be set to a variable. A getter can also check the returned data for some criteria.

If your property is private and your setters and recipients are publicly available, anyone can access your variable and modify it as if they had public access to the actual variable. Thus, in reality, you should check your data to make sure that it is valid or some other data check.

 private int myVariable; public int myVariable { get { return myVariable; } set { if (value < 0) { throw new Exception("This is your exception some where else in code"); } myVariable = value; //remember value is something that is //declared automatically } } public string FirstName { get; set; } 

The above way is to write down the following

 private string firstName; public string FirstName { get { //...code here } set { //...code here } } 
+3
Apr 22 '14 at 16:58
source share

In your example, A is a public field of class ABC and B is a public property of class ABC . In particular, B is an automatically implemented property . This means that “under the hood” the compiler does a certain job for you and effectively converts your code into:

 public class ABC() { private int b; public int A; public int B { get { return b; } set { b = value; } } } 
0
Nov 10 '10 at 9:22
source share

A variable is defined mainly for accessing a value from a class or to the same class in accordance with their modifier assigned to this variable.

When we define a property, two methods are created for one property, so additional overhead is generated, which is a drawback of the property.

The advantages of properties are to get or set a value in a private class variable.

0
Aug 19 2018-12-12T00:
source share

There is a very good article (link below) about using fields / variables vs Properties from Microsoft itself. Although the article mostly talks about the FxCop violation rule, it clearly defines the difference between the two and the exact rules of use.

Excerpt from the article:

The main use of the field should be as an implementation detail. Fields must be private or internal and must be opened using properties. Access to an object is as simple as access to a field, and the code in property accessories can change as functions of the type expand without making changes.

See https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.0/ms182141(v=vs.80)

0
Dec 6 '18 at 10:06
source share

You can set read-only for properties by only declaring get fnc for the property. But we cannot do this with a variable.

- BinhPT -

-5
Dec 23 '14 at 0:13
source share



All Articles