拧一个类似.ToString()的方法 [英] Wring a method like .ToString()
本文介绍了拧一个类似.ToString()的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想要像.ToString()
这样的方法
说int i = 100;
如果i.ToCustomInt();
那么"i"的输出应显示105(i +某个常数(5),然后输出将为105);
我尝试使用密封的类和静态方法"ToCustomInt()".
想知道如何在静态方法中获取该"i"值..
Hi to all,
I would like to have method like .ToString()
say int i=100;
if i.ToCustomInt();
then out put of "i" should show 105(i+some constant value(5) then out put would be 105);
i tried to have sealed class and static method "ToCustomInt()".
wondering how to get that "i" value inside of the static method..
is there any suggestion please.
推荐答案
这可以通过扩展方法"来完成.检查以下知识库文章:
http://msdn.microsoft.com/en-us/library/bb383977.aspx [ ^ ]
This can be done by "Extension Methods". check the following KB article:
http://msdn.microsoft.com/en-us/library/bb383977.aspx[^]
我同意Om Prakash Pant,这是扩展方法的工作.
这是一个例子
I gree with Om Prakash Pant, this is a job for an extension method.
Here is an example
public static class IntExtensions
{
public static int ToCustomInt(this Int32 i)
{
return i + 5;
}
}
这篇关于拧一个类似.ToString()的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文