Public Group

# Need help understanding Encapsulation, Properties and Fields in C#

This topic is 2603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have no idea how to use these, or even what they are used for exactly.

I have a million books, all of which I've looked at, I looked at a bunch of YT tutorials, still don't understand

As far as I understand, Fields are like, variables specific to a class or something.

And Properties are sort of like methods. The Get/Set accessor things have me scratching my head as well.

Basically, can someone explain to me in the most basic way, how to use Fields, and how to use Properties with the Get/Set accessors?

As for Encapsulation, all I know is that it's a way of keeping data hidden within a class. Only problem is, what is it used for exactly? I'm not sure what encapsulations purpose is.

Sorry in advance if some of these questions aren't worded good/the questions overall are silly, I am just about to head to bed.

##### Share on other sites
Classes represent a cohesive concept. Concepts have data that represent their state or configuration, and methods that perform some action or change the state/configuration.

Fields represent that state or configuration that is required by your class to do... whatever it's doing.
Properties behave like fields, but (unless they're auto properties) don't actually store anything. They simply provide state or configuration that you can calculate some other manner.

If you have a class that represents a rectangle for example, you don't need to store the area of the rectangle. You can store the width and height, and then provide Area as a property, calculated from the other two fields. The get/set accessors are the means for you to specify how to map the property to 0 or more fields.

Encapsulation is used to keep a class behaving as it should. Concepts often have invariants. Invariants are... rules essentially that a class must follow. For the sake of example (don't do this in the real world) we wanted a class that was a square. While it has a width and a height, is has the invariant that width and height always be equal. If you just supply width and height as public fields, someone could go in and make the width different from the height. Instead you would keep that storage private, and provide properties or methods that perform actions on the class while maintaining that class requirement. A more practical benefit is that the fewer things that can modify a field, the easier it is to then debug your program when the field ends up with the wrong value somehow.

##### Share on other sites
Very good question. I too am having big problems coding anything at the moment.

I try to make a few classes and have the bullets shooting from the ship but no joy.

I get null reference errors, variables changing values etc

I try to code to take a break from theory study.

I have been learning for a month, but not much has sunk in cos reading code is a bit of a dry subject to me.

Don't tell me it's not either!

Here's a decent website for beginners. http://www.csharp-station.com/default.aspx

##### Share on other sites

I have been learning for a month, but not much has sunk in cos reading code is a bit of a dry subject to me.

Don't tell me it's not either!

This depends on the person and perhaps the level of experience they have.

Frankly, reading other peoples code, especially finely crafted code can be a similar experience to watching a great moving or reading an expertly written book. At times it can be an inspiring and informative experience.

On the flipside though, reading "abstract" teaching code, code written to teach a language feature as opposed to actually "doing" something... like code showing inheritance using classes A, B and C, this is horrifically painful for me to read! It's as much how my brain is wired, but purposeless code is horrifically difficult for me to grok.

##### Share on other sites

Classes represent a cohesive concept. Concepts have data that represent their state or configuration, and methods that perform some action or change the state/configuration.

Fields represent that state or configuration that is required by your class to do... whatever it's doing.
Properties behave like fields, but (unless they're auto properties) don't actually store anything. They simply provide state or configuration that you can calculate some other manner.

If you have a class that represents a rectangle for example, you don't need to store the area of the rectangle. You can store the width and height, and then provide Area as a property, calculated from the other two fields. The get/set accessors are the means for you to specify how to map the property to 0 or more fields.

Encapsulation is used to keep a class behaving as it should. Concepts often have invariants. Invariants are... rules essentially that a class must follow. For the sake of example (don't do this in the real world) we wanted a class that was a square. While it has a width and a height, is has the invariant that width and height always be equal. If you just supply width and height as public fields, someone could go in and make the width different from the height. Instead you would keep that storage private, and provide properties or methods that perform actions on the class while maintaining that class requirement. A more practical benefit is that the fewer things that can modify a field, the easier it is to then debug your program when the field ends up with the wrong value somehow.

Someone mind wording this so an idiot like myself can understand it, I have no idea what he's talking about, honestly.

##### Share on other sites
I hear what your saying. I don't understand 90% of what he said.

To tell you the truth I had to look up Encapsulation the other day. I had an idea but as it is used so often I needed a dictionaries definition.

I too struggle with some of the computer talk even though I'm an Englishman. The words aren't really in a persons everyday vocabulary.

Thanks to the poster for the attempt to explain some things.

Is there anyone who could provide an explanation that even a 13 year old could understand. LOL.

##### Share on other sites
 class MyClass { //Attributes / Fields private int myNumber; //Properties public int Number { get {return myNumber;} set {myNumber = value;} } //Method public void MyNumber(int p) { myNumber = p; } } 

 MyClass c = new MyClass(); //Calling the Properties //Set c.Number = 1; //Get int gotNumber = c.Number; //Calling the Method c.MyNumber(5); 

Class: Is a concept for modeling data. Classes are like blueprints for Objects which are created from a class.
Example Class Car can be used to create an Object Car.

Attributes/Fields: Are the Attributes of the class and Objects. For example you can store the number of wheels of the car or the horsepower.

Properties:Are a specific C# feature in other languages they are called Getter and Setter methods. The only purpose of this classes to access the Class attributes like number of wheels and horsepower. Why we need methods for it? because we declare attributes as private most of the time so you wont be able to change attrbiutes directly. Maybe you just want to return attribute values and dont change them.

Methods:Offer specific calculations or algorithms like divide numbers or something.

The Reason why we Encapsulate the data is that we have a better overview about our program at the first place and that we will do less mistakes at programming. Its easier to use and you have better controls about your attributes and methods.

##### Share on other sites
So Fields are class-specific variables, and Properties are the method-like feature to allow access to them outside of that class?

##### Share on other sites

So Fields are class-specific variables, and Properties are the method-like feature to allow access to them outside of that class?

Yes

Because the attributes are private you have to acces them via methods/properties which are provided by the class itself

##### Share on other sites
I try to make a few classes and have the bullets shooting from the ship but no joy.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
15
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002300
×