System.Collections.Generic.List'需要& # 39;1 & # 39;类型参数
本文关键字:类型参数 需要 Collections Generic List System | 更新日期: 2023-09-27 18:04:15
我在c#中创建了一个代码,但是我有这个错误:
错误1使用泛型类型'System.Collections.Generic.List'C:'Users'Abdelhakim'Desktop'Wind applicationc# '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);
}
}
}
using
指令只能用于命名空间,不能用于单独的类型。
改变这一行
using System.Collections.Generic.List;
using System.Collections.Generic;
要解决您的下一个问题,您需要更改这一行:
ls.Items.Add(ls[i]);
ls.Add(ls[i]);
应该允许您的代码编译,然而,这将导致无限循环,因为您不断地将相同的项添加回列表中。您需要重新考虑您在button1_Click
方法中要做的事情。