Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I experimented with a code:


C#
class gen<t>
   {
       static int count = 0;
       static gen()
       {
           Console.WriteLine("created: {0}",count.ToString());
           count++;
       }

   }


C#
class Program
   {
       static void Main(string[] args)
       {

           gen<string> g1 = new gen<string>();
           gen<int> g2 = new gen<int>();
           gen<guid> g3 = new gen<guid>();



           gen<string> g11 = new gen<string>();
           gen<int> g22 = new gen<int>();
           gen<guid> g33 = new gen<guid>();




           Console.ReadLine();

       }
   }



- It prints:
created: 0<br />
  created: 0<br />
  created: 0

- It prints this once for each type. If i use an instance constructor then the output would be:
created: 0<br />
  created: 0<br />
  created: 0<br />
  created: 1<br />
  created: 1<br />
  created: 1<br />

- This means that an instance constructor gets initialized every time. Now my doubt is that if use a static constructor then the object is created just once on the heap as against an instance constructor?
- The next thing is: whats better to use: static or an instance constructor.
- Because by using an instance constructor i can pass arguments multiple times depending upon the scenario: which means that i can produce a variety of result sets. Am i correct?
- When to use an instance constructor and when to use a static constructor?

Thanks and regards,
-Rahul
Posted
Updated 6-Jan-14 20:30pm
v2

As Static constructor called first time as class is referenced
C#
TestClass.MyStaticMethod()

So you need to create a static constructor when you want to initialize any static data, or to perform a particular action that needs to be performed once only.
and
use Instance constructor whenever you want to to create and initialize any instance member variables that you use the new expression to create an object of a class.
 
Share this answer
 
Comments
Rahul VB 7-Jan-14 5:53am    
Sir,
Thanks. This means that i can create a new variety of results using an instance constructor. And static constructor, fields, methods are initialized just once(I hope i have understood correctly).
That means :
Class()
{
static method1();
}

so if i say:
Class.Method1(); //then this would work just once?
then what would happen if i say :
Class.Method1() ;
Class.Method1() ;
Class.Method1() ;
/// then the output would be just once? as i am using a static method? Please correct me if i am wrong.

Thanks,
- Rahul
uspatel 7-Jan-14 7:15am    
static method are called by classed name.as you code,these method called three times.
But here we are discussing static constructor
public class SS
{
static readonly long baseline;
static SS()
{
baseline = DateTime.Now.Ticks;
}
public static void Method(int a)
{

Console.WriteLine(a);
Console.WriteLine(baseline);
}

}
Now call as
public static void Main()
{

SS.Method(10);
SS.Method(20);
SS.Method(30);
Console.ReadKey();
}
See example here value of baseline will be same as it is static.

10
635247135133415819
20
635247135133415819
30
635247135133415819
Rahul VB 7-Jan-14 7:22am    
Sir,
Ok now i get it, so a static constructor is initialized once.
Thanks,
-Rahul
1.In generally a static constructor is used to initialize the static members of a class and it is invoked first time before to create a new object of that type OR before to invoke any of its static members. So the static constructor cannot be invoked manually and they are automatically invoked by the .NET before to access any member and methods of the class (including the non static constructors)!!!

2.In your example you are using templates and is more complicated, but the principle is the same: when you invoke an instance constructor for a given "template type", then ONLY if is the first time when that "template type" is used, before you instance constructor code to be executed, the static constructor is executed like the 1st rule above said.

PS: To understand better the static constructors I recommend you first to test them for normal classes (that are not using templates), then to complicate the problem like in your example by using templates.
 
Share this answer
 
v2
MSDN wrote:
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.


I would suggest to start with basics:
Using Constructors (C# Programming Guide)[^]
Static Constructors (C# Programming Guide)[^]
Instance Constructors (C# Programming Guide)[^]
Classes (C# Programming Guide)[^]
Static Classes and Static Class Members (C# Programming Guide)[^]
Abstract and Sealed Classes and Class Members (C# Programming Guide)[^]
Access Modifiers (C# Programming Guide)[^]
Object and Collection Initializers (C# Programming Guide)[^]
Reflection and Generic Types[^]
Auto-Implemented Properties (C# Programming Guide)[^]

You are probably looking for something similar to: C# Class Auto increment ID[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900