You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#基于Model和List实现待办事项删除与编辑的方法咨询

实现C#待办事项应用的删除与编辑功能

首先先修正你现有代码里的问题:比如add方法写法错误,Todo类的可空属性没必要(创建待办项时这些值都应为确定值),同时补充ID生成逻辑。接下来一步步实现删除和编辑功能:

一、先修正现有代码问题

  1. 完善add方法:生成唯一ID(基于当前列表最大ID+1),接收用户输入标题后创建Todo实例并加入列表
  2. 调整Todo类:使用非可空属性,遵循C# PascalCase命名规范,保证数据完整性

二、实现删除功能

核心逻辑是根据ID定位待办项并移除:

  • 先判断列表是否为空,为空则直接提示
  • 接收用户输入的待办ID,用int.TryParse处理输入避免异常
  • 查找匹配ID的待办项,存在则移除,不存在提示用户

三、实现编辑功能

核心逻辑是找到目标待办项后,允许用户修改标题或切换完成状态:

  • 同样先判断列表是否为空
  • 定位到目标待办项后,提供编辑选项供用户选择
  • 根据选择更新对应属性

修改后的Todo.cs

internal class Todo
{
    public int Id { get; set; }
    public string Title { get; set; }
    public bool IsCompleted { get; set; }

    public Todo(int id, string title, bool isCompleted)
    {
        Id = id;
        Title = title;
        IsCompleted = isCompleted;
    }
}

修改并完善后的Program.cs

List<Todo> todos = new List<Todo>();
string select;

while (true)
{
    Console.WriteLine("Select: \n---------\n[1] Exit\n[2] List\n[3] Add\n[4] Delete\n[5] Edit");
    select = Console.ReadLine()!;

    switch (select)
    {
        case "1":
            Exit();
            break;
        case "2":
            ListTodos();
            break;
        case "3":
            AddTodo();
            break;
        case "4":
            DeleteTodo();
            break;
        case "5":
            EditTodo();
            break;
        default:
            Console.WriteLine("请输入1-5之间的数字!");
            continue;
    }
}

void Exit()
{
    Console.WriteLine("Goodbye...");
    Environment.Exit(0);
}

void ListTodos()
{
    if (todos.Count > 0)
    {
        foreach (var item in todos)
        {
            Console.WriteLine("ID: {0}\n标题: {1}\n是否完成: {2}\n---", item.Id, item.Title, item.IsCompleted ? "是" : "否");
        }
    }
    else
    {
        Console.WriteLine("待办列表为空。");
    }
}

void AddTodo()
{
    Console.WriteLine("请输入待办事项标题:");
    string title = Console.ReadLine()!;
    // 生成唯一ID:列表为空则设为1,否则取最大ID+1
    int newId = todos.Count == 0 ? 1 : todos.Max(t => t.Id) + 1;
    todos.Add(new Todo(newId, title, false));
    Console.WriteLine("待办事项添加成功!");
}

void DeleteTodo()
{
    if (todos.Count == 0)
    {
        Console.WriteLine("待办列表为空,无需删除。");
        return;
    }

    Console.WriteLine("请输入要删除的待办事项ID:");
    if (int.TryParse(Console.ReadLine(), out int deleteId))
    {
        var targetTodo = todos.FirstOrDefault(t => t.Id == deleteId);
        if (targetTodo != null)
        {
            todos.Remove(targetTodo);
            Console.WriteLine("删除成功!");
        }
        else
        {
            Console.WriteLine("未找到ID为{0}的待办事项。", deleteId);
        }
    }
    else
    {
        Console.WriteLine("请输入有效的数字ID!");
    }
}

void EditTodo()
{
    if (todos.Count == 0)
    {
        Console.WriteLine("待办列表为空,无需编辑。");
        return;
    }

    Console.WriteLine("请输入要编辑的待办事项ID:");
    if (int.TryParse(Console.ReadLine(), out int editId))
    {
        var targetTodo = todos.FirstOrDefault(t => t.Id == editId);
        if (targetTodo != null)
        {
            Console.WriteLine("请选择编辑操作:\n[1] 修改标题\n[2] 切换完成状态");
            string editSelect = Console.ReadLine()!;
            switch (editSelect)
            {
                case "1":
                    Console.WriteLine("请输入新的标题:");
                    targetTodo.Title = Console.ReadLine()!;
                    Console.WriteLine("标题修改成功!");
                    break;
                case "2":
                    targetTodo.IsCompleted = !targetTodo.IsCompleted;
                    Console.WriteLine("完成状态已切换为:{0}", targetTodo.IsCompleted ? "是" : "否");
                    break;
                default:
                    Console.WriteLine("请输入有效的选项!");
                    break;
            }
        }
        else
        {
            Console.WriteLine("未找到ID为{0}的待办事项。", editId);
        }
    }
    else
    {
        Console.WriteLine("请输入有效的数字ID!");
    }
}

关键说明

  • 用int.TryParse处理用户输入,避免非数字输入引发的异常
  • 删除和编辑均通过FirstOrDefault根据ID精准定位待办项
  • 编辑功能提供两种常用操作:修改标题、切换完成状态,贴合待办应用实际需求
  • 遵循C#编码规范,用PascalCase命名属性和方法,提升代码可读性

内容的提问来源于stack exchange,提问作者Emir Bolat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 11:25:36