tags:

views:

112

answers:

3

I want to write a 'Date' class that behaves like a Value Type. for example, Instead of writing a Clone method for setting properties safely, make the Date class to pass by value:

public Date Birthday
        {
            get { return this.birthday; }
            set 
            { 
               this.birthday = value.Clone(); 
            } //I want to write this.birthday = value; 
              //without changing external value when this.Birthday changes
        }

I know this is possible because System.String is a class and behaves like a value. for example:

String s1 = "Hello";
String s2 = "Hi";
s1 = s2;
s2="Hello";
Console.WriteLine(s1);  //Prints 'Hi'

First I thought writers of this class override '=' operator, but now I know that the '=' operator can not be overridden. so how they write String class?

Edit: I just want to make my Date class to pass it's instances by value, like as String.

+7  A: 

First, your string-based example does not illustrate your question.

The thing with DateTime and String is that they are immutable: once an instance is created, it cannot be changed in any way. For example, you cannot add 2 minutes to a DateTime instance by just saying date.Minutes += 2: you'll have to invoke date.AddMinutes(2), which will yield a totally new instance.

To make objects read-only, just follow the same pattern.

Anton Gogolev
define: immutable: `not subject or susceptible to change or variation in form or quality or nature; "the view of that time was that all species were immutable ...`
Filip Ekberg
I didn't mean System.DateTime class. I write a class named Date, and it's instances behave are passed by reference. i want to pass them by value without changing user-side code.
Sorush Rabiee
Use a struct instead of a class then
thecoop
@ thecoop : but the 'System.String' is not a struct.
Sorush Rabiee
Downvote. System.DateTime is a struct, and thus a value type. It is mutable.
tc.
@tc How exactly `DateTime` is mutable?
Anton Gogolev
@@@@ Who speaks about System.DateTime? I'm writing my own class and want it to pass by value.
Sorush Rabiee
@tc, DateTime is not mutable, and it is not related to it being a struct : both values and reference types can be mutable or immutable...
Thomas Levesque
+4  A: 

It's not the '=' operator, it's the fact that when you say

stringThing = "thing";

you're creating a new string, not changing the current string to something else.

Dave Arkell
+4  A: 

public class Date{ ...code...} would be a reference type...not what you want.

public struct Date { ...code...} would be a value type...probably what you want.

The string class is, as it is a class, a reference type...and is immutable..how being immutable effects the behavior of string objects can be confusing at the start.

Given string s1 = "Fish"; s1 is a reference that points to "Fish"...It is the "Fish" bit can never be changed....what s1 points to can be changed. If you then assign s1 = "Tuna"; "Fish" still exists but is no longer referenced and will be GC'd.

In your example after: s1=s2 s1,s2 now reference the same string "Hi"...there is only one "Hi".

I hope I have not gone way below your level.

Rusty
+1, probably the most understandable answer
Thomas Levesque
+1,Thanks. now I understand what is happening when saying s1=s2. but why other classes have not same behavior? for example if d1 and d2 were 'Date's then d1=d2 didn't do like s1=s2.?
Sorush Rabiee
@Sorush, other *classes* (reference types) **do** have the same behavior. But *structs* (value types) have value semantics, so if Date is a struct, d1 = d2 will copy the value of d2 into d1, so you will have 2 instances of Date.
Thomas Levesque
Sorush, this happens because string is a reference type where just pointers change. Whereas Date is a valuetype.
Justin
-snip- Exactly what Thomas said.
Rusty