Why is datetime a struct




















Does it represent some sort of quantity, or value, rather than a "thing" in the sense that a Customer is a "thing" that deserves an identity whereas an integer is a value. Here's a big hint: could you give it a unique key? Would that make sense? Customers have customer numbers; sales orders have sales order numbers. These are all classes, pure and simple. The concept makes no sense. As I said, I have a value type that contains a reference member: it's called Measure, and it's a Decimal coupled with a unit of measure, which is a class reference type in my system.

Measure, however, is clearly a value: I do mathematical operations with it. I want it to be copied when it's assigned. I want it to be immutable: I certainly don't want to have a reference to a Measure and go modifying it. On the other hand, I can imagine a class such as "annual sales figures" that contains nothing more than values: a customer number, a start date, and buckets for monetary values.

No reference types. However, it is a "thing," insofar as it is conceivable and probable that it has UID the customer number plus the start date. As well, you may want to modify the sales figures for a customer for a time period. Definitely reference semantics stuff.

Definitely a class. So, no: whether something contains only value types is not a good way of deciding whether it needs to be a struct. Jeffrey, I too disagree with this rule! When do you need to use a Struct? I follow the. Wow, great feedback. After this discussion and reading the content in the links provided, I must take back my previous post. Yes, value semantics must weigh out in the decision process.

Personally, I have never had the need for a struct. I do mostly business applications where my object have an identify. I'd like to ask a question about the 2nd MS guideline: "have an instance size under 16 bytes". Is that really a hard and fast rule? The DateTime struct, if you look at all the "long" members, its instance size would be more than 16 bytes.

No, I think that the "less than 16 bytes" rule is more a guideline. It simply asks you to keep in mind that value types are copied whenever they are assigned, and if you create a large value type then all of that copying will have a performance penalty.

However, it really depends upon how often you use the type in question, and what else your application is doing. If your app spends all of its time in the CPU, and the type in question is heavily used, then big value types could be a problem. If you're writing an application that reads from a database and so you spend most of your user's "real time" waiting for the database to answer you , and the value type is not often used, then it probably doesn't matter how big it is. Thanks, Bruce. This thread has turned out to be a great discussion.

Which members are those? I think there's really just one. Cor Ligthert. Bruce, In my opinion the best message in this thread. Compliments :- Cor. Jeffrey, As Bruce states the "instance size under 16 bytes" is a guide line all four are guidelines. The size of a DateTime is a single Long. Don't confuse the size of various properties with the underlying storage. The only real value stored is DateTime. Ticks, the other properties are derived from DateTime.

NET Framework 2. The value of the ticks field can be obtained with the Ticks property. Jon, You are right. It only has one instance field, a long. I didn't look closely enough.

All the others are static or constants. I did find System. CounterSample that has 7 long members 8 bytes per field. Am I right about that? So this may be one time where the 16 bytes is exceeded, but I can see the reason to keep the instance size as small as possible. I think so. I think the main point is to favour reference types in general.

This discussion thread is closed Start new discussion. Similar topics Python. Can't subclass datetime. NET Framework. Can i inherit from TabControl and use the designer? How to determine if a string can convert to datetime.

Can we inherit a class and it's aspx ahead. Code reuse, code behind, and can't inherit since already inheritsPage. Visual Basic. Can't inherit from System. Why can't a datetime be converted to a double? What is JavaScript? Web Applications. Microsoft Excel. How to implement control array with vba.

Data Scientist vs Software Engineer. The research path of clustering. C Visual Studio visualForm. PNG printer. Payroll Program. Storing session info in Local Storage? Follow us! Get the Latest Bytes Updates. By using this site, you agree to our Privacy Policy and Terms of Use. Why can't I inherit from DateTime? DateTime has a small size. So it is not efficient to allocate memory at heap for this.

It had to be convertible to the corresponding COM structure. You don't need to overide the ToString method just check the patterns here you won't really need others : more detailed post here. Hannoun Yassir Hannoun Yassir Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Quality code is the easiest to delete. Upcoming Events. Featured on Meta. Now live: A fully responsive profile. Candidate changes in Moderator Election — review your ballot. Linked Related Hot Network Questions. Question feed. Returns a new DateTime that adds the specified number of years to the value of this instance.

Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

Compares the value of this instance to a specified object that contains a specified DateTime value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value. Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

Returns a value indicating whether two DateTime instances have the same date and time value. Deserializes a bit binary value and recreates an original serialized DateTime object. Converts the value of this instance to all the string representations supported by the standard date and time format specifiers. Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier.

Converts the value of this instance to all the string representations supported by the specified standard date and time format specifier and culture-specific formatting information. Converts the value of this instance to all the string representations supported by the standard date and time format specifiers and the specified culture-specific formatting information.

Returns the TypeCode for value type DateTime. Indicates whether this instance of DateTime is within the daylight saving time range for the current time zone. Converts a memory span that contains string representation of a date and time to its DateTime equivalent by using culture-specific format information and a formatting style. Converts the string representation of a date and time to its DateTime equivalent by using the conventions of the current thread culture.

Converts the string representation of a date and time to its DateTime equivalent by using culture-specific format information. Converts the string representation of a date and time to its DateTime equivalent by using culture-specific format information and a formatting style. Converts the specified span representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly or an exception is thrown.

Converts the specified span representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style.

The format of the string representation must match at least one of the specified formats exactly or an exception is thrown. Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.

The format of the string representation must match the specified format exactly. Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style.

Returns a new TimeSpan that subtracts the specified date and time from the value of this instance. Returns a new DateTime that subtracts the specified duration from the value of this instance. Serializes the current DateTime object to a bit binary value that subsequently can be used to recreate the DateTime object.

Converts the value of the current DateTime object to a Windows file time. Converts the value of the current DateTime object to local time. Converts the value of the current DateTime object to its equivalent long date string representation.

Converts the value of the current DateTime object to its equivalent long time string representation. Converts the value of the current DateTime object to its equivalent short date string representation. Converts the value of the current DateTime object to its equivalent short time string representation. Converts the value of the current DateTime object to its equivalent string representation using the formatting conventions of the current culture.

Converts the value of the current DateTime object to its equivalent string representation using the specified culture-specific format information. Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture.

Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information. Converts the specified char span of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded. Converts the span representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.

Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded. Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.

The method returns a value that indicates whether the conversion succeeded. The format of the string representation must match at least one of the specified formats exactly.

Determines whether two specified instances of DateTime are equal. Determines whether one specified DateTime is later than another specified DateTime. Determines whether one specified DateTime represents a date and time that is the same as or later than another specified DateTime. Determines whether two specified instances of DateTime are not equal.

Determines whether one specified DateTime is earlier than another specified DateTime. Determines whether one specified DateTime represents a date and time that is the same as or earlier than another specified DateTime. Subtracts a specified date and time from another specified date and time and returns a time interval. Subtracts a specified time interval from a specified date and time and returns a new date and time.

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

Returns the TypeCode for this instance. This conversion is not supported. Attempting to use this method throws an InvalidCastException. Returns the current DateTime object. Converts the current DateTime object to an object of a specified type.

Populates a SerializationInfo object with the data needed to serialize the current DateTime object. All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Represents an instant in time, typically expressed as a date and time of day. Important Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. Note Some C examples in this article run in the Try.

Note If you are working with a ticks value that you want to convert to some other time interval, such as minutes or seconds, you should use the TimeSpan. Note An alternative to the DateTime structure for working with date and time values in particular time zones is the DateTimeOffset structure. Note As an alternative to performing date and time arithmetic on DateTime values to measure elapsed time, you can use the Stopwatch class. Is this page helpful?

Yes No. Any additional feedback? Skip Submit. DateTime Int32, Int32, Int DateTime Int Gets the milliseconds component of the date represented by this instance. Gets the number of ticks that represent the date and time of this instance. Add TimeSpan. AddDays Double. AddHours Double. AddMilliseconds Double. AddMinutes Double. AddMonths Int AddSeconds Double. AddTicks Int AddYears Int Compare DateTime, DateTime.

CompareTo DateTime. CompareTo Object. DaysInMonth Int32, Int Equals DateTime. DateTime as a structure instead of a class? Maybe for backward compatibilty with any existing old code? FYI, one of the differences between structure and class is we can set the structure to Nothing. Follow Post Reply. Sorry it should be: one of the differences between structure and class is we can NOT set the structure to Nothing. Jay B. Harlow [MVP - Outlook].

Pram, I believe DateTime is a structure, as it meets all the criteria to use when deciding to make a type a Structure as set forth by the. Which is 4 for 4! Jeff Callahan. Hi Pram, The key difference between structs, which are value types, and Classes, which are reference types, is how they are allocated in memory. A struct is allocated directly on the stack and a Class is allocated on the run-time heap.

This distinction allows them to be used in different ways and perhaps most importantly it allows the runtime to treat them in different ways. Jeff, I do understand the benefits of structure over class.



0コメント

  • 1000 / 1000