当前位置:网站首页>C#基于当前时间,获取唯一识别号(ID)的方法
C#基于当前时间,获取唯一识别号(ID)的方法
2022-07-02 09:38:00 【chengjl8】
在软件开发里,我们常常要区别各个类似的对象,比如批量生成的多个变量,多个标注等等。在PLC-Recorder开发的过程中,ID号的生成方法也经过了多次优化,才终于稳定下来,能够基于当前时间,生成不重复的ID号(突发需要量不是很大时有效),分享给大家,让朋友们也少走些弯路。
1、基于当前时间
由于软件可能会重启,因此,将历史的ID都记录下来,将是个非常大的负担。因此,基于当前时间进行ID生成,能避免与历史上ID的冲突。
Int64 ID = DateTime.Now.Ticks;
2、发现ID号的重复
仅用上述语句生成ID号是不够的,因为,批量生成时,他们是相同的,因此,需要查重。
将有效的ID存入字典,可以快速查重。程序自己生成ID后,会立即存入字典。如果是其他程序生成的,要注意也存入字典。
当然,用嘻哈表可能更简单,大家可以改进,我这边生成ID的动作不频繁,所以,无所谓。
3、发现重复时的处理
如果发现重复了,则将ID++,直到找到不重复的。然后将新的ID存入字典,并反馈出去。
/// <summary>
/// 采用字典方式
/// </summary>
/// <returns>新ID</returns>
public static Int64 IDGetNew()
{
//确认ID不重复
Int64 ID = DateTime.Now.Ticks;
while (LibID.ContainsKey(ID))
{
ID++;//获得一个新的ID
}
LibID.Add(ID, ID);
return ID;
}
public static void IDAdd(long ID)
{
if (LibID.ContainsKey(ID)) return;
LibID.Add(ID, ID);
}
private static Dictionary<long, long> LibID = new Dictionary<long, long>();2022年6月30日
边栏推荐
- [applinking practical case] share in app pictures through applinking
- What are the methods of adding elements to arrays in JS
- Implementation of six singleton modes
- Jinshanyun - 2023 Summer Internship
- Importerror: impossible d'importer le nom « graph» de « graphviz»
- 每月1号开始计算当月工作日
- TIPC Getting Started6
- Some suggestions for young people who are about to enter the workplace in the graduation season
- VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
- 通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
猜你喜欢
![[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]](/img/50/22f2fa8fd606572b13a18cc889ca2e.png)
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
![[applinking practical case] share in app pictures through applinking](/img/12/5616f1fa55387b81e25e55a98022b9.png)
[applinking practical case] share in app pictures through applinking

Redis超出最大内存错误OOM command not allowed when used memory &gt; 'maxmemory'

Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
![[in simple terms, play with FPGA learning 3 ----- basic grammar]](/img/f0/0204fa5197033877dc0758203253ae.png)
[in simple terms, play with FPGA learning 3 ----- basic grammar]

金山云——2023届暑期实习

Huawei game failed to initialize init with error code 907135000

PowerBI中导出数据方法汇总

QT learning diary 7 - qmainwindow

2022年4月17日五心红娘团队收获双份喜报
随机推荐
Multi line display and single line display of tqdm
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
The working day of the month is calculated from the 1st day of each month
III Chip startup and clock system
TIPC Cluster5
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
Compilation errors and printout garbled problems caused by Chinese content in vs2019 code
TIPC messaging3
Summary of data export methods in powerbi
制造业数字化转型和精益生产什么关系
SQLite modify column type
TIPC Getting Started6
liftOver进行基因组坐标转换
金山云——2023届暑期实习
Why does LabVIEW lose precision in floating point numbers
在连接mysql数据库的时候一直报错
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
C file and folder operation
PLC-Recorder快速监控多个PLC位的技巧
Openmldb meetup No.4 meeting minutes