C # odd object behavior

I noticed something in C # when working with custom objects, which seemed a little strange to me. I am sure that this is simply a lack of understanding on my part, so maybe someone can enlighten me.

If I create a custom object and then assign this object to a property of another object, and the second object changes the object assigned to it, these changes are reflected in the same class that performed the assignment, even if nothing is returned.

Do you want it in English? Here is an example:

class MyProgram
{
    static void Main()
    {
        var myList = new List<string>();
        myList.Add("I was added from MyProgram.Main().");
        var myObject = new SomeObject();
        myObject.MyList = myList;
        myObject.DoSomething();

        foreach (string s in myList)
            Console.WriteLine(s); // This displays both strings.
    }
}

public class SomeObject
{
    public List<string> MyList { get; set; }

    public void DoSomething()
    {
        this.MyList.Add("I was added from SomeObject.DoSomething()");
    }
}

In the above example, I would think that since SomeObject.DoSomething()void returns, this program will only display "I was added from MyProgram.Main().", However, it List<string>actually contains this line, and "I was added from SomeObject.DoSomething()".

. . ?

class MyProgram
{
    static void Main()
    {
        var myString = "I was set in MyProgram.Main()";
        var myObject = new SomeObject();
        myObject.MyString = myString;
        myObject.DoSomething();

        Console.WriteLine(myString); // Displays original string.
    }
}

public class SomeObject
{
    public string MyString { get; set; }

    public void DoSomething()
    {
        this.MyString = "I was set in SomeObject.DoSomething().";
    }
}

"I was set in MyProgram.Main()". , "I was set in SomeObject.DoSomething()." , , - .

+5
8

, . , . , , , , .

var myList = new List<string>();
myList.Add("I was added from MyProgram.Main().");
var myObject = new SomeObject();
myObject.MyList = myList;
myObject.DoSomething();

, List<string> myList. "I was added from MyProgram.Main()." , myList. myObject.MyList ( , myList myObject.MyList List<string>! myObject.DoSomething(), "I was added from SomeObject.DoSomething()" myObject.MyList myList myObject.MyList List<string>, .

. . . . , , : "I was added from MyProgram.Main()." , "I was added from SomeObject.DoSomething()". , , , , . ,

I was added from MyProgram.Main().

-

I was added from SomeObject.DoSomething()

?

, , .

var myString = "I was set in MyProgram.Main()";
var myObject = new SomeObject();
myObject.MyString = myString;
myObject.DoSomething();

string, "I was set in MyProgram.Main()", myString. myObject.MyString. , myString, myObject.MyString string, "I was set in MyProgram.Main()". myObject.DoSomething,

this.MyString = "I was set in SomeObject.DoSomething().";

, string, "I was set in SomeObject.DoSomething()." string myObject.MyString. , , myString. , myString myObject.MyString !

. - . . - . - . , !

, string. , , string.

+15

:

myObject.MyList = myList; 

myList myObject.
, GetHashCode() myList myObject.MyList.

, .

+2

, -, , . , , , .

.

var myList = new List<string>();

List<string> myList.
List<string>, , .

var theSameList = myList; 
var sameOldList = myList;
someObject.MyList = myList;

myList, theSameList, sameOldList someObject.MyList (, , SomeObject, ) .

:

var bob = new Person();
var guyIMetInTheBar = bob;
alice.Daddy = bob;
harry.Uncle = bob;
itDepartment.Head = bob;

Person .
, , Age .
.

, , .

,

,

- , . . , , , , .

, : , , .

, , , , .

, :

  • .NET, string .
  • (, List<T>, , ), , .

    var goodGuy = jack;
    alice.Lover = jack;
    alice.Lover = mike;
    

alice jack ? .
, myObject.MyString myString. ( ).

+1

. - string.., , :)

Add, .

, DoSomething(), , Main. , .

0

, . List < > . , .

.

0

. myList myObject.MyList List .

,

myObject.MyString = myString;

myString myObject.MyString(.. )

, ( , , , )

0

- -

, , SomeObject.DoSomething() void, " MyProgram.Main().". , " SomeObject.DoSomething()".

. VOID , . this.MyList.Add, DoSomething(). - myList MyList SomeObject.

0

... , . MyList .

-. (.. ) String, . , .

UPDATE

Jason is right , he has nothing to do with the immutability of String ... but .... I cannot help but think that the string is incredible. Not in this specific example, but if the code SomeObject.DoSomethingwere as follows: this.MyString += "I was updated in SomeObject.DoSomething().";you would need to explain that the new line is created by "concatenation" and the first line is not updated

0
source

All Articles