0

I'm learning C# from the book "Head First C#". I thought I understood properties. They are used like fields but work like methods, with getters and setters. I never thought of them as another object appending to the instantiated object. Is that the case?

Please see the code given in the book and the Outfit object which got me thinking about this.

Thank you so much for your help!!

Follow-up questions:

  1. Is it the case that when an object is instantiated, all of its properties will also be instantiated as objects on the heap, except for the value types?
  2. Why isn't there a HairStyle object connected to the Guy object in this case?

enter image description here

enter image description here

  • 1
    Look carefully at the `Guy` class - 2 of the properties are also other classes: `HairStyle` and `Outfit` When instantiated, those 2 props are objects. `Name` on the other hand is just a string. – Ňɏssa Pøngjǣrdenlarp Apr 03 '22 at 19:44
  • 2
    Look at the `Guy` class. There is a line that says something like `public Outfit Clothes { get; set; }`. Do you understand what that line says? What do you think would happen if I call the setter/getter of `Guy.Clothes`? In fact, the setter is called in `Clothes = new Outfit() { ... }`. What do you think happens there? – Sweeper Apr 03 '22 at 19:45
  • Got it, thank you so much for your answers! So there should also be a HairStyle object connecting to the Guy object in the graph, right? – AlwaysLearning Apr 03 '22 at 19:48
  • [I think this might answer your question](https://stackoverflow.com/a/6001941/5133585). – Sweeper Apr 03 '22 at 19:51
  • Is it the case that when an object is instantiated, all of its properties will also be instantiated as objects on the heap, except for the value types? – AlwaysLearning Apr 03 '22 at 19:59
  • *Technically,* the `Hair` of a `Guy` isn't an object, because it is of a value type. But C# is designed so that all types *can be treated as* objects. Regarding your follow up question, see [this](https://stackoverflow.com/questions/4487289/memory-allocation-stack-vs-heap). – Sweeper Apr 03 '22 at 19:59
  • I don't understand why HairStyle won't become an object in this case. Could you please explain a bit? Thank you so much! – AlwaysLearning Apr 03 '22 at 20:06
  • Your question now needs more focus. Please only ask *one* very specific question per post. If your original problem of "why is there an `Outfit` object referenced by the `Guy` object" solved by any of the answers, please accept one of them and ask the other question ("why no `Hairstyle` object") in another post. The question about where things are allocated is likely to be closed as a duplicate of [this](https://stackoverflow.com/q/4487289/5133585), so I'd suggest that you don't ask that, unless you can explain why the answers there don't answer your question. – Sweeper Apr 03 '22 at 20:19

3 Answers3

0

Yes, you are rigth when say "property like methods", but this methods needs to work with some type, and it can be base type or object as well.

Pivazi Z.
  • 76
  • 5
  • Thank you so much for your answer! So when an object is instantiated, all of its properties will also be instantiated as objects on the heap, except for the value types? – AlwaysLearning Apr 03 '22 at 19:54
  • @Programmer No, It depends on the class's constructor; if you instantiate your reference properties with a new keyword inside your constructor, It's Ok. – Reza Heidari Apr 03 '22 at 20:16
0

I think it's better that we pay attention to the difference between class and object first.

Many people get confused by the difference between a class and an object. The difference is simple and conceptual. A class is a template for objects. A class defines object properties, including a valid range of values and a default value. A class also describes object behavior. An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.

And now, let's check out the properties:

Properties are attributes or features that characterize classes. While classes are groups of objects, an instance is a specific object that actually belongs to a class.

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23
0

I'm not exactly sure what you're asking but maybe this will help. The properties you're looking at are like, you said, getters and setters of a certain type (string, int, Outfit etc).

The "Full" property below is what's going on under the hood (as far as I know)

            public class Props
            {
                //Syntactic Sugar
                public string? MyProperty { get; set; }


                //What's actually happening
                private string? myProperty_full;//This is the field that holds the value
                public string? MyProperty_Full
                {
                    get => myProperty_full;
                    //value is whatever the user sets MyProperty_Full to 
                    set => myProperty_full = value;
                }

            }

In the case of Clothes property. There is a private backing field of type Outfit with some getters and setters for manipulating it.

ShanieMoonlight
  • 1,623
  • 3
  • 17
  • 28