English 中文(简体)
最佳做法:新日期(时间)与日期
原标题:Best practice: new DateTime() vs DateTime.Parse()
  • 时间:2011-11-13 01:09:45
  •  标签:
  • c#
  • datetime

我要说的是,我正在制造一个名称和日期都相同的一类物体。 在确定日期时哪些是最佳做法?

var employees = new List<Employee>() 
{ 
    new Employee {Name = "Foo", HireDate = new DateTime(2000, 1, 15)}, 
    new Employee {Name = "Bar", HireDate = new DateTime(2001, 5, 25)}, 
}; 

var employees = new List<Employee>() 
{ 
    new Employee {Name = "Foo", HireDate = DateTime.Parse("2000, 1, 15")}, 
    new Employee {Name = "Bar", HireDate = DateTime.Parse("2001, 5, 25")}, 
}; 

I am assuming there really isn t a huge difference, but I am slightly new to C# so I am not sure which is prefered by the maj或ity of C# programmers and why (perf或mance, readability, etc.). Thanks in advance f或 any insight you can give!

最佳回答

参考<代码>新日期

为了支持<代码>new,它将允许你直接使用变数:

// clean because the variable is applied directly to the function
int year = 2000;
var date1 = new DateTime(year, 1, 15);
var date1 = new DateTime(year, 7, 3);
var date1 = new DateTime(year, 8, 19);

// kind of gross and prone to errors because it is appended to the string
int day = 23;
var date1 = DateTime.Parse("2001, 5, " + day.ToString());

在反对<代码>Parse的争论中,在操作时间之前,它没有发现错误:

var date1 = new DateTime(200fdsa, 1, 15); // Error on compile
var date2 = DateTime.Parse("2001fdsjf, 5, 25"); // Must run to find the error

<代码>Datetime.Parse将永远业绩较差,因为它需要操作能够发现错误的代码,并将体格转换成数字数值。 在您的方案实施期间,它这样做。

但是,如果你must接受扼杀性投入(例如,从文字档案中获取),你就应当使用为这项工作而建立的工具:Datetime。 Parse


如果你有兴趣简化你代码中的日期/时间不变的规格,则有Joon Skeet为此写的图书馆。

这是一种老旧的、未坚持的,但具体法典(关于惯用/最新和时间结构的延伸方法)可能确实需要大量的维护。

这些延期的源代码载于<代码>。 MiscUtilMiscUtilExtensions Relatd (inside the source zip)

请你以友善的方式写出你的日期,但还是向您提供具有类似性能的清洁代码,以<新日期(2012年、11月13日):

DateTime someDateAndTime = 19.June(1976) + 8.Hours();
问题回答

我认为这是最终主观的,但我不能想象在这种情况下使用<代码>Parse<>/code>的任何良好理由。 为什么要执行(效率不高)的教 par,而不是采用实际组成部分价值的简单构造者?

I d just use new DateTime() in the example you provided. You know exactly what the year/month/day components of the date are. Why create a string and parse it?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签