0

i make a class (Person) and i create a object from it, then add it to a generic list and initialise as a null but this not affect on main object!! i want it to affect it, tnks:)

Person i = new Person();
            i.Name = "asal";
            i.Age = 25;

            List<Person> lst = new List<Person>();
            lst.Add(i);

            lst[0] = null;

            Console.WriteLine($"Age: {i.Age}, Name: {i.Name}");

            Console.ReadKey();

please see picture

بعد از اجرا مقدادیر را نشان میدهد ولی میخواهم ارور بدهد چون میخوام مقدارش نال شده باشد

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 5
    Fundamental concept to understand before going on: [What exactly is a reference in C#](https://stackoverflow.com/questions/40686776/what-exactly-is-a-reference-in-c-sharp#:~:text=References%20are%20things%20that%20act%20as%20references%20are,may%20be%20passed%20around%20like%20any%20other%20value.) – Steve Oct 11 '20 at 13:38
  • You might want to read about reference types. That is the keyword you are looking for(6 seconds slower than Steve) – ilkerkaran Oct 11 '20 at 13:38
  • OP: Be aware that WinForms is a _graphical_ user interface framework built into .NET. Please do not add unrelated tags to your questions. – ProgrammingLlama Oct 11 '20 at 13:48

3 Answers3

2

You need to first understand heap memory and referencing. In your code both i and lst[0] points to the same object in the memory.

i --> { Name : "Afsal", Age: 25} <-- lst[0]

by doing lst[0]=null; you just removed the reference not the object itself. At the same time i will still point to the person intact

i --> { Name : "Afsal", Age: 25}

Read more

Beingnin
  • 2,288
  • 1
  • 21
  • 37
0

Maybe this will help you understand what's happening:

lst.Add(i) adds a reference to the person stored in i to the list. lst[0] = null means that null should now be the first element in the list.

The person stored itself is not changed directly, it is simply no longer part of the list. If you want to change the value of the object stored inside the list, you need to access its properties like: lst[0].Name = null;.

You can think of the variable i as a reference to a person. If you set i=null, the object stored inside of i is not directly changed. i is now only referencing to null. The same is true for elements inside the list.

p-vogt
  • 46
  • 3
  • What is the solution (other than i = null) – tester testy Oct 11 '20 at 15:35
  • The way you want to do it is simply not possible as far as I know. What you could do is build a wrapper class where the property is of type Person and set this to null or use pointers, which I would not recommend you to do. – p-vogt Oct 11 '20 at 15:51
0

Imagine taking a dog for a walk

Person i = new Person();

i is the handle of the dog lead. It is in your hand. The other end of the lead is attached to the dog (the new Person is the dog). The handle in your hand is a reference to the dog

Now imagine a friend comes with you on this walk. They also have a lead.

lst.Add(i);

Now your friend lst has attached their lead to the dog. One dog, two leads, two people. Both leads are connected directly to the dog

lst[0] = null;

Now your friend has disconnected their lead from the dog. Your lead is still connected to the dog. Their disconnection has no impact on your lead's connection to the dog

lst[0] = i;

Their lead is reconnected directly to the dog. This is another important point. This line of code does not connect their lead to the handle of your lead. There is no "chaining leads together so that the dog is held on one longer lead". The connections are always direct to the dog, which means that disconnections don't affect other connections

If you want the dog to run off and get picked up by the dog warden (garbage collector) you'll have to disconnect both leads

lst[0] = null;
i = null;

Bye bye, Fido!

Caius Jard
  • 72,509
  • 5
  • 49
  • 80