c# - How do i prevent a class referencing its own class? -


how prevent making class implicitly referencing own class?

example:

class foo {     public foo(int y)     {         x = y;     }     public int x; } class program {     static void main()     {         foo myfoo1 = new foo(10);         foo myfoo2 = myfoo1; //how prevent this?         console.read();     } } 

you cannot prevent this. myfoo1 of type foo, can assigned myfoo2, period.

in case, it's not clear believe can gained preventing this. since foo reference type (a class), result of assignment had 1 name refer foo object created, have two. since names there developer's convenience, there no difference in behavior of program. assignment not create object.

note if foo value type (a struct) assignment make copy of value; myfoo1 , myfoo2 independent each other.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -