using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestConstructorSeq
{
class Program
{
public class A
{
public A()
{
Console.WriteLine("I am A");
}
static A()
{
Console.WriteLine("I am static A");
}
}
public class B:A
{
public B()
{
Console.WriteLine("I am B");
}
static B()
{
Console.WriteLine("I am static B");
}
}
static void Main(string[] args)
{
B b = new B(); //This outputs
//I am Static B
Console.ReadLine(); //I am Static A
} //I am A
//I am B
}
}
=================================================================================
But this static constructor execution pattern changes in this 3 level inheritance
=================================================================================
class Program
{
public Program()
{
Console.WriteLine("I am p");
}
static Program()
{
Console.WriteLine("I am sp");
}
class Program1:Program
{
public Program1()
{
Console.WriteLine("I am p1");
}
static Program1()
{
Console.WriteLine("I am sp1");
}
}
class Program2 : Program1
{
public Program2()
{
Console.WriteLine("I am p2");
}
static Program2()
{
Console.WriteLine("I am sp2");
}
}
static void Main(string[] args)
{
Program2 p2 = new Program2();
Console.ReadLine();
}
}
}
//Check out the difference between the two
Wednesday, April 23, 2008
Dynamic Binding with datahiding query
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.FrameWork;
namespace DynamicBindingWithHiding
{
class A
{
public virtual void Whoareyou()
{
Console.WriteLine("I am in A");
Console.ReadLine();
}
}
class B:A
{
public override void Whoareyou()
{
Console.WriteLine("I am in B");
Console.ReadLine();
}
}
class C : B
{
public new virtual void Whoareyou()
{
Console.WriteLine("I am in C");
Console.ReadLine();
}
}
class D : C
{
public override void Whoareyou()
{
Console.WriteLine("I am in D");
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
//C c = new D();
//c.Whoareyou();
A a = new D();
a.Whoareyou(); //It shows "I am in B" how?
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.FrameWork;
namespace DynamicBindingWithHiding
{
class A
{
public virtual void Whoareyou()
{
Console.WriteLine("I am in A");
Console.ReadLine();
}
}
class B:A
{
public override void Whoareyou()
{
Console.WriteLine("I am in B");
Console.ReadLine();
}
}
class C : B
{
public new virtual void Whoareyou()
{
Console.WriteLine("I am in C");
Console.ReadLine();
}
}
class D : C
{
public override void Whoareyou()
{
Console.WriteLine("I am in D");
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
//C c = new D();
//c.Whoareyou();
A a = new D();
a.Whoareyou(); //It shows "I am in B" how?
}
}
}
Subscribe to:
Posts (Atom)