c#linq增加新的一条记录同时删除最后一条代码
时间: 2023-12-08 10:03:08 浏览: 68
一个B样条C#程序源代码
4星 · 用户满意度95%
Sure! Here's an example code snippet in C# using LINQ to add a new record to a list and delete the last record:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<string> records = new List<string>() { "Record 1", "Record 2", "Record 3" };
// Adding a new record
string newRecord = "Record 4";
records.Add(newRecord);
// Deleting the last record
string lastRecord = records.Last();
records.Remove(lastRecord);
// Printing the updated records
foreach (string record in records)
{
Console.WriteLine(record);
}
}
}
```
In this example, we start with a `List<string>` called `records` containing three records. We add a new record using the `Add()` method, and then delete the last record using the `Last()` method to retrieve it and the `Remove()` method to remove it. Finally, we print the updated records using a foreach loop.
阅读全文