What is the difference between Convert and Parse?

前端 未结 4 867
广开言路
广开言路 2020-12-20 22:17

I could write the following to convert an object to an integer.

Convert.ToInt32(myObject);

But I could also write

Int.Parse         


        
4条回答
  •  盖世英雄少女心
    2020-12-20 22:44

    This how Convert.ToInt32 method source looks like

    public static int ToInt32(object value) {
        return value == null? 0: ((IConvertible)value).ToInt32(null); 
    }
    

    As long as your object implement IConvertible interface you should call this method.

提交回复
热议问题