You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System;
  2. namespace IPA.Utilities
  3. {
  4. /// <summary>
  5. /// A class providing various extension methods.
  6. /// </summary>
  7. public static class Extensions
  8. {
  9. /// <summary>
  10. /// Gets the default value for a given <see cref="Type"/>.
  11. /// </summary>
  12. /// <param name="type">the <see cref="Type"/> to get the default value for</param>
  13. /// <returns>the default value of <paramref name="type"/></returns>
  14. public static object GetDefault(this Type type)
  15. {
  16. return type.IsValueType ? Activator.CreateInstance(type) : null;
  17. }
  18. /// <summary>
  19. /// Unwraps a <see cref="Nullable{T}"/> where T is <see cref="bool"/> such that if the value is null, it gives <see langword="false"/>.
  20. /// </summary>
  21. /// <param name="self">the bool? to unwrap</param>
  22. /// <returns>the unwrapped value, or <see langword="false"/> if it was <see langword="null"/></returns>
  23. public static bool Unwrap(this bool? self) => self != null && self.Value;
  24. }
  25. }