2
Reply

Basics clarification

S

S

Mar 30 2012 6:05 AM
1.2k
Hi,

Given the following code:-

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string GetName()
        {
            return name;
        }

        public void SetName(string value)
        {
            name = value;
        }

The first part is an example of an Automatic Get\Set property. The Get is returning the field value and the Set is setting the field value.

The second part is showing how the properties can be turned into methods to achieve the same thing.

My question is with that Constructors and Methods always seem to assign to Properties directly rather than the underlining fields as seen in the method examples GetName and SetName above? is the code just purely an example of how to achieve the same thing ie a like for like comparison? 

Thanks

Steven


Answers (2)