-->

错误:扩展方法必须在顶级静态类中定义(CS1109)(Error: Extension method

2019-07-02 14:42发布

我试图做一个倒计时程序,我可以启动和停止,如果需要倒计时的值设置为10分钟。

但我得到一个错误,我不太明白。 我不是到C#,所以这里的代码:

可有一个人帮我一点吗? 我想我的框架3.0什么跑?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;

namespace PauseMaster
{   
    public partial class MainForm : Form
    {           
        public MainForm()
        {           
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {               
        }

        private DateTime endTime;
        private void btnStartStop_Click(object sender, EventArgs e)
        {
            if(btnStartStop.Text == "START")
            {
                int hours = int.Parse(string.IsNullOrEmpty(txtCountFromHour.TextBox.Text) || txtCountFromHour.TextBox.Text == "timer" ? "0" : txtCountFromHour.TextBox.Text);
                int mins = int.Parse(string.IsNullOrEmpty(txtCountFromMin.TextBox.Text) || txtCountFromMin.TextBox.Text == "minutter" ? "0" : txtCountFromMin.TextBox.Text);
                int secs = int.Parse(string.IsNullOrEmpty(txtCountFromSec.TextBox.Text) || txtCountFromSec.TextBox.Text == "sekunder" ? "0" : txtCountFromSec.TextBox.Text);

                endTime = DateTime.Now.AddHours(hours).AddMinutes(mins).AddSeconds(secs);

                timer1.Enabled = true;  
                btnStartStop.Text = "STOP";
            }
            else
            {
                timer1.Enabled = false;
                btnStartStop.Text = "START";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (endTime <= DateTime.Now)
            {
                timer1.Enabled = false;
                lblTimer.Text = "00:00:00";
                btnStartStop.Text = "Start";
                return;
            }

            TimeSpan ts = endTime - DateTime.Now;
            lblTimer.Text = string.Format("{0}:{1}:{2}.{3}", ts.Hours.AddZero(), ts.Minutes.AddZero(), ts.Seconds.AddZero());
        }

        public static class IntExt
        {
            public static string AddZero(this int i) // GETTING ERROR HERE AT 'AddZero'
            {
                int totLength = 2;
                int limit = (int)Math.Pow(10, totLength - 1);
                string zeroes = "";
                for (int j = 0; j < totLength - i.ToString().Length; j++)
                {
                    zeroes += "0";
                }
                return i < limit ? zeroes + i : i.ToString();
            }
        }     
    }       
}

Answer 1:

该错误消息说,究竟出了什么问题:你的IntExt方法不是顶级静态类。 这是一个嵌套的静态类。 刚拉出来的MainForm ,它会被罚款。



Answer 2:

我知道这个问题是旧的,但万一别人运行到这个问题?

检查以确保关键字“这”不是在参数名称定义(通常从复制/粘贴或拖)

因此,更换:

public static string AddZero(this int i)

有了这个:

public static string AddZero(int i)

这应该解决您的问题。



文章来源: Error: Extension methods must be defined in a top level static class (CS1109)