Why reference to object is not initializing in List()

9 hours ago 1
ARTICLE AD BOX

I am trying to understand why field (MyCl1 cl1;  - is reference type,  which is not yet initialized) of class Form1, is not getting initialized.

I placed that (cl1) reference in list (List<MyCl1> l_my) and after that I tried to initialize that field via Method (init_cl1()). So finally I got initialization of field (cl1) – this is OK, but as soon as I put that field in List, so I also expected that item in List via reference (cl1) to be initialized also. But it is not, it still remained null.

Could anyone please explain how it is possible, because I expected that reference placed in List to be linked to that new object ( cl1 = new MyCl1(); ).

public partial class Form1 : Form { public Form1() { InitializeComponent(); List<MyCl1> l_my = new List<MyCl1>(); l_my.Add(cl1); init_cl1(); MyCl1 my2 = l_my[0]; // my2=null } MyCl1 cl1; //========================================== private void init_cl1() { cl1 = new MyCl1(); } } public class MyCl1 { }
Read Entire Article