问题描述:

I have no practical need for this, but I was wondering:

is there anyway in C# to know, for any two arbitrary objects, which one was created first?

网友答案:

No. Unless the object has a read-only property that stored the date and time that the object was created, there's no way to know.

网友答案:

Only if, inside the constructor of the object, you store the current date/time (note that DateTime.Now almost certainly has insufficient precision to be useful) and provide some means of exposing that information publicly.

网友答案:

Short of storing it - you can also log the timestamp somewhere.
(Being that the most obvious purpose for this could be some form of debugging that makes sense)

Still it won't work for arbitrary objects, unless you have full control.

Most solutions derive from practical needs, this comes up rarely - if you need anything like that you can make a custom factory for 3rd party objects, or your own implementation for ones you own.

相关阅读:
Top