Click here to Skip to main content
13,446,762 members (42,906 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 24 Oct 2012

DateTime is immutable

, 24 Oct 2012
Rate this:
Please Sign up or sign in to vote.
DateTime is immutable

In this post I am going to discuss about the DateTime object and how its immutable. But before I start its better to clear "What is immutable object in OOP" and "What is mutable object in OOP".

Immutable object

Object values cannot be modified once get created.

Mutable object

Object values can be modified or changed after get created.

So above definition clears the difference between mutable and immutable object in OOP.

Now coming on DateTime object following example shows how its immutable object.

//Create DateTime object 
DateTime dt = DateTime.Now;
//Now make change in object 
//Write down dt 

When you run the above line and print value of the datetime variable on console. It write down current date rather than adding 3 day in it.

For example : -

if the current date is 26/3/2012 output is 26/3/2012 not 29/3/2012.

This example clear out that DateTime is immutable type. what ever you changes done by you its not change the value of datetime. If you want to make chnage in the value of DateTime variable value you need to write down

//Create DateTime object 
DateTime dt = DateTime.Now;
//Now make change in object 
dt = dt.AddDays(3);//change in above code
//Write down dt 

As you se above I did change in code by assigin value changed value to dt again so its now going to preserve the value what we change. Now if you write down value of dt on console its write donw "29/3/2012" if the current date is "26/3/2012".

I hope that I clear out the point DateTime is immutable because there are not of begineer level devloper thinks that when the do change in DateTime object it going to reflect on the sate of object.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

QuestionHow is this then immutable? Pin
Kevin Bewley29-Oct-12 0:51
memberKevin Bewley29-Oct-12 0:51 
AnswerRe: How is this then immutable? Pin
VMAtm29-Oct-12 22:28
memberVMAtm29-Oct-12 22:28 
AnswerRe: How is this then immutable? Pin
Abdul Azeez TK24-Dec-15 18:04
memberAbdul Azeez TK24-Dec-15 18:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180318.2 | Last Updated 24 Oct 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid