StudyMaterial.Utilitities.Extensions

Home > Preview

The flashcards below were created by user mateotete on FreezingBlue Flashcards.


  1. What is the basic structure of an extension method? Give an example which is used to reverse a string: 'abc' to 'cba'.
    Basic structure: static class, static method, uses 'this' keyword to represent the item it is extending.

    • public static string Reverse(this string input)
    • {
    •      char[] chars = input.ToCharArray();
    •      Array.Reverse(chars);
    •      return new String(chars);
    • }
  2. What are extension methods? Where would we put it and how would we call it?
    Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. 

    An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.

    We typically place them in a folder called Utilities within a root program containing other commonly shared objects. Within another project, this utility can then be referenced and then imported into the file with the using directive.
  3. Write a simple Int32 extension method to convert a string value to Int32 (include error handling).
    • public static long ToInt32(this string value)
    • {
    •      Int32 result = 0;

    •      if (!string.IsNullOrEmpty(value))
    •           Int32.TryParse(value, out result);
    •      else
    •           throw new FormatException(string.Format("'{0}' cannot be converted as Int32", input));

    •      return result;
    • }
  4. Write an extension method to format a string into a phone number: (555) 123-1212
    • public static string FormatPhone(this string input)
    • {
    •      if (IsPhone(input))
    •           return string.Format("({0}) {1}-{2}", input.Substring(0, 3), input.Substring(3, 3), input.Substring(7, 4));
    •      else
    •           return "(000) 000-0000";
    • }
  5. Write an extension method to validate if an email address is correctly formatted.
    • public static bool IsEmail(this string input)
    • {
    • var match = Regex.Match(input, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
    • return match.Success;
    • }
  6. Write an extension method to determine if the date provided is a weekend.
    • public static bool IsWeekend(this DateTime value)
    • {     
    •      return (value.DayOfWeek ==      DayOfWeek.Sunday || value.DayOfWeek == DayOfWeek.Saturday);
    • }
  7. Write an extension method used to check and simplify checking for null values. For example, rather than "if (i == null)" you could write "If i.IsNull()".
    public static bool IsNull(this object source) {     return source == null; }
  8. Write an extension method to return a date if one exists or throw an error.
    • public static DateTime ToDate(this string input, bool throwExceptionIfFailed = false)
    • {
    •      DateTime result;
    •      var valid = DateTime.TryParse(input, out result);
    •      if (!valid)
    •            if (throwExceptionIfFailed)
    •                throw new FormatException(string.Format("'{0}' cannot be converted as DateTime", input));
    •      return result;
    • }

Card Set Information

Author:
mateotete
ID:
335114
Filename:
StudyMaterial.Utilitities.Extensions
Updated:
2017-10-15 13:32:15
Tags:
Extensions
Folders:

Description:
Programming Extensions: StringExtensions, DateExtensions, NumericExtensions
Show Answers:

Home > Flashcards > Print Preview