You are on page 1of 6

BEGINNERS C# TUTORIAL

LESSON 10 - PROPERTIES

B E G I N N E R S ’ S

C# TUTORIAL
1 0 . P R O P E R T I E S

WRITTEN BY JOE MAYO


JMAYO@CSHARP–STATION.COM
UPDATED 10/02/01, 12/03/03

CONVERTED TO PDF BY ASH WEAVER 02/09/03

WWW.CSHARP–STATION.COM

PAGE 1 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 10 - PROPERTIES

This lesson teaches C# Properties. Our objectives are as follows:

• Understand What Properties Are For.


• Implement a Property.
• Create a Read-Only Property.
• Create a Write-Only Property.

Properties provide the opportunity to protect a field in a class by reading


and writing to it through the property. In other languages, this is often
accomplished by programs implementing specialized getter and setter
methods. C# properties enable this type of protection while also letting you
access the property just like it was a field. To get an appreciation for what
properties accomplish, let's take a look at how to provide field
encapsulation by traditional methods.

Listing 10-1. An Example of Traditional Class Field


Access: Accessors.cs

using System;

public class PropertyHolder


{
private int someProperty = 0;

public int getSomeProperty()


{
return someProperty;
}

public void setSomeProperty(int propValue)


{
someProperty = propValue;
}

public class PropertyTester


{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();

propHold.setSomeProperty(5);

Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());

return 0;
}
}

Listing 10-1 shows the traditional method of accessing class fields. The
PropertyHolder class has the field we're interested in accessing. It has two
methods, getSomeProperty() and setSomeProperty(). The

PAGE 2 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 10 - PROPERTIES

getSomeProperty() method returns the value of the someProperty


field. The setSomeProperty() method sets the value of the someProperty
field.

The PropertyTester class uses the methods of the PropertyHolder class to


get the value of the someProperty field in the PropertyHolder class. The
Main() method instantiates a new PropertyHolder object, propHold. Next it
sets the someMethod of propHold to the value 5 by using the
setSomeProperty method. Then the program prints out the property value
with a Console.WriteLine() method call. The argument used to obtain the
value of the property is a call to the getSomeProperty() method of the
propHold object. It prints out "Property Value: 5" to the console.

This method of accessing information in a field has been good because it


supports the object-oriented concept of encapsulation. If the
implementation of someProperty changed from an int type to a byte type,
this would still work. Now the same thing can be accomplished much
smoother with properties.

Listing 10-2. Accessing Class Fields With


Properties: Properties.cs

using System;

public class PropertyHolder


{
private int someProperty = 0;

public int SomeProperty


{
get
{
return someProperty;
}
set
{
someProperty = value;
}
}
}

public class PropertyTester


{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();

propHold.SomeProperty = 5;

Console.WriteLine("Property Value: {0}", propHold.SomeProperty);

return 0;
}
}

PAGE 3 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 10 - PROPERTIES

Listing 10-2 shows how to create and use a property. The PropertyHolder
class has the SomeProperty property implementation. Notice that the first
letter of the first word is capitalized. That's the only difference between
the names of the property SomeProperty and the field someProperty. The
property has two accessors, get and set. The get accessor returns the value
of the someProperty field. The set accessor sets the value of the
someProperty field with the contents of value. The value shown in the set
accessor is a C# reserved word.

The PropertyTester class uses the SomeProperty property in the


PropertyHolder class. The first line of the Main() method creates a
PropertyHolder object named propHold. Next the value of the
someProperty field of the propHold object is set to 5 by using the
SomeProperty property. It is that simple -- just assign the value to the
property as if it were a field.

After that, the Console.WriteLine() method prints the value of the


someProperty field of the propHold object. It does this by using the
SomeProperty property of the propHold object. Again, it is that simple --
just use the property as if it were a field itself.

Properties can be made read-only. This is accomplished by having only a


get accessor in the property implementation.

Listing 10-3. Read-Only Property: ReadOnlyProperty.cs

using System;

public class PropertyHolder


{
private int someProperty = 0;

public PropertyHolder(int propVal)


{
someProperty = propVal;
}

public int SomeProperty


{
get
{
return someProperty;
}
}
}

public class PropertyTester


{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder(5);

Console.WriteLine("Property Value: {0}", propHold.SomeProperty);

PAGE 4 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 10 - PROPERTIES

return 0;
}
}

Listing 10-3 shows how to implement a read-only property. The


PropertyHolder class has a SomeProperty property that only implements a
get accessor. It leaves out the set accessor. This particular PropertyHolder
class has a constructor which accepts an int parameter.

The Main() method of the PropertyTester class creates a new


PropertyHolder object named propHold. The instantiation of the propHold
object uses the constructor of the PropertyHolder that takes an int
parameter. In this case, it is set to 5. This initializes the someProperty
field of the propHold object to 5.

Since the SomeProperty property of the PropertyHolder class is read-only,


there is no other way to set the value of the someProperty field. If you
inserted propHold.SomeProperty = 7 into the listing, the program would not
compile, because SomeProperty is read-only. When the SomeProperty
property is used in the Console.WriteLine() method, it works fine. This is
because it is a read operation which only invokes the get accessor of the
SomeProperty property.

Listing 10-4. Write-Only Property: WriteOnlyProperty.cs

using System;

public class PropertyHolder


{
private int someProperty = 0;

public int SomeProperty


{
set
{
someProperty = value;

Console.WriteLine("someProperty is equal to {0}", someProperty);


}
}
}

public class PropertyTester


{
public static int Main(string[] args)
{
PropertyHolder propHold = new PropertyHolder();

propHold.SomeProperty = 5;

return 0;
}
}

PAGE 5 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED
BEGINNERS C# TUTORIAL
LESSON 10 - PROPERTIES

Listing 10-4 shows how to create and use a write-only property. This time
the get accessor is removed from the SomeProperty property of the
PropertyHolder class. The set accessor has been added, with a bit more
logic. It prints out the value of the someProperty field after it has been
modified.

The Main() method of the PropertyTester class instantiates the


PropertyTester class with a default constructor. Then it uses the
SomeProperty property of the propHold object to set the someProperty
field of the propHold object to 5. This invokes the set accessor of the
propHold object, which sets the value of its someProperty field to 5 and
then prints "someProperty is equal to 5" to the console.

In summary, you now know what properties are for and how they're
used. Traditional methods of encapsulation have relied on separate
methods. Properies allow you to access objects state as if it was a
field. Properties can be made read-only or write-only and you know how to
implement each type.

PAGE 6 OF 6
COPYRIGHT © 2000–2003 C# STATION , ALL RIGHTS RESERVED

You might also like