当前位置:网站首页>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
边栏推荐
猜你喜欢

TIPC Service and Topology Tracking4

ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘

Eight sorting summaries

Skills of PLC recorder in quickly monitoring multiple PLC bits

ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘

Basic usage of MySQL in centos8

【IDEA】使用插件一键逆向生成代码

Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic

Verilog 和VHDL有符号数和无符号数相关运算

MTK full dump grab
随机推荐
spritejs
III Chip startup and clock system
deepTools对ChIP-seq数据可视化
Resources reads 2D texture and converts it to PNG format
金山云——2023届暑期实习
程序员成长第六篇:如何选择公司?
揭露数据不一致的利器 —— 实时核对系统
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)
基于Hardhat和Openzeppelin开发可升级合约(二)
可升级合约的原理-DelegateCall
Pit of the start attribute of enumrate
STM32单片机编程学习
CentOS8之mysql基本用法
基于 Openzeppelin 的可升级合约解决方案的注意事项
PLC-Recorder快速监控多个PLC位的技巧
Complement (Mathematical Simulation
接口调试工具概论
What is the relationship between digital transformation of manufacturing industry and lean production
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘