【问题标题】:String to numeric conversion in c#c#中字符串到数字的转换
【发布时间】:2013-03-26 07:12:15
【问题描述】:

我正在尝试编写一个函数来将string“12345”的内容转换为int

如果字符串为空,我想返回 null(未初始化),而不是值 0

问题是,函数不会返回未初始化的值。

我的代码无法编译,因为 Retval 会返回一个未初始化的值......

到目前为止我的尝试:

public int ConvertStringToNumber(String TheString)
{
    // Uninitialized
    int Retval;

    if (TheString.Length > 0)
    {
        // We have a valid string
        if (Int32.TryParse(TheString, out Retval))
        {
            // We have a valid Number
        }
    }

    // Return the number or null
    return Retval;
}

【问题讨论】:

    标签: c# string numbers


    【解决方案1】:

    你可以使用 Nullable int 吗?它将允许设置为 nullable 。见这里:http://www.codeproject.com/Articles/11854/C-2-0-Nullable-Types

    【讨论】:

      【解决方案2】:

      您可以使用可为空的 int(更多信息 here)。

      可空类型可以表示底层类型的所有值,并且 一个额外的空值。

      public int? ConvertStringToNumber(String TheString)
      {
          int retval;
          bool isInt = Int32.TryParse(TheString, out retval);
          return isInt ? retval : null;
      }
      

      注意:使用可为空的类型时,您需要使用强制转换或获取它的值。见here

      示例:

      int? n = ConvertStringToNumber("123");
      
      int value = n.Value;
      // or
      int value = (int)n;
      

      【讨论】:

        【解决方案3】:

        如果您在第一次为 Retval 对象分配了一个值,则该值仅在该区域有效。 所以,当你返回它时,Retval 是空的。

        【讨论】:

          【解决方案4】:

          因为 Int32.TryParse(TheString, out Retval) 要求 int 类型不可为空

          public int? ConvertStringToNumber(String TheString)
          {
          
              // Uninitialized
              int Retval;
          
              if (TheString.Length > 0)
              {
          
                  // We have a valid string
          
                  if (Int32.TryParse(TheString, out Retval))
                  {
          
                      // We have a valid Number
                      return Retval;
                  }
          
              }
          
                      // Return the number or null
          
              return null;
          }
          

          【讨论】:

            【解决方案5】:

            解决您问题的简单扩展方法

            using System;
            
            namespace temp
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        string valu = "";
                        Console.WriteLine(valu.ToInt32());
                        Console.ReadLine();
                    }
                }
                public static class MyExtentions
                {
                    public static int ToInt32(this string s)
                    {
                        int x;
                        if (s != null)
                        {
                            if (s.Length > 1)
                                x = Convert.ToInt32(s);
                            else
                                x = 0;
                        }
                        else
                        {
                           x= 0;
                        }
                        return x;
                    }
                }
            }
            

            【讨论】:

              【解决方案6】:
              int? x = ConvertStringToNumber("1");
              

              int 值 = x.Value;

              c#中字符串到数值的转换

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-01-27
                • 2018-09-02
                • 1970-01-01
                • 1970-01-01
                • 2012-05-04
                • 2011-03-25
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多