thi*_*eek 4 c# extension-methods c#-3.0
我正在C#中为String和DateTime实用程序函数编写扩展方法库.你可以通过建议你想成为它的一部分的String和DateTime的有用的功能来帮助我吗?根据你的建议,我可以使它更具凝聚力和集体性.
谢谢!
public static bool IsNullOrEmpty(this string value){
return string.IsNullOrEmpty(value);
}
public static string Reverse(this string value) {
if (!string.IsNullOrEmpty(value)) {
char[] chars = value.ToCharArray();
Array.Reverse(chars);
value = new string(chars);
}
return value;
}
public static string ToTitleCase(this string value) {
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
}
public static string ToTitleCaseInvariant(this string value) {
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(value);
}
Run Code Online (Sandbox Code Playgroud)
琐碎,但稍微好一点.
那些没有专门扩展字符串或DateTime的方法,而是定位或返回字符串或DateTime?然后你也可以构建一些int
和TimeSpan
方法,所以你可以编写流畅的接口,如:
DateTime yesterday = 1.Days().Ago();
Run Code Online (Sandbox Code Playgroud)
.
public static TimeSpan Days(this int value)
{
return new TimeSpan(value, 0, 0, 0);
}
public static TimeSpan Hours(this int value)
{
return new TimeSpan(value, 0, 0);
}
public static TimeSpan Minutes(this int value)
{
return new TimeSpan(0, value, 0);
}
//...
Run Code Online (Sandbox Code Playgroud)
.
public static DateTime Ago(this TimeSpan value)
{
return DateTime.Now.Add(value.Negate());
}
public static DateTime FromNow(this TimeSpan value)
{
return DateTime.Now.Add(value);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1613 次 |
最近记录: |