c# - Assignment in constructor vs direct assignment -
This morning a basic question arose in my head. Here's what:
There is a difference in doing this:
Public class mylas {private object _myField = new object (); ...}
and do the following:
public class MyClass {private object _myField; Public MyClass () {_myField = New Object (); } ...}}
Just to display ...
using the system; Public Essential Class Base Class {Public Base Class () {Console.WriteLine ("Results for {0}: {1}", GetType (), Colledbink Constructor ()); } Protected abstract string collidebconstructor (); } Public class convertible initiator: base class {private string foo = "foo"; Safe override string calledByConstructor () {return foo; }} Public class builder start: base class {personal string foo; Public Constructor Initialization () {foo = "foo"; } Safe override string calledByConstructor () {return foo; }} Public Sector Exam {Fixed Zero Main () New Constructor Start (); }}
Here the original class creator calls the abstract method applied in the child class - this means that we can see the state of the object before implementing its constructor body < / Em>. As you can see, the variable initializer has already been implemented - but
results for variability: case, where the initiator only In the constructor body, the
foo
still has its default value of zero.
Calling virtual ways from constructors is usually a very bad idea for a similar reason.
Comments
Post a Comment