C# Properties (Get and Set)


Properties and Encapsulation

Before we can begin to define structures, you must have a basic understanding of "Encapsulation".

Encapsulation definition is, to ensure that "sensitive" data is hidden from users. To do this, you must:

  • declare fields/variables as private
  • provide public get and set methods, through properties, to access and update the value of a private field

Properties

You learned in the preceding chapter that a private variables can only be accessed in the same class (the external class cannot access it). However, sometimes we need access to them - and it can be done through properties.

Property is similar to a combination of variable and method, and has two methods: get method and set method:

Example
class Person
            {
              private string name; // field
            
              public string Name   // property
              {
                get { return name; }   // get method
                set { name = value; }  // set method
              }
            }
Example Explained

Name property is associated with a name field. It is good practice to use the same name in both the property and the private field, but with the first capital letter.

The get method returns the value of the variable name.

The set method assigns a value for the name. The value keyword represents the value we give the property.

We can now use the Name feature to access and update the private field of the Person class:

Example
class Person
            {
              private string name; // field
              public string Name   // property
              {
                get { return name; }
                set { name = value; }
              }
            }
            
            class Program
            {
              static void Main(string[] args)
              {
                Person myObj = new Person();
                myObj.Name = "Liam";
                Console.WriteLine(myObj.Name);
              }
            }
            

Automatic Properties (Short Hand)

C# also provides a way to use short / default layouts, where you do not have to define a field field, and you should only write get; and set; inside the property.

The following example will show the same result as the example above. The only difference is that there is less code:

Example

Using automatic properties

class Person
            {
              public string Name  // property
              { get; set; }
            }
            
            class Program
            {
              static void Main(string[] args)
              {
                Person myObj = new Person();
                myObj.Name = "Liam";
                Console.WriteLine(myObj.Name);
              }
            }
            

Why Encapsulation?

  • Better control of class members (reducing your chances of (or others) breaking code)
  • Fields can only be read only(if you only use the find method), or write only (if you only use the default method)
  • It is flexible: the designer can change one part of the code without touching the other parts
  • Additional data protection