C# WriteLine中的System.FormatException

C# WriteLine中的System.FormatException,c#,console.writeline,formatexception,C#,Console.writeline,Formatexception,我的代码有问题,出现以下异常: System.FormatException 其他信息:输入字符串的格式不正确 我的visual studio C#解决方案中有两个文件: Program.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EventPubSub { class

我的代码有问题,出现以下异常:

System.FormatException

其他信息:输入字符串的格式不正确

我的visual studio C#解决方案中有两个文件:

  • Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EventPubSub
    {
        class Program
        {
            static void Main(string[] args)
            {
                Rectangle rect = new Rectangle();
                // Subscribe to the Changed event
                rect.Changed += new EventHandler(Rectangle_Changed);
                rect.Length = 10;
            }
            static void Rectangle_Changed(object sender, EventArgs e)
            {
                Rectangle rect = (Rectangle)sender;
                Console.WriteLine("Value Changed: Length = { 0}", rect.Length);
            }
        }
    }
    
  • 文件
    Rectangle.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EventPubSub
    {
        class Rectangle
        {
            //Declare an event named Changed of
            //delegate type EventHandler
    
            public event EventHandler Changed;
    
            private double length = 5;
    
            public double Length
            {
                get
                {
                    return length;
                }
                set
                {
                    length = value;
                    //Publish the Changed event
                    Changed(this, EventArgs.Empty);
                }
            }
        }
    }
    

  • 当我执行该行时会出现异常:
    rect.Length=10

    首先尝试添加
    Try catch
    以捕获它抛出的错误。所以你可以识别并修复它。这只是为了帮助您下次解决自己的问题。:)


    请像这样更改事件处理程序,一切正常

    static void Rectangle_Changed(object sender, EventArgs e)
    {
        Rectangle rect = (Rectangle)sender;
        Console.WriteLine(string.Format("Value Changed: Length = {0}", rect.Length));
    }
    
    我在这里做了两个改变-

  • 添加了string.Format(这不是问题所在)

  • 删除了
    {
    0
    之间的空格。原来是
    {0}
    现在我做了
    {0}
    (这是实际问题)


  • 处理程序中的
    {
    0
    之间有一个空格,导致异常

    执行此操作后,我收到一条消息:输入字符串的格式不正确。这意味着您的问题在消息部分。我只是在这里教您下次如何处理此类错误。“教一个人如何钓鱼比喂他鱼好”。;)Console.WriteLine(String.Format(…)是毫无意义的重复,但在其他方面是很好的答案。@AlexeiLevenkov是对的。有或没有String格式都是一样的。添加它是毫无意义的,因为它们是等价的。这里的错误仅仅是两者之间的空格{和0。@AlexeiLevenkov,哦,是的!当然……我会对我的答案进行编辑,以突出显示
    FormatException
    不是由于
    int.Parse(“bob”)
    造成的罕见情况。。。
    static void Rectangle_Changed(object sender, EventArgs e)
    {
        Rectangle rect = (Rectangle)sender;
        Console.WriteLine(string.Format("Value Changed: Length = {0}", rect.Length));
    }