为什么日期时间在.net中的结构?日期、结构、时间、net

由网友(£岁月★激情彡)分享简介:为什么的DateTime 的结构,而不是一个可继承类?Why is DateTime a structure instead of an inheritable class?(我希望能够重写ToString()方法,但我不能。)(I would like to be able to override the To...

为什么的DateTime 的结构,而不是一个可继承类?

Why is DateTime a structure instead of an inheritable class?

(我希望能够重写ToString()方法,但我不能。)

(I would like to be able to override the ToString() method but I can't.)

推荐答案

可能是因为它被看作是一个小的,简单的和不可改变的数据结构,很像一个整数或小数点。使使用日期时间非常有效的使之成为在这些条件下的结构体。如果它已经取得了一类,这种效率的好处会被丢失,因为这需要你创建一个新的DateTime每次内存分配。

Probably because it was seen as a small, simple and immutable data structure, much like an integer or decimal. Making it a struct under these conditions make using a DateTime very efficient. If it had been made a class, this efficiency benefit would have been lost because that would require memory allocations every time you create a new DateTime.

此外,有多少变种a的DateTime的形式你能想出? (忽略你心目中的allternate的ToString实现。)它不完全,邀请多态性的类型。

Besides, how many variant forms of a DateTime can you come up with? (Ignoring the allternate ToString implementations you have in mind.) It's not exactly a type that invites polymorphism.

请注意,对于使用不同格式的策略为您创建DateTime对象,因为我想你想,你在用不同的方式最好先看看比只使用的ToString格式化。如果你看一下 ICustomFormatter MSDN中的界面,你会看到你如何插入的String.Format管道覆盖,而无需子集现有类型的格式。

Note that for using a different formatting strategy for your DateTimes, as I think you want, you'd better look at a different way to format than just using ToString. If you look at the ICustomFormatter interface in MSDN, you'll see how you can plug into the String.Format pipeline to override formatting without needing to subset an existing type.

阅读全文

相关推荐

最新文章