如何将单个对象转换为IEnumerable<T>

Sep 12 2019

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static class IEnumerableExt
{
/// <summary>
/// Wraps this object instance into an IEnumerable&lt;T&gt;
/// consisting of a single item.
/// </summary>
/// <typeparam name="T"> Type of the object. </typeparam>
/// <param name="item"> The instance that will be wrapped. </param>
/// <returns> An IEnumerable&lt;T&gt; consisting of a single item. </returns>
public static IEnumerable<T> Yield<T>(this T item)
{
yield return item;
}
}
1
2
3
new T[] { item }
// or
new [] { item }
1
2
3
4
public static IEnumerable<T> ToEnumerable<T>(params T[] items)
{
return items;
}
C#首字母大写方法

Jun 13 2019

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// Returns the input string with the first character converted to uppercase
/// </summary>
public static string FirstLetterToUpperCase(this string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentException("There is no first letter");

char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
C#获取枚举值的DescriptionAttribute内容

Apr 05 2018

1
2
3
4
5
6
7
8
9
10
11
12
public static string GetDescription<T>(this T source)
{
FieldInfo fi = source.GetType().GetField(source.ToString());

DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);

if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return source.ToString();
}
1
2
3
// In Enums.NET you'd use:

string description = ((MyEnum)value).AsString(EnumFormat.Description);
Hello World

Nov 06 2013

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment