I'm wondering something. Instead of writing String.Format("{0:X}", num);
to convert numbers to hex. Is there a way where i could extend string directly so that i could simply write num.ToHex();
instead?
问问题
52 次
2 回答
6
您可以创建扩展方法:
public static class IntExtensions
{
public static string ToHex(this int source)
{
return string.Format("{0:X}", source);
}
}
像这样执行:
string hexNum = 1234.ToHex();
于 2013-07-28T09:57:04.363 回答
2
它被称为扩展方法。但是,它应该设置为数字类型,以允许{0:X}
字符串格式:
public static class Extensions
{
public static string ToHex(this int source)
{
return string.Format("{0:X}", source);
}
}
于 2013-07-28T09:58:06.733 回答