-2

I have a class defined as follows:

class foo
    {
        private string _bar;
        public string Bar
        {
            get { return _bar; }
            set { _bar = value; }
        }
    }

Look into the following operations:

  foo myFoo = new foo();//Creating an object of foo
  myFoo.Bar = "sample";//assign value for bar
  foo currentFoo = myFoo;//Assign the object to another object
  currentFoo.Bar = "newValue";//Change the value of newly created object.

In this scenario, after the final step value of bar in both the object will became newValue where as the value of object(.net) type is not reflecting like this:

  object obj = "currentValue";//assign some value 
 object newObj = obj;//assign object to another object
 newObj = "Changed Value";//change the value of new object.

Why it is not getting updated the value of old object?what will be the reason? Is it possible to create class's objects Like this?

Updates:

I have checked the following scenario too:

foo myFoo = new foo();
 myFoo.Bar = "sample";
 object currentFoo = myFoo;
 ((foo)currentFoo).Bar = "newValue";
 //Here also both the objects get updated.
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88

3 Answers3

0

C# uses references to pretty much everything expect the base types.

In this case you have a class that gets instantiated as an object and then reference it later on. When you use '=' in objects it will always create a reference.

In the second case your object is a 'string' so since it's a base type no reference gets created but instead another object with that value is created~

EDIT: Make 'immutable' class. Not really but just a way.

class foo
    {
        private string _bar;
        public string Bar
        {
            get { return _bar; }
            set { _bar = value; }
        }

        public foo Clone()//Cloning is creating a new reference type with same values as old object
        {
           foo newc = new foo();
           newc.Bar = this.Bar;
           return newc;
        }
    }

And then just do this

foo obj1 = new foo();
foo.Bar = "hi";
foo obj2 = foo.Clone();
obj2.Bar = "bye";
Diogo
  • 176
  • 3
0

string is reference type, which overrides operation of assignment and other, to work with like with value types, such as byte, short, int, etc.

Your defined class is actually derived from object and .NET works with object like with reference type, not value type.

Except some basic types, in .NET all objects are passing by reference.

Mikhail Tulubaev
  • 4,141
  • 19
  • 31
0

Classes are Reference types, while structures are value types

meaning that classes pass a pointer to the memory location they are stored in while structure pass a new value

string is a peculiar cross over type but can generally be considered as a value type

using your examples this is what's happening

foo myFoo = new foo();//Creating a new foo object at memory location #1 and point variable myFoo at #1
myFoo.Bar = "sample";//set the bar section of #1 to Sample
foo currentFoo = myFoo;//point variable current foo at #1
currentFoo.Bar = "newValue";//set the bar section of #1 to Sample

now in your second this is whats happening

object obj = "currentValue";//Create a value at #1 with the value "currentValue" and point obj at #1
object newObj = obj;//Point newObj at #1
newObj = "Changed Value";//create value at #2 as "Changed Value" and point newObj at #2
MikeT
  • 5,398
  • 3
  • 27
  • 43