当前位置:网站首页>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
边栏推荐
- Xiao Sha's pain (double pointer
- A white hole formed by antineutrons produced by particle accelerators
- sql left join 主表限制条件写在on后面和写在where后面的区别
- 基于Hardhat编写合约测试用例
- JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
- What are the methods of adding elements to arrays in JS
- 通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
- VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
- Thanos Receiver
- Redis超出最大内存错误OOM command not allowed when used memory &gt; 'maxmemory'
猜你喜欢

RPA进阶(二)Uipath应用实践

金山云——2023届暑期实习

Solve the problem of data blank in the quick sliding page of the uniapp list

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

PYQT5+openCV项目实战:微循环仪图片、视频记录和人工对比软件(附源码)

Never forget, there will be echoes | hanging mirror sincerely invites you to participate in the opensca user award research

What is the relationship between digital transformation of manufacturing industry and lean production
![[cloud native] 2.5 kubernetes core practice (Part 2)](/img/87/826894d758392a0c7a60dd5fa09eef.png)
[cloud native] 2.5 kubernetes core practice (Part 2)

Verilog and VHDL signed and unsigned number correlation operations

TIPC addressing 2
随机推荐
Regular and common formulas
What are the methods of adding elements to arrays in JS
Basic usage of MySQL in centos8
A white hole formed by antineutrons produced by particle accelerators
MySQL linked list data storage query sorting problem
js中给数组添加元素的方法有哪些
Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'
Liftover for genome coordinate conversion
Multi line display and single line display of tqdm
Jinshanyun - 2023 Summer Internship
Thanos Receiver
Is the Ren domain name valuable? Is it worth investing? What is the application scope of Ren domain name?
启牛商学院给的股票账户安全吗?能开户吗?
Skills of PLC recorder in quickly monitoring multiple PLC bits
对毕业季即将踏入职场的年轻人的一点建议
高德根据轨迹画线
Tdsql | difficult employment? Tencent cloud database micro authentication to help you
II Stm32f407 chip GPIO programming, register operation, library function operation and bit segment operation
Principe du contrat évolutif - delegatecall
SSRF