473,408 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Why is System.DateTime a structure?

I am curious why did Microsoft write System.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.


Nov 22 '05 #1
4 1777
Sorry it should be:
one of the differences between structure and class is we can NOT set the
structure to Nothing.

"Pram" <pr**@nospam.com> wrote in message
news:uw**************@TK2MSFTNGP09.phx.gbl...
I am curious why did Microsoft write System.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.

Nov 22 '05 #2
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 .NET Design
Guidelines for Class Library Developers, in that it:

- Acts like a primitive type
- It has an instance size of under 16 bytes
- It is immutable
- It has value semantics.

Which is 4 for 4!

http://msdn.microsoft.com/library/de...guidelines.asp

The one item that is not listed, but is implied by the above list is as a
Structure it is significantly easier to work with as opposed to being a
class!

Hope this helps
Jay

"Pram" <pr**@nospam.com> wrote in message
news:uw**************@TK2MSFTNGP09.phx.gbl...
I am curious why did Microsoft write System.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.

Nov 22 '05 #3
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. For a good discussion of this, check out the following MSDN page:

ms-help://MS.MSDNQTR.2003FEB.1033/vbls7/html/vblrfVBSpec6_1.htm

or if you do not have MSDN, you can check it out on the web at:
http://msdn.microsoft.com/library/de...us/vbls7/html/
vblrfvbspec6_1.asp

Another good (and more thorough) explanation:

http://www.yoda.arachsys.com/csharp/memory.html

Let me know if you still have any questions about this.

Jeff.

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 22 '05 #4
Jeff,
I do understand the benefits of structure over class.

The reason I asked that question because I have an XML string that has an
empty date element.
Then when I do "XML Serialization into an object", I get an exception
because dateTime structure
does not allow Null or Nothing. I have not found a nice way or trick to
solve this problem yet.

Thanks Jeff.

"Jeff Callahan" <Je*****@online.microsoft.com> wrote in message
news:tj**************@cpmsftngxa06.phx.gbl...
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. For a good discussion of this, check out the following MSDN page:

ms-help://MS.MSDNQTR.2003FEB.1033/vbls7/html/vblrfVBSpec6_1.htm

or if you do not have MSDN, you can check it out on the web at:
http://msdn.microsoft.com/library/de...us/vbls7/html/ vblrfvbspec6_1.asp

Another good (and more thorough) explanation:

http://www.yoda.arachsys.com/csharp/memory.html

Let me know if you still have any questions about this.

Jeff.

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pram | last post by:
I am curious why did Microsoft write System.DateTime as a structure instead of a class? Maybe for backward compatibilty with any existing old code? FYI, one of the differences between...
2
by: Robert | last post by:
I have no problem storing dates + times in a System.DateTime object. In addition, it's easy to output a Time as a string from an existing Date/Time. But I'm having trouble storing a time only. ...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
5
by: A.M | last post by:
Hi, I know we can assigne year, month and day to a DateTime value by the constructor like Dim dt As DateTime = New DateTime(2003,8,1) How can i assign value by year, month and day? I am...
2
by: Fredrik Melin | last post by:
Is there any way to extend or write a own "System.Decimal"? In the doc its declared as <Serializable> Public Structure Decimal Implements IFormattable, IComparable, IConvertibleBut how do you...
13
by: Alan M Dunsmuir | last post by:
I need to specify a new data type, almost entirely a 'clone' of the existing DateTime type, with the following specific difference, and all the consequent differences in properties and methods...
11
by: frizzle | last post by:
Hi groupies I'm building a news site, to wich a user can add new items into a mySQL db. It's still in testfase, but it's so extremely slow, i want to figure out what i'm doing wrong, or what to...
7
by: =?Utf-8?B?Q29ubnVsbA==?= | last post by:
Can anybody assist me on the code to set the System's ShortDate format to yyyy/MM/dd at runtime?
5
by: indika | last post by:
Hi, I'm a newbie to python but have some experience in programming. I came across this requirement of using datetime.date objects associated with some another object. eg. a dictionary containing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.