<bdo id='dEqrw'></bdo><ul id='dEqrw'></ul>
    <tfoot id='dEqrw'></tfoot>

      <small id='dEqrw'></small><noframes id='dEqrw'>

      <i id='dEqrw'><tr id='dEqrw'><dt id='dEqrw'><q id='dEqrw'><span id='dEqrw'><b id='dEqrw'><form id='dEqrw'><ins id='dEqrw'></ins><ul id='dEqrw'></ul><sub id='dEqrw'></sub></form><legend id='dEqrw'></legend><bdo id='dEqrw'><pre id='dEqrw'><center id='dEqrw'></center></pre></bdo></b><th id='dEqrw'></th></span></q></dt></tr></i><div id='dEqrw'><tfoot id='dEqrw'></tfoot><dl id='dEqrw'><fieldset id='dEqrw'></fieldset></dl></div>
      <legend id='dEqrw'><style id='dEqrw'><dir id='dEqrw'><q id='dEqrw'></q></dir></style></legend>
    1. TypeConverter vs. Convert vs. TargetType.Parse

      时间:2023-06-09
      <legend id='VtlFo'><style id='VtlFo'><dir id='VtlFo'><q id='VtlFo'></q></dir></style></legend>
        <tbody id='VtlFo'></tbody>
          <bdo id='VtlFo'></bdo><ul id='VtlFo'></ul>

        • <small id='VtlFo'></small><noframes id='VtlFo'>

          <tfoot id='VtlFo'></tfoot>

                <i id='VtlFo'><tr id='VtlFo'><dt id='VtlFo'><q id='VtlFo'><span id='VtlFo'><b id='VtlFo'><form id='VtlFo'><ins id='VtlFo'></ins><ul id='VtlFo'></ul><sub id='VtlFo'></sub></form><legend id='VtlFo'></legend><bdo id='VtlFo'><pre id='VtlFo'><center id='VtlFo'></center></pre></bdo></b><th id='VtlFo'></th></span></q></dt></tr></i><div id='VtlFo'><tfoot id='VtlFo'></tfoot><dl id='VtlFo'><fieldset id='VtlFo'></fieldset></dl></div>
              1. 本文介绍了TypeConverter vs. Convert vs. TargetType.Parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                据我所知,在.NET中转换数据类型至少有3种方式:

                As far as I know, there are at least 3 ways to convert data types in .NET:

                使用 System.ComponentModel.TypeConverter

                var conv = System.ComponentModel.TypeDescriptor.GetConverter(typeof(int));
                var i1 = (int)conv.ConvertFrom("123");
                

                使用 System.Convert.ChangeType():

                var i2 = (int) Convert.ChangeType("123", typeof (int));
                

                使用目标类型的 Parse/TryParse 方法:

                using the Parse/TryParse methods of the destination type:

                var i3 = int.Parse("123"); // or TryParse
                


                是否有任何指南或经验法则何时使用哪种方法在 .NET 基本数据类型之间进行转换(尤其是从字符串到其他数据类型)?

                Are there any guidelines or rules-of-thumb when to use which method to convert between the .NET base data types (especially from string to some other data type)?

                推荐答案

                我要晚 6 年在这里发帖,因为我认为这是一个很好的问题,我对现有的答案并不满意.

                I'm going to post here 6 years late, because I think this is a good question and I am not satisfied with the existing answers.

                静态 Parse/TryParse 方法只能在当您想要从字符串转换为具有这些方法的类型 时使用.(如果您预计转换可能会失败,请使用 TryParse).

                The static Parse/TryParse methods can be used only when you want to convert from string to the type that has those methods. (use TryParse when you expect that the conversion may fail).

                System.Convert 的意义在于,它的 文档 说,将从一种基本数据类型转换为另一种基本数据类型.请注意,使用 Convert 时,您还可以使用获取 Object 并自行确定如何转换它的方法.

                The point of System.Convert is, as its documentation says, to convert from a base data type to another base data type. Note that with Convert you also have methods that take an Object and figure out by themselves how to convert it.

                至于System.ComponentModel.TypeConverter,作为typeconverter"堆栈溢出标签的文档,当您想要提供类实例的文本表示以供设计器序列化与字符串之间的转换> 或用于在属性网格中显示

                As to System.ComponentModel.TypeConverter, as the "typeconverter" stack overflow tag's documentation, they are used primarily to convert to and from string, when you want to provide a text representation of a class instance for use by designer serialization or for display in property grids

                这篇关于TypeConverter vs. Convert vs. TargetType.Parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 C# 中将数据从字符串转换为长整数 下一篇:如何查找和调用特定类型的 .Net TypeConverter?

                相关文章

                <legend id='z7giO'><style id='z7giO'><dir id='z7giO'><q id='z7giO'></q></dir></style></legend>

                  <small id='z7giO'></small><noframes id='z7giO'>

                    <i id='z7giO'><tr id='z7giO'><dt id='z7giO'><q id='z7giO'><span id='z7giO'><b id='z7giO'><form id='z7giO'><ins id='z7giO'></ins><ul id='z7giO'></ul><sub id='z7giO'></sub></form><legend id='z7giO'></legend><bdo id='z7giO'><pre id='z7giO'><center id='z7giO'></center></pre></bdo></b><th id='z7giO'></th></span></q></dt></tr></i><div id='z7giO'><tfoot id='z7giO'></tfoot><dl id='z7giO'><fieldset id='z7giO'></fieldset></dl></div>
                  1. <tfoot id='z7giO'></tfoot>

                      <bdo id='z7giO'></bdo><ul id='z7giO'></ul>