четверг, 1 сентября 2011 г.

Полезные расширяющие методы

То ли где то увидел, то ли сам придумал.. в обще мне помню, откуда взял эти расширяющие методы, но мне с ними намного удобнее.
  1. namespace AMuradov.Extensions
  2. {
  3.   public static class StringExtensions
  4.   {
  5.     public static string StringFormat(this string source, object arg)
  6.     {
  7.       return string.Format(source, arg);
  8.     }
  9.  
  10.     public static string StringFormat(this string source, object arg0, object arg1)
  11.     {
  12.       return string.Format(source, arg0, arg1);
  13.     }
  14.  
  15.     public static string StringFormat(this string source, object arg0, object arg1, object arg2)
  16.     {
  17.       return string.Format(source, arg0, arg1, arg2);
  18.     }
  19.  
  20.     public static string StringFormat(this string source, params object[] args)
  21.     {
  22.       return string.Format(source, args);
  23.     }
  24.  
  25.     public static bool IsNullOrEmptyString(this string source)
  26.     {
  27.       return string.IsNullOrEmpty(source);
  28.     }
  29.   }
  30. }
* This source code was highlighted with Source Code Highlighter.

Этот код можно использовать так:

  1. var someString = "{0}";
  2. //...
  3. if (!someString.IsNullOrEmptyString())
  4. {
  5.   var text = someString.StringFormat(10);
  6.   // Что то делаем с этим текстом
  7. }
* This source code was highlighted with Source Code Highlighter.

Комментариев нет:

Отправить комментарий