Skip to content

Instantly share code, notes, and snippets.

@nesteruk
Created December 4, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesteruk/a8c2abbe39b000bdaf2db8f0bc68a056 to your computer and use it in GitHub Desktop.
Save nesteruk/a8c2abbe39b000bdaf2db8f0bc68a056 to your computer and use it in GitHub Desktop.
Percentage Value Proxy
using System;
using System.Diagnostics;
namespace DotNetDesignPatternDemos.Structural.Proxy.ValueProxy
{
[DebuggerDisplay("{value*100.0f}%")]
public struct Percentage
{
private readonly float value;
internal Percentage(float value)
{
this.value = value;
}
// public static implicit operator Percentage(float value)
// {
// return new Percentage(value);
// }
public static float operator *(float f, Percentage p)
{
return f * p.value;
}
public static Percentage operator +(Percentage a, Percentage b)
{
return new Percentage(a.value + b.value);
}
public static implicit operator Percentage(int value)
{
return value.Percent();
}
public bool Equals(Percentage other)
{
return value.Equals(other.value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is Percentage other && Equals(other);
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return $"{value*100}%";
}
}
public static class PercentageExtensions
{
public static Percentage Percent(this int value)
{
return new Percentage(value/100.0f);
}
public static Percentage Percent(this float value)
{
return new Percentage(value/100.0f);
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(10f * 5.Percent());
Console.WriteLine(2.Percent() + 3.Percent());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment