C#创建如何创建具有日期格式的唯一字符串 [英] C# Create how do I create a unique string with date format
本文介绍了C#创建如何创建具有日期格式的唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么myDate在循环中保持值?
即使下降到微秒,字符串在每次运行时应该有不同的值。
我无法想象处理器很快。我甚至尝试使用Thread.Sleep强制它在循环中等待。我对C#相当新,也许我错过了一些明显的东西。
Why does myDate hold the value in the loop?
Even going down to microsecond the string should have a different value on each run.
I can't imagine the processor is that fast. I even attempted putting Thread.Sleep to force it to wait in the loop. I am fairly new to C# maybe I am missing something obvious.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string myDate;
for (int i = 0; i <= 100000; i++)
{
myDate = String.Empty;
myDate = DateTime.Now.ToString("hh:mm:ss:fff:ms");
Console.WriteLine(myDate);
}
Console.ReadLine();
}
}
}
推荐答案
您的计算机处理器非常快。使用日期时间字段几乎不可能完成唯一标识符。
您应该使用全局唯一标识符来获得所需的结果。
推荐阅读:
http://en.wikipedia.org/wiki/Globally_unique_identifier < br $>
http://msdn.microsoft.com/en-us/library/system.guid.aspx
JAFC
Your computer processor is really fast. It is almost impossible to accomplish a unique identifier using a datetime field.
You should use a globally unique identifier to obtain the desired result.
Recommend reading:
http://en.wikipedia.org/wiki/Globally_unique_identifier
http://msdn.microsoft.com/en-us/library/system.guid.aspx
JAFC
你的电脑真的那么快,这就是为什么你并不总是有不同的价值。您可以使用System.Diagnostics
命名空间中的秒表
类来查找运行循环所需的时间:
Your computer is really that fast, and that's why you don't always have a different value. You can use theStopwatch
class in theSystem.Diagnostics
namespace to find out how long it takes to run your loop:
string myDate;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i <= 100000; i++)
{
myDate = String.Empty;
myDate = DateTime.Now.ToString("hh:mm:ss:fff:ms");
Console.WriteLine(myDate);
}
sw.Stop();
Console.WriteLine(sw.Elapsed.ToString());
Console.ReadLine();
我得到这个结果:
And I get this result:
00:00:05.1339143
因此,运行循环(在我的计算机上)只需要五秒钟。所以,如果你真的总想得到不同的结果,那么你应该使用 Thread.Sleep
。
添加Thread.Sleep(10)允许足够的时间让毫秒改变。它应该足够独特,我需要的东西。我并不担心这个循环的总时间。它不被用于任何关键的东西。而不是平面文件上的唯一键。
我认为我的问题是我之前在代码的错误部分有睡眠。
Adding Thread.Sleep(10) allows enough time for the milliseconds to change. it should be unique enough for what I need. I am not worried about the overall time of this loop. It isn't being used for anything critical. Rather than a unique key on a flat file.
I think my issue was I had the Sleep in the wrong part of the code before.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string myDate;
for (int i = 0; i <= 100; i++)
{
myDate = String.Empty;
myDate = DateTime.Now.ToString("hh:mm:ss:fff");
Console.WriteLine(myDate);
Thread.Sleep(10);
}
Console.ReadLine();
}
}
}
这篇关于C#创建如何创建具有日期格式的唯一字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文