Few objects need to be created without being initialized initialize objects during construction if initialization parameters are known.
Consider the following code|
MyObject m=new
MyObject(); m.x=2; m.y=4; |
| MyObject m=new MyObject(2,4); |
The first method of creating MyObject results in twice as many lines of IL code as the second and is roughly twice slower.
|
class A { public int x1=2; public A() { DoIt(); } public virtual void DoIt() { Console.WriteLine("Base"); } } |
|
class B:A { private bool x; public int x2=3; public B() { } public override void DoIt() { Console.WriteLine("Child"); } static void Main() { A a=new B(true);//results in Child being printed out } } |
|
class A { public void fun() { Console.WriteLine("Base"); } } class B:A { public void fun() { Console.WriteLine("Child"); } } A a=new B(); //What will a.fun() do ? |