我在C#中创建了一个代码,但是我遇到了这个错误:
错误1使用泛型类型'System.Collections.Generic.List' 需要'1'类型参数C:\ Users \ Abdelhakim \ Desktop \ Wind Applic C#\ Exercice4 \ Exercice4 \ Form1.cs 9 34 Exercice4
这是我写的代码:
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.Collections.Generic.List;
namespace Exercice4
{
public partial class Form1 : Form
{
List<string> ls = new List<string>();
string p;
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void r1_CheckedChanged(object sender, EventArgs e)
{
p = "+";
l3.Text = (Convert.ToDouble(t1.Text) + Convert.ToDouble(t2.Text)).ToString();
}
private void r2_CheckedChanged(object sender, EventArgs e)
{
p = "-";
l3.Text = (Convert.ToDouble(t1.Text) - Convert.ToDouble(t2.Text)).ToString();
}
private void r3_CheckedChanged(object sender, EventArgs e)
{
p = "*";
l3.Text = (Convert.ToDouble(t1.Text) * Convert.ToDouble(t2.Text)).ToString();
}
private void r4_CheckedChanged(object sender, EventArgs e)
{
p = "/";
if (Convert.ToDouble(t2.Text) == 0)
l3.Text = "Erreur";
else
l3.Text = (Convert.ToDouble(t1.Text) / Convert.ToDouble(t2.Text)).ToString();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < ls.Count; i++)
ls.Items.Add(ls[i]);
}
private void button2_Click(object sender, EventArgs e)
{
ls.Add(t1.Text + p + t2.Text + " =" + l3.Text);
}
}
}
答案 0 :(得分:2)
using
directive只能用于名称空间,而不能用于单个类型。
更改此行
using System.Collections.Generic.List;
要
using System.Collections.Generic;
要解决下一个问题,您需要更改此行:
ls.Items.Add(ls[i]);
要
ls.Add(ls[i]);
这应该允许您的代码进行编译,但是,这会导致无限循环,因为您会不断将相同的项目添加回列表中。您需要重新考虑在button1_Click
方法中尝试执行的操作。