Skip to content

Instantly share code, notes, and snippets.

@pinkshrub
Last active October 17, 2017 17:29
Show Gist options
  • Save pinkshrub/c26578cd0c962406714020c7a3430ed9 to your computer and use it in GitHub Desktop.
Save pinkshrub/c26578cd0c962406714020c7a3430ed9 to your computer and use it in GitHub Desktop.

What is get and set?

So first, lets talk about Encapsulation. Encapsulation is the idea that we can hide information and implementation details inside of a class. If we have, for example, a Math class, we would like it to be able to return the absolut value of a number. We dont care necessarily how it does it, only that it does it. It could multiply the value by -1 if it is a negative number or it could take the root of the number's square. What is convenient is that we know where to look if this method doesn't behave properly - inside the Math class! So we can hide complex behavior (if(x>-1){return x}else{return x\*-1}) behind a simple public interface like Math.abs(x) and we can easily change it because it exists in 1 place. Another side effect of encapsulation is that it makes it easy to "hide" where/when change occurs. For example, maybe we don't like that our abs(x) does logic. Maybe its faster if we just do a simple math operation like return sqrt(x*x). We may have bunch of other code that depends on or is coupled to our Math.abs(x) method. by encapsulating the behavior, we make it so that when we implement changes, it doesn't break all of our code elsewhere. Hopefully this idea illustrates how designing an appropriate class and interface is.

Sweet! Encapsulation, woah! But what does this have to do with { get; set; }? Well, in C# part of how we accomplish encapsulation is by using accessibility keywords to define what is publicly available(part of the public interface) or private/protected/internal. This distinction is important, there are sorta 2 "interfaces" we can care about, how our class is interfaced with publicly and how child-classes(of our class) interface with our class. If we don't want certain information or methods available to anybody(public) or to child-classes(protected) we mark them as private. There are plenty of reasons to encapsulate/control how things are accessed(validations, trigger events, subject to change, etc.). But remember an underlying goal of encapsulation: Hide Implementation details. We can provide a public interface to get(retrieve) data or set(assign) data and then privately control that data. This way we can change the data a bunch, but how it is accessed doesn't change. So how you might do this in C# would be to define a private field(word for attribute/property) and also a public method(s) to get and/or set that field.

public class Genre
{
    private string name; // This is the backing field or the data we may be toying with
    public string Name // This is your property, or how you want the private data to be publicly available
    {
        get 
        {
            return name;
        }
        set 
        {
            name = value;
        }
    }
}

That is great, but kinda boilerplaty So C# gives us a shortcut when we want to give a field accessor methods with the same name(like above):

public class Genre
{
    public string Name { get; set; }
}

One last thing to note is that we can make something read(get) or write(set) only by either excluding the method we don't want/need public string Name { get; } or putting private in front of the method. public string Name { get; private set; }

Where I got these words from: https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables https://dzone.com/articles/why-encapsulation-matters Some exploration of mechanics of C# syntax: https://stackoverflow.com/questions/5096926/what-is-the-get-set-syntax-in-c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment