Home and Learn: C# NET Course

Adding Properties To Your C# Classes

<< Part of an ongoing lesson - first part is here >>

You've been using properties a lot throughout this book: setting the Text property for a Textbox, setting size properties, setting font properties, etc. The way you did it in code was this:

textBox1.Text = "My Text";

So the object is called textBox1. This textBox1 object has a property called Text. To set a value for the Text property, you're typing a string between double quotes, and after an equals sign ( = ).

Behind the scenes, this property value gets handed over to a class, so that something can be done with it.

C# .NET allows you to set up your own properties, so that they can be used the same way. Let's see how it's done.

The only thing you have in your class at the moment is a private method:

private string getMessage(string givenName)

return "Happy Birthday " + givenName;


This Method can't be seen from the outside world. But that doesn't mean that it can't be used inside of the class. We'll have our property call this method.

But our property will do the following:

  • Allow us to pass a value over to our class
  • Allow us to get that value back out of the class
  • The easy way to add a property to your class is via the Insert Snippet menu. So right click just after the final curly bracket of your getMessage Method. You should see the following menu appear:

    The Insert Snippet menu

    Select Insert Snippet to see the following: (Community 2017 users should select Snippet > Insert Snippet from the menu, rather than Insert Snippet.)

    Insert Snippet sub menu

    Double click C# to see the menu below:

    Insert Snippet - list of available shortcuts

    Select propfull from the list and C# will add this default code:

    The default snippet code for a C# Property

    The code is a little bit baffling, so we'll go through what's been added.

    The default code is for an integer property called MyProperty. A private integer variable has also been set up. (Variables inside of your own classes are called Fields.)

    But we want to return a birthday message as a string, not as an integer. So where it says private int myVar, change the int to string. Now change myVar to birthdayMessage. The variable name is highlighted:

    Highlighting a class field

    Hold your mouse over the highlighted area to see a light bulb icon appear:

    Potential Fixes popup

    Click the arrow on the light bulb to see this:

    The Rename option in C#

    Click on Rename myVar to birthdayMessage. All occurences of myVar variables will then be renamed to birthdayMessage.

    Where it says public int MyProperty, change the int to string. Your property will then look like this:

    The renamed property

    So the name of the variable outside of the property has been changed to birthdayMessage:

    private string birthdayMessage;

    This means that the variable will now be visible from everywhere in the class.

    Note that we've also replaced myVar with birthdayMessage inside of the property.

    The structure of the property itself, though, is this:

    public string MyProperty


    This sets a property called MyProperty. You can change this to almost anything you like - it's just the same as a variable name.

    Inside of the property body, you have an area that gets values out:

    get { return birthdayMessage; }

    And an area where you can put values in (set them):

    set { birthdayMessage = value; }

    The keywords get and set are used for this purpose. The value is the value you will be passing in to the property. Change the line to this:

    birthdayMessage = getMessage( value );

    We are now using our private getMessage Method. The value being handed over to our Method will be the value that gets handed over to the property.

    (If you only need to hand values over and not get them out, then just use the set part. This is known as a write-only property. If you just want to get values out, then just use the get part. The property will then be read-only.)

    back to the c# net contents page


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk