0

I have code that looks like this;

public ObservableCollection<ParamViewModel> ListOfPhrases{
   get => _listOfPhrases;
   set => SetProperty(ref _listOfPhrases, value);
}

public class ParamViewModel : ObservableObject
{

    bool _selected;
    int _id;
    string _name;
    string _state;

    public bool Selected { get => _selected; set => SetProperty(ref _selected, value); }
    public int Id { get => _id; set => SetProperty(ref _id, value); }
    public string Name { get => _name; set => SetProperty(ref _name, value); }
    public string State { get => _state; set => SetProperty(ref _state, value); }
}

private void OnPhraseTapped(string btnText)
{
   ListOfPhrases.ForEach(x => x.Selected = false);
   var first = ListOfPhrases.FirstOrDefault(x => x.Name == btnText);
   if (first != null) first.Selected = true;

So when the btnText is ABC it populates first with an element of the ListOfPhrases list. Then the value of first is set to true.

My question is. Why does changing the Selected value of first change an entry in the ListOfPhrases?

If I create this code:

var a = "ABC";
var b = a;
b = "x"

then I believe changing b doesn't change the value of a

  • Please provide the details of the types involved in the `List` or whatever `ListOfPhrases` is. – Wyck Dec 28 '20 at 17:36
  • 1
    As Wyck alludes to, `Phrase` is probably a reference type. How much have you learned about references in c#? – gunr2171 Dec 28 '20 at 17:38
  • `ListOfPhrases` almost certainly contains reference type objects. Essentially all variables that "point" to the instance will be pointing to the same thing. Strings are immutable however. – Charleh Dec 28 '20 at 17:40
  • So you mean that `first` is still part of the list and if I get a reference to it with the = then I can change that and the value in the list will change also? – Janice_Feb_1998 Dec 28 '20 at 17:41
  • 1
    There is a difference between _redefining_ a variable (`=`) and _modifying a member_ (`.Selected = `). – gunr2171 Dec 28 '20 at 17:42
  • If you have `List` and `Phrase` is a `class`. Then each entry in the list is a reference to a specific instance of a `Phrase` object. – Wyck Dec 28 '20 at 17:48
  • Does this answer your question? [In C# if an object in a list is added to another list, does changing the object in the second list change the same object in the first list?](https://stackoverflow.com/questions/28783768/in-c-sharp-if-an-object-in-a-list-is-added-to-another-list-does-changing-the-ob) – Progman Dec 28 '20 at 20:41

1 Answers1

0

References.

Long Story Short; Objects are assigned by REFERENCE. While value types are self contained in their own memory space.

Example:

// assume the constructor argument assigns to the public property Value
var listOfObjects = new List<SomeObject>() {
  new SomeObject(1),
  new SomeObject(2)
}

var a = listOfObjects[0];
var a.Value = 3;

// will print out 3 instead of 1
Console.WriteLine(listOfObjects[0].Value);

You'll want to get some reading on this. This is a very important concept to understand in Object Oriented Programming and C#.

To understand this I would highly recommend reading this:

https://jonskeet.uk/csharp/references.html

Paul Carlton
  • 2,785
  • 2
  • 24
  • 42