Skip to content

Instantly share code, notes, and snippets.

@roy-t
Created December 20, 2017 10:34
Show Gist options
  • Save roy-t/21d52f1b7cc0164acb52d44bf2dce373 to your computer and use it in GitHub Desktop.
Save roy-t/21d52f1b7cc0164acb52d44bf2dce373 to your computer and use it in GitHub Desktop.
Using real units in C# (meters, seconds, meters per second)
MetersPerSecond speed = 5.0f;
Seconds elapsed = 1.0f / 60.0f
Meters distance = speed * elapsed;
public struct Meters
{
public Meters(float value)
{
this.Value = value;
}
public float Value { get; set; }
public static implicit operator Meters(float value)
{
return new Meters(value);
}
}
public struct Seconds
{
public Seconds(float value)
{
this.Value = value;
}
public float Value { get; set; }
public static implicit operator Seconds(float value)
{
return new Seconds(value);
}
}
public struct MetersPerSecond
{
public MetersPerSecond(float value)
{
this.Value = value;
}
public float Value { get; set; }
public static implicit operator MetersPerSecond(float value)
{
return new MetersPerSecond(value);
}
public static Meters operator *(MetersPerSecond mps, Seconds seconds)
{
return new Meters(mps.Value * seconds.Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment