Casting vs Converting an object toString, when object really is a string

后端 未结 9 2097
终归单人心
终归单人心 2020-11-28 06:13

This isn\'t really an issue, however I am curious. When I save a string in lets say an DataRow, it is cast to Object. When I want to use it, I have to cast it ToString. As f

9条回答
  •  星月不相逢
    2020-11-28 06:35

    Basically in your case it is better to leave type cast because .ToString() may hide bugs. For example, your data base schema changed and name is no longer of string type but with .ToString() your code still works. So in this case it is better to use type cast.

    Here is implementation of String.ToString() - nothing special =)

    public override string ToString()
    {
        return this;
    }
    

提交回复
热议问题