• Forum
  • Lounge
  • Are data members the same as properties?

 
Are data members the same as properties?

Hi everyone!

It's been a while since I've posted in these forums. Been busy with my final year at University but all that said and done I've been revising and found some small (and some rather large) gaps in my knowledge.

I was wondering if Data Members and Properties are the same thing? If they arn't them what are the differences? This question isn't really language specific but just a general idea of what they are in a "programming" context.

Thanks in advance.

lnk2019
If a distinction between data members and properties is made, then data members are private instance or class data, whereas properties are data that is accessible through getters / setters. If you're talking about C#, that is how the words will probably be used, but in the end it depends on the person talking.
Thank you very much, that cleared it right up for me!
I'm pretty sure that C#'s properties and accessors (i.e., get and set) are just abstractions, i.e., the compiler probably turns
1
2
3
4
5
6
7
8
9
10
11
12
class MyClass {
        public int MyInt
        {
                get {  return myInt; }
                set { myInt = value; }
        }

        public void MyMethod()
        {
                this.MyInt = this.MyInt + 5;
        }
}

into
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyClass {
        public int GetMyInt()
        {
                return myInt;
        }

        public void SetMyInt(int value)
        {
                myInt = value;
        }

        public void MyMethod()
        {
                this.SetMyInt(this.GetMyInt() + 5);
        }
}
Last edited on
I believed that's what I said.
I didn't see your post until after I posted mine and refreshed the page.
Oh I thought what I said was ambiguous somehow.
Nope, it's good.
Thanks to the both of you! :D
12
13
14
15
        public void MyMethod()
        {
                this.SetMyInt(this.GetMyInt() + 5);
        }
That's stupid, why would the compiler generate less efficient code by throwing in function calls from nowhere?
It wouldn't, the idea was to demonstrate that they're semantically equivalent.

[edit] I guess "the compiler turns ... into ..." was the wrong way of putting it.
Last edited on
Thanks, just clearing that up.
Topic archived. No new replies allowed.