Created
December 20, 2017 10:34
-
-
Save roy-t/21d52f1b7cc0164acb52d44bf2dce373 to your computer and use it in GitHub Desktop.
Using real units in C# (meters, seconds, meters per second)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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