Monday, March 26, 2012

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 donw 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 youe 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.


  1. Nice post. I learn something new and challenging on websites I stumbleupon everyday.
    It's always helpful to read through content from other writers and use a little something from their web sites.

    Feel free to surf to my site: broker forex

  2. It's awesome in favor of me to have a web page, which is beneficial in favor of my experience. thanks admin

    Also visit my blog :: Treating gynecomastia - Will be the Torso Coach Method worthwhile?

  3. You ought to take part in a contest for one of the highest
    quality blogs on the web. I most certainly will recommend
    this web site!

    Check out my site - In the event you construct Toned Chest or even Substantial Pecs ?

  4. With havin so much content do you ever run
    into any issues of plagorism or copyright infringement?
    My blog has a lot of unique content I've either written myself or outsourced but it seems a lot of it is popping it up all over the internet without my permission. Do you know any methods to help stop content from being stolen? I'd
    truly appreciate it.

    Visit my webpage;

  5. I was recommended this web site by means of my cousin.
    I am now not sure whether or not this put up
    is written by way of him as nobody else understand
    such exact about my problem. You're incredible! Thanks!

    Here is my webpage ... Anadrol increases the danger regarding gynecomastiain males

  6. My spouse and I stumbled over here coming from a different page and thought I may as well check things out.
    I like what I see so now i'm following you. Look forward to looking at your web page repeatedly.

    Here is my web page: Will using tobacco pot result in " moobs " improvement?

  7. Does your site have a contact page? I'm having problems locating it but, I'd like to
    shoot you an e-mail. I've got some ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.

    Look into my webpage; Is actually fat loss important for managing gynecomastia?