Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

Click here to download the source code for this lesson.

In this lesson I am going to cover how to create properties in a C# class.  This lesson is very important because it comes in handy a lot in C# programming, especially with database programming.  You can think of a class property as like a variable or an option of the class.  Take a look at the following syntax that shows an example of how to refer to a class property in some code.

AccessorMethods accessortest = new AccessorMethods();

accessortest.Total = 55;

In the above example I am instantiating an instance of the class AccessorMethods.  Then I refer to a property of that class called “Total” in an assignment statement where I give it the value 55.  Properties contain special methods called getters and setters (because you get the value of a variable or set the value of a variable).  In the above example, I am using the setter because I am setting the value of the Total property.  Formally the getter and settor methods are referred to as accessor methods in C#.

Now let’s look at how you can code class properties.  I have a class called AccessorMethods that contains two instance variables:

int mTotal;
string mCustomerName;

These two variables are not accessible from outside the class because they are not declared as public.  This adheres to the Object-Oriented design best practices of data hiding and encapsulation.  There are situations however when you need to allow the caller of a class to be able to set the value of some instance variables for convenience.  This can be accomplished through properties.  If I want the caller of my AccessorMethods class to be able to get and set the value of the mTotal variable, I can declare a property like this:

public int Total
{
 get { return mTotal; }
 set { mTotal = value; }
}

Look strange, doesn’t it?  It is actually not so bad once you understand what is going on.  On the first line is the declaration of the property name, which is “Total” in this case.  Notice that the property name is NOT the same as the instance variable name (mTotal).  I had to declare the property as public so that a caller can access it.  I also had to define what type the property will return from its getter method, which is “int” in this case.  The return type of the property (Total) must match the type of the instance variable (mTotal).  The rest of the property is contained within curly braces.

Underneath the curly brace is the get (getter) property method.  This is the method that will be called when a caller tries to read the property.  In the example, the value of the variable mTotal will be returned.

On the next line is the set (setter) property method.  This is the method that will be called when a caller tries to change the value of the property (write).  This line contains a special keyword called “value”.  “value” is a dynamic variable that will get populated at runtime during an assignment statement.  In the first code example at the beginning of this lesson there was a line where I called the setter method:

//set { mTotal = value; } 

accessortest.Total = 55;

At runtime the variable “value” in the setter method will get replaced with 55 and the instance variable mTotal will be assigned 55.  See how that works?  Not so bad huh!  Now let’s take a look at the property code for the other instance variable in my class: mCustomerName.

public string CustomerName
{
 get { return mCustomerName; }
 set { mCustomerName = value; }
}

It looks similar to the Total property but there is a difference.  Since mCustomerName is a string type, I had to change the return type for the CustomerName property to string.  Here is an example of how I can call the setter property for CustomerName.

AccessorMethods accessortest = new AccessorMethods();

accessortest.CustomerName = "Ted";

To use the getter property methods the syntax is the same as the setter method, except it is used in a read operation instead of an assignment operation:

lblOutput.Text = string.Format("Total = {0}. CustomerName = {1}.",
 accessortest.Total,
 accessortest.CustomerName);

Since the syntax for property definitions looks very similar no matter what the underlying instance variable actually is, you can use the following template and just re-use the template for all of your programs:

public int MyProperty {
 get {return mClassVariable;}
 set { mClassVariable = value; }
}

You have to replace three things in the template to fit your scenario.  First change the return type int to the type of your variable.  Then change the name of the property from MyProperty to whatever you would like to call your property.  Finally you change the name mClassVariable to the name of your instance variable.  You can copy and paste this template as needed in your programs.

Click here to watch an example video where I create some class properties and then use them in a calling program.

Did you enjoy this article? Join my free Amazing ASP.NET newsletter.
It's safe and easy to signup. Unleash your unlimited web programming potential. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

1 Comment »

  1. The video is a life saver. With it, the application is easy to build. Without it, I get stuck.

    Comment by — March 17, 2009 @ 9:28 am

RSS feed for comments on this post. TrackBack URL

Leave a comment