2.7.15 Representing Properties in Class Diagrams

Representing Properties in Class Diagrams

A C# property gives controlled member access through get and set accessors. The class diagram should distinguish that public contract from its backing field.

Read the Code Before Drawing

This complete Player definition stores the value in a private field. The public property reads or writes that field without validation in this example.

Source to translate
public class Player
{
    private int jerseyNumber;

    public int JerseyNumber
    {
        get { return jerseyNumber; }
        set { jerseyNumber = value; }
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Keep the stored jersey number in a private backing field
Expose a public JerseyNumber property
Its getter reads that field
Its setter writes the supplied value to that same field
Describe the property contract separately from its implementation storage

These words describe the class contract, not a sequence of runtime actions. Identify the types, stored members, operation inputs and results, and relationships before choosing compartments and connectors.

Map the Source to Class Boxes and Connectors
Build the Diagram in Violet
  1. Create a Violet class diagram with Player.
  2. In the attributes area enter the private backing field -jerseyNumber : int.
  3. On a separate attribute line enter the exact property contract +JerseyNumber : int <<property>> {get; set;}. Preserve capitalization, annotation, and both listed accessors.
  4. Add a note explaining <<property>> marks a C# property; {get; set;} lists its accessors. The annotations are text in this diagram, not extra methods to paste into C#.
  5. Save and check that the backing field remains private while the property remains public. The source's getter and setter both reach the same backing field.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

Player has private backing field jerseyNumber and public int property JerseyNumber with get and set accessors, using an explicitly labeled C# notation convention.
One property contract over private storage
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

JerseyNumber is not another independent stored count in this implementation. Both accessors use the same backing field.

Verify the Drawing Against the Source
Predict and Check a Small Change