BoxedConstants.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. namespace PaintDotNet
  2. {
  3. /// <summary>
  4. /// Provides access to a cached group of boxed, commonly used constants.
  5. /// This helps to avoid boxing overhead, much of which consists of transferring
  6. /// the item to the heap. Unboxing, on the other hand, is quite cheap.
  7. /// This is commonly used to pass index values to worker threads.
  8. /// </summary>
  9. public sealed class BoxedConstants
  10. {
  11. private static object[] boxedInt32 = new object[1024];
  12. private static object boxedTrue = (object)true;
  13. private static object boxedFalse = (object)false;
  14. public static object GetInt32(int value)
  15. {
  16. if (value >= boxedInt32.Length || value < 0)
  17. {
  18. return (object)value;
  19. }
  20. if (boxedInt32[value] == null)
  21. {
  22. boxedInt32[value] = (object)value;
  23. }
  24. return boxedInt32[value];
  25. }
  26. public static object GetBoolean(bool value)
  27. {
  28. return value ? boxedTrue : boxedFalse;
  29. }
  30. static BoxedConstants()
  31. {
  32. }
  33. private BoxedConstants()
  34. {
  35. }
  36. }
  37. }