{"id":533,"date":"2003-11-04T10:04:32","date_gmt":"2003-11-04T14:04:32","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=533"},"modified":"2003-11-04T10:04:32","modified_gmt":"2003-11-04T14:04:32","slug":"c-static-constructors-destructors","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/11\/04\/c-static-constructors-destructors\/","title":{"rendered":"C# static constructors, destructors"},"content":{"rendered":"<p>Spent some more time on the <a href=\"http:\/\/cephas.net\/blog\/2003\/10\/26\/logging_in_c_enumerations_threadsafe_streamwriter.html\">C# logging class<\/a> I&#8217;ve been working on.  Per Joe&#8217;s suggestions, I modified the StreamWriter so that it is now a class variable and as such, it need not be initialized every time the class is used.  Instead, you can use a static constructor (the idea exists in both C# and Java, although they go by different names). In C#, you simply append the keyword &#8216;static&#8217; to the class name:<br \/>\n<code><br \/>\npublic class Logger {<br \/>\n&nbsp;static Logger() {<br \/>\n&nbsp;&nbsp;\/\/ insert static resource initialization code here<br \/>\n&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nIn Java it&#8217;s called a static initialization block (<a href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/javaOO\/classvars.html\">more here<\/a>) and it looks like this:<br \/>\n<code><br \/>\npublic class Logger {<br \/>\n&nbsp;static {<br \/>\n&nbsp;&nbsp;\/\/ insert static resource initialization code here<br \/>\n&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nIf you&#8217;d like a real life example of a Java static initialization block, check out the <a href=\"http:\/\/jakarta.apache.org\/log4j\/docs\/download.html\">source<\/a> for the <a href=\"http:\/\/jakarta.apache.org\/log4j\/docs\/api\/org\/apache\/log4j\/LogManager.html\">LogManager<\/a> class in the log4j package.<\/p>\n<p>Anyway, now the Logger class declares a StreamWriter:<br \/>\n<code><br \/>\nprivate static StreamWriter sw;<br \/>\n<\/code><br \/>\nand then uses the static constructor to initialize it for writing to the text file:<br \/>\n<code><br \/>\nstatic Logger() {<br \/>\n&nbsp;&nbsp;\/\/ load the logging path<br \/>\n&nbsp;&nbsp;\/\/ ...<br \/>\n&nbsp;&nbsp;\/\/ if the file doesn't exist, create it<br \/>\n&nbsp;&nbsp;\/\/ ...<br \/>\n&nbsp;&nbsp;\/\/ open up the streamwriter for writing..<br \/>\n&nbsp;&nbsp;sw = File.AppendText(logDirectory);<br \/>\n}<br \/>\n<\/code><br \/>\nThen use the lock keyword when writing to the resource to make sure that multiple threads can access the resource:<br \/>\n<code><br \/>\n&nbsp;...<br \/>\n&nbsp;lock(sw) {<br \/>\n&nbsp;&nbsp;&nbsp;sw.Write(\"\\r\\nLog Entry : \");<br \/>\n&nbsp;&nbsp;&nbsp;...<br \/>\n&nbsp;&nbsp;&nbsp;sw.Flush();<br \/>\n&nbsp;}<br \/>\n<\/code><br \/>\nNow all objects that call the various static methods will be using the same private streamwriter.  But you&#8217;re left with one more problem.  The streamwriter is never explicitly closed.  If the StreamWriter was an instance variable, then we could solve this by implementing a <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/csref\/html\/vclrfClassDestructors.asp\">destructor<\/a>. The destructor would take this form:<br \/>\n<code><br \/>\n~ Logger() {<br \/>\n&nbsp;&nbsp;try {<br \/>\n&nbsp;&nbsp;&nbsp;sw.Close();<br \/>\n&nbsp;} catch {<br \/>\n&nbsp;&nbsp;&nbsp;\/\/ do nothing, exit..<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nHowever, in this case the StreamWriter is a static\/class variable, no &#8216;instance&#8217; of Logger ever exists in the system, the ~Logger destructor will never get called.  Instead, when the StreamWriter is eligible for destruction the garbage collector runs the StreamWriter&#8217;s Finalize method (which itself will then presumably call the Close() method of the StreamWriter instance), which will then automatically free up the resources used by the StreamWriter.<\/p>\n<p>I updated the Logger class and it&#8217;s personal testing assistant TestLogger (which has also been updated to use 3 threads). You can download them here:<\/p>\n<p>&middot; <a href=\"\/images\/files\/Logger.cs\">Logger.cs<\/a><br \/>\n&middot; <a href=\"\/images\/files\/TestLogger.cs\">TestLogger.cs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spent some more time on the C# logging class I&#8217;ve been working on. Per Joe&#8217;s suggestions, I modified the StreamWriter so that it is now a class variable and as such, it need not be initialized every time the class is used. Instead, you can use a static constructor (the idea exists in both C# &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/11\/04\/c-static-constructors-destructors\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C# static constructors, destructors<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/533"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=533"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}