Skip to content

Define a class with mutable variable

CodingUnit edited this page Dec 1, 2011 · 11 revisions

Define a class with mutable variable

  • Category: Defining Types
  • Description: Define a class with mutable variable
  • Code:
using System;
using System.Console;

  enum BuilderState
  {
    | None
    | Active
    | Completed
  }

// opens BuilderState enumeration to implicitly access it's internal structure
  using BuilderState;

	class Builder
	{
	  // a current state mutable auto property with private setter
	  public CurrentState : BuilderState { get; private set; }

	  public Begin() : void
	  {
		  CurrentState = Active
	  }

	  public End() : void
	  {
		// complete the build process...
		CurrentState = Completed
	  }
	}

  def print_state(b) { WriteLine($"current state = $(b.CurrentState)") }

  def b = Builder();
  print_state(b);
  b.Begin();
  print_state(b);
  b.End();
  print_state(b);
  • Execution Result:
current state = None
current state = Active
current state = Completed
Clone this wiki locally