关于ToString()方法? [英] about ToString() method?

查看:54
本文介绍了关于ToString()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设.net框架中没有toString方法,我们如何实现呢?

Hi,
Suppose toString method is not present in .net framework , how we can implement it?

推荐答案





你可以找到几种方式



如我所知:





Hi,

You can find several ways

as Per me something like as follows:


public interface IStringable
    {
        string ToString();
    }
    public class Stringable : IStringable
    {
        public string ToString()
        {
            return this.GetType().FullName;
        }
    }





另一种方式





Another way

public static class ExtentionBase
{
public static string ToString(this Object Obj)
{
if(Obj is IStringable)
return ((IStringable)Obj).ToString();
else
return Obj.GetType().FullName;
}
}


自定义ToString方法 [ ^ ]


仅用于提问的问题。使用.ToString()总是

使用这样的自定义函数



An anwser to ur question only. use ".ToString()" always
use custom function like this

private static string toString(object value)
{
          return "" + value + "";
}





如下呼叫功能





call function like below

//string result= toString((object)textBox1.Text);
string result= toString((object)15);





祝你好运; - )



good luck ;-)


这篇关于关于ToString()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆