BUG记录 c# static List Clear() 函数

本文探讨了在清空静态列表(staticList)时遇到的问题,即List.Clear()方法未能完全清空列表,导致数据残留。文章提供了一种替代方案,通过for循环结合List.Remove()方法逐一删除元素,有效避免了数据残留问题。

之前做了一个购物车功能 声明了一个 static List 辅助数据的常驻
然后清空购物车的时候 List.Clear() 了一下 断点的Count ==0 Clear前的数量是 3
如果这时候再次请求页面访问List的时候本来的数据其实还是存在的
可能是Clear清除不了 static List 的数据
解决方案 remove

for( int i=0;i<List.Count;i++){
	List.Remove(List[i]);
}

RemoveAll的方法这里不贴了
然后删除元素不要用 foreach 会出现异常

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using LitJson; using 项目.Model; using 项目.View; namespace 项目.Controler { internal class Form1Control { public static event Action ShowDataGridView; public static DataGridView dataGrid; public static TextBox textBox1; public static TextBox textBox2; static List<UserMange> list = new List<UserMange>(); public Form1Control(Form1 form1) { ShowDataGridView += LoadDataGridBView; Form1.Login += LoginUser; foreach (Control item in form1.Controls) { if (item is DataGridView) { if (item.Tag.ToString() == "1") { dataGrid = item as DataGridView; } } else if (item is TextBox) { if (item.Tag.ToString() == "2") { textBox1 = item as TextBox; } else if (item.Tag.ToString() == "3") { textBox2 = item as TextBox; } } } } public static bool LoginUser() { try { foreach (UserMange item in list) { if (textBox1.Text == item.UserName && textBox2.Text == item.PassWord) { Form3 form3 = new Form3(); form3.ShowDialog(); return true; } else if (textBox1.Text == item.UserName && textBox2.Text != item.PassWord) { MessageBox.Show("密码错误"); return false; } else if (textBox1.Text != item.UserName) { MessageBox.Show("用户不存在"); return false; } } } catch (Exception e) { MessageBox.Show("登录失败" + e.Message); return false; } return true; } public static void LoadDataGridBView() { string jsonStr = IOManage.ReadIo(UserDataPath.USER_DATA_PATH); Console.WriteLine("jsonStr的日志为" + jsonStr); JsonData jsonData = JsonManage.ReadJson(jsonStr); if (jsonData == null && !jsonData.IsArray) { MessageBox.Show("数据格式错误"); return; } try { foreach (JsonData item in jsonData) { if (item.ContainsKey("UserName") && item.ContainsKey("PassWord")) { list.Add(new UserMange() { UserName = item["UserName"].ToString(), PassWord = item["PassWord"].ToString() }); } } dataGrid.DataSource = null; // 避免绑定冲突 dataGrid.DataSource = list; } catch (Exception e) { MessageBox.Show("list载入错误: " + e.Message); Console.WriteLine("list载入错误详情: " + e); } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using 项目.Model; using 项目.View; using LitJson; using System.IO; using System.Windows.Forms; using System.Text.RegularExpressions; // 引入正则表达式命名空间 namespace 项目.Controler { internal class Form2Control { public TextBox textBox1; public TextBox textBox2; public TextBox textBox3; public TextBox textBox4; public TextBox textBox5; public TextBox textBox6; public TextBox textBox7; private List<UserMange>list=new List<UserMange>() ; public Form2Control(Form2 form2) { foreach (Control item in form2.Controls) { if (item is TextBox) { if (item.Tag.ToString() == "100") { textBox1 = (TextBox)item; } else if (item.Tag.ToString() == "200") { textBox2 = (TextBox)item; } else if (item.Tag.ToString() == "300") { textBox3 = (TextBox)item; } else if (item.Tag.ToString() == "400") { textBox4 = (TextBox)item; } else if (item.Tag.ToString() == "500") { textBox5 = (TextBox)item; } else if (item.Tag.ToString() == "600") { textBox6 = (TextBox)item; } else if (item.Tag.ToString() == "700") { textBox7 = (TextBox)item; } } } } public void Register() { try { // 用户名正则表达式:只能包含字母、数字、下划线,长度3-20 string usernamePattern = @"^\w{3,20}$"; // 密码正则表达式:至少6位,包含字母和数字 string passwordPattern = @"^(?=.*[A-Za-z])(?=.*\d).{6,}$"; if (!Regex.IsMatch(textBox1.Text, usernamePattern)) { MessageBox.Show("用户名格式不正确。\n只能包含字母、数字、下划线,长度3-20"); return; } if (!Regex.IsMatch(textBox2.Text, passwordPattern)) { MessageBox.Show("密码格式不正确。\n至少6位,必须包含字母和数字"); return; } // 文件创建与JSON读取部分保持不变 bool isFileCreated = IOManage.CreateFile(UserDataPath.USER_DATA_PATH); if (isFileCreated) { MessageBox.Show("无法创建文件"); return; } string jsonContent = IOManage.ReadIo(UserDataPath.USER_DATA_PATH); JsonData jsonData; if (string.IsNullOrEmpty(jsonContent)) { jsonData = new JsonData(); jsonData.SetJsonType(JsonType.Array); } else { try { jsonData = JsonManage.ReadJson(jsonContent); } catch (JsonException ex) { MessageBox.Show($"JSON 解析错误: {ex.Message}"); Console.WriteLine($"JSON 解析错误: {ex.Message}"); return; } } if (jsonData.IsArray) { foreach (JsonData item in jsonData) { if (item.ContainsKey("UserName") && textBox1.Text == item["UserName"].ToString()) { MessageBox.Show("用户名已存在"); return; } } } else { MessageBox.Show("JSON数据格式不正确"); Console.WriteLine("JSON数据格式不正确"); return; } list.Clear(); foreach (JsonData item in jsonData) { if (item.ContainsKey("UserName") && item.ContainsKey("passWord")) { list.Add(new UserMange { UserName = item["UserName"].ToString(), PassWord = item["PassWord"].ToString(), }); } } list.Add(new UserMange { UserName = textBox1.Text, PassWord = textBox2.Text }); string updatedJson = JsonManage.WriteJson(list); Console.WriteLine($"Updated JSON content: {updatedJson}"); IOManage.WriteIo(UserDataPath.USER_DATA_PATH, updatedJson); MessageBox.Show("注册成功"); } catch (Exception ex) { MessageBox.Show($"发生错误: {ex.Message}"); Console.WriteLine($"发生错误: {ex.Message}"); } } } } 纠错
最新发布
08-17
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值