当前位置:网站首页>C # method of obtaining a unique identification number (ID) based on the current time
C # method of obtaining a unique identification number (ID) based on the current time
2022-07-02 11:36:00 【chengjl8】
In software development , We often have to distinguish between similar objects , For example, multiple variables generated in batch , Multiple annotations, etc . stay PLC-Recorder In the process of development ,ID The generation method of No. has also been optimized many times , Finally stabilized , Can be based on the current time , Generate non-repeating ID Number ( It is effective when the sudden demand is not very large ), Share with you , Let friends also take fewer detours .
1、 Based on the current time
Because the software may restart , therefore , Will the history of ID Write it all down , It will be a great burden . therefore , Based on the current time ID Generate , Can avoid with history ID The conflict of .
Int64 ID = DateTime.Now.Ticks;
2、 Find out ID Repetition of No
Only use the above statement to generate ID Number is not enough , because , Batch generation , They are the same , therefore , Need to double check .
Will be effective ID Deposit in dictionary , You can check the duplicate quickly . The program generates itself ID after , Will be immediately stored in the dictionary . If it is generated by other programs , Pay attention to also store in the dictionary .
Of course , It may be easier to use a hip-hop watch , You can improve , I generate ID Your actions are not frequent , therefore , It doesn't matter .
3、 Handling when duplicate is found
If you find a repetition , Will ID++, Until you find something that doesn't repeat . Then the new ID Deposit in dictionary , And feedback out .
/// <summary>
/// Use dictionary
/// </summary>
/// <returns> new ID</returns>
public static Int64 IDGetNew()
{
// confirm ID No repetition
Int64 ID = DateTime.Now.Ticks;
while (LibID.ContainsKey(ID))
{
ID++;// Get a new 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 year 6 month 30 Japan
边栏推荐
- RPA advanced (II) uipath application practice
- 从ros1到ros2配置的一些东西
- III Chip startup and clock system
- What is the relationship between digital transformation of manufacturing industry and lean production
- ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
- Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
- ASTParser 解析含有emum 枚举方法的类文件的踩坑记
- Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
- Solve the problem of data blank in the quick sliding page of the uniapp list
- TIPC introduction 1
猜你喜欢

Webauthn - official development document

ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)

PLC-Recorder快速监控多个PLC位的技巧

On April 17, 2022, the five heart matchmaker team received double good news

Basic usage of MySQL in centos8

Win11 arm system configuration Net core environment variable

Amazon cloud technology community builder application window opens
![[cloud native] 2.5 kubernetes core practice (Part 2)](/img/87/826894d758392a0c7a60dd5fa09eef.png)
[cloud native] 2.5 kubernetes core practice (Part 2)

Thanos Receiver

Wechat applet uses Baidu API to achieve plant recognition
随机推荐
ctf 记录
基于Hardhat和Openzeppelin开发可升级合约(一)
MySql存储过程游标遍历结果集
TIPC addressing 2
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
ren域名有价值吗?值不值得投资?ren域名的应用范围有哪些?
Jenkins installation
Some suggestions for young people who are about to enter the workplace in the graduation season
ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)
flutter 问题总结
Resources读取2d纹理 转换为png格式
Is the stock account given by qiniu business school safe? Can I open an account?
C# 文件与文件夹操作
tqdm的多行显示与单行显示
ros缺少xacro的包
Is it safe to open a stock account through the QR code of the securities manager? Or is it safe to open an account in a securities company?
Compilation errors and printout garbled problems caused by Chinese content in vs2019 code
Tick Data and Resampling
Solve the problem of data blank in the quick sliding page of the uniapp list
On April 17, 2022, the five heart matchmaker team received double good news