c#搜索记录内容相同,只记一次代码
时间: 2023-12-07 13:36:03 浏览: 21
你可以使用一个字典(Dictionary)来实现记录内容相同的搜索记录只记一次的功能。字典的键可以是搜索内容,值可以是搜索记录的次数。
下面是一个示例代码:
```csharp
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> searchRecords = new Dictionary<string, int>();
string searchContent = "搜索内容";
if (searchRecords.ContainsKey(searchContent))
{
searchRecords[searchContent]++;
}
else
{
searchRecords[searchContent] = 1;
}
Console.WriteLine($"搜索内容 '{searchContent}' 出现了 {searchRecords[searchContent]} 次。");
}
}
```
在上面的示例中,我们使用了一个名为 `searchRecords` 的字典来存储搜索记录。当搜索内容已经存在于字典中时,我们将相应的值加1;否则,我们将搜索内容添加为新的键,并将值设置为1。
你可以根据自己的需求扩展该代码,比如可以将其封装成一个函数,以便在多个地方复用。