c#错误1:找不到类型或命名空间名称“ArrayList”(是否缺少 using 指令或程序集引用?)

2025-02-23 06:40:56
推荐回答(2个)
回答1:

引用命名空间:
using System.Collections;

不行哈,ArrayList是System.Collections下的一个类型,而不是System.Collections.Generic下的,如果你不引用可以直接这么写:
System.Collections.ArrayList = new System.Collections.ArrayList(arr);
上面得也是对的

用using System.Collections.Generic就没有把ArrayList这个类型引用进去,会报错

回答2:

{
public partial class Form1 : Form
{
ArrayList points;
Point currentPoint;
Pen thepen;
float penwidth;
SolidBrush thebrush;
private System.Windows.Forms.Button btnColor;
private System.Windows.Forms.ListBox lstPenStyle;
private System.Windows.Forms.CheckBox rbPen;
Color thecolor;
public Form1()
{
InitializeComponent();
currentPoint = new Point(50,10);
points = new ArrayList();
points.Add(currentPoint);
penwidth = 1;
thecolor = Color.Black;
thepen = new Pen(thecolor);
thebrush = new SolidBrush(thecolor);
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (currentPoint.X != e.X || currentPoint.Y != e.Y)
{
currentPoint.X = e.X;
currentPoint.Y = e.Y;
points.Add(currentPoint);
this.Invalidate();
this.Update();
}
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Point[] ps=(Point[]) points.ToArray(typeof(Point));
if (rbLine.Checked)
g.DrawLine(Pens.Black, ps[0], ps[ps.Length - 1]);
else if (rbRectangle.Checked)
g.DrawRectangle(thepen, ps[0].X, ps[0].Y, ps[ps.Length - 1].X - ps[0].X, ps[ps.Length - 1].Y - ps[ps.Length - 1].Y);
else if (rbCurve.Checked)
g.DrawCurve(thepen, ps);
else if (rbPolygon.Checked)
g.DrawPolygon(thepen, ps);
else if (rbEllipse.Checked)
g.DrawEllipse(thepen,ps[0].X,ps[0].Y,ps[ps.Length-1].X-ps[0].X,ps[ps.Length-1].Y-ps[0].Y);
}

private void ChangeDrawType(object sender, PaintEventArgs e)
{
points.Clear();
currentPoint = new Point(50,10);
points.Add(currentPoint);