I have doubt in static and non static constructor;
please see following example carefully here I am declaring static and non static constructor and accessing static and non static members from it
class SampleConstructor
{
public static int s;
public int ns;
static SampleConstructor()
{
s = 23;
}
public SampleConstructor()
{
ns = 33;
}
}
class UseSampleConstructor
{
static void Main(string[] args)
{
SampleConstructor sc = new SampleConstructor();
Console.WriteLine("{0},{1}" ,sc.ns,SampleConstructor.s);
Console.ReadKey();
}
}
------------------------------------------------------------------------
output of above example is 33,23
which is completely fine however when I am trying to initialize static member of class through non static constructor without altering anything like this
class SampleConstructor
{
public static int s;
public int ns;
static SampleConstructor()
{
s = 23;
}
public SampleConstructor()
{
s = 45;
ns = 33;
}
}
class UseSampleConstructor
{
static void Main(string[] args)
{
SampleConstructor sc = new SampleConstructor();
Console.WriteLine("{0},{1}" ,sc.ns,SampleConstructor.s);
Console.ReadKey();
}
}
now In this case I am getting output as 33,45
why??? even though I am still accessing SampleConstructor.s which is static is member of static constructor
can anyone please help me??