Click here to Skip to main content
15,880,503 members
Home / Discussions / C#
   

C#

 
QuestionStatic constructor [SOLVED] Pin
mabrahao6-Jan-10 0:42
mabrahao6-Jan-10 0:42 
AnswerRe: Static constructor Pin
Saksida Bojan6-Jan-10 0:54
Saksida Bojan6-Jan-10 0:54 
GeneralRe: Static constructor Pin
OriginalGriff6-Jan-10 1:04
mveOriginalGriff6-Jan-10 1:04 
GeneralRe: Static constructor Pin
Saksida Bojan6-Jan-10 1:07
Saksida Bojan6-Jan-10 1:07 
GeneralRe: Static constructor Pin
mabrahao6-Jan-10 1:05
mabrahao6-Jan-10 1:05 
GeneralRe: Static constructor Pin
Pete O'Hanlon6-Jan-10 1:22
mvePete O'Hanlon6-Jan-10 1:22 
GeneralRe: Static constructor Pin
Saksida Bojan6-Jan-10 1:28
Saksida Bojan6-Jan-10 1:28 
GeneralRe: Static constructor Pin
dojohansen7-Jan-10 4:03
dojohansen7-Jan-10 4:03 
I wouldn't use the terminology "static constructor" (as ALL constructors are NECESSARILY static - you cannot invoke an instance method until you've constructed an intance). But there certainly is something that (in C#) uses the keyword "static" followed by same syntax as a constructor - the class initializer.

Class initializers can be used if you want to perform initialization of some kind once before a type can be used. For example, let's say you are making a bookmaker system. The prices (odds) may vary from 1.01 to 1000, but not everything in between is legal. Instead you have something like this:

From    To    Increment
1.01    2.00  0.01
2.02    4.00  0.02
4.04   10.00  0.05
....
500    1000   10


You could use an Int32 to store the integral number of 1/100 units, i.e. 125 for 1.25, and cover more than the range you need. But if there are only 256 legal prices you could store it in just a byte. This could make a big difference when tens of thousands of transactions per hour are to be handled, especially in terms of writing data to a database.

Anyway, it doesn't really matter for our purposes whether or not the representation is useful! The point is this is a good candidate for a type initializer. Given a byte value of 135, we need to find price number 135 of the legal prices. So we can equip the class with an array and initialize the type before it's used like this:

public class OddsPrice
{
   // --- Class members --- //

   // Legal prices in ascending order.
   static int[] priceArray;

   // Type initializer: Initializes price array.
   static OddsPrice()
   {
       priceArray = new int[256];
       int price = 100; // cents
       int i = 0;
       while ((price += 1) <= 200) priceArray[i++] = price;
       while ((price += 2) <= 400) priceArray[i++] = price;
       ...
   }

   // --- Instance members --- //

   // Byte representation of value: Index in ordered set of legal values.
   byte value;

   // Value in cents
   public int Value
   {
      get { return priceArray[value]; }
   }
}


The type initializer will be called automatically at an undefined time (in current implementations of the CLR I believe it happens when the assembly is loaded, but the spec says undefined so don't assume anything) prior to the type being used at run-time.

The advantage is that you do not need to handle any thread-syncing to ensure it runs once and only once and before anything else on the type can be used.
AnswerRe: Static constructor Pin
V.6-Jan-10 1:02
professionalV.6-Jan-10 1:02 
GeneralRe: Static constructor Pin
mabrahao6-Jan-10 1:15
mabrahao6-Jan-10 1:15 
AnswerRe: Static constructor Pin
OriginalGriff6-Jan-10 1:07
mveOriginalGriff6-Jan-10 1:07 
GeneralRe: Static constructor Pin
mabrahao6-Jan-10 1:11
mabrahao6-Jan-10 1:11 
QuestionProblem in memory usage in .net Pin
Kim06186-Jan-10 0:11
Kim06186-Jan-10 0:11 
AnswerRe: Problem in memory usage in .net Pin
Harvey Saayman6-Jan-10 0:40
Harvey Saayman6-Jan-10 0:40 
GeneralRe: Problem in memory usage in .net Pin
Rob Philpott6-Jan-10 3:12
Rob Philpott6-Jan-10 3:12 
AnswerRe: Problem in memory usage in .net Pin
Dave Kreskowiak6-Jan-10 3:22
mveDave Kreskowiak6-Jan-10 3:22 
AnswerRe: Problem in memory usage in .net Pin
Luc Pattyn6-Jan-10 3:30
sitebuilderLuc Pattyn6-Jan-10 3:30 
GeneralRe: Problem in memory usage in .net Pin
Kim06186-Jan-10 6:02
Kim06186-Jan-10 6:02 
GeneralRe: Problem in memory usage in .net Pin
Luc Pattyn6-Jan-10 6:30
sitebuilderLuc Pattyn6-Jan-10 6:30 
QuestionProblem with insert query Pin
<<Tash18>>6-Jan-10 0:10
<<Tash18>>6-Jan-10 0:10 
AnswerRe: Problem with insert query Pin
SeMartens6-Jan-10 0:22
SeMartens6-Jan-10 0:22 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 0:23
<<Tash18>>6-Jan-10 0:23 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 0:27
SeMartens6-Jan-10 0:27 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 0:35
<<Tash18>>6-Jan-10 0:35 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 0:42
SeMartens6-Jan-10 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.