Saturday, November 04, 2006

Static Constructors


Static Constructors:
  1. Cannot have parameters
static A(int i) {} // error CS0132: 'A.A(int)': a static constructor must be parameterless
  1. Cannot have accessibility modifiers
public static A() {}; // error CS0515: 'A.A()': access modifiers are not allowed on static constructors

  • Cannot be called explicitly

  • Called when the class is loaded for the first time

  • Run at most once per application domain

  • Can be declared for a struct as well

  • Execution is triggered by when an instance of the class is created or any of the static members of the class are referenced.

  • For a type is run at most once per application domain.

  • That are marked extern usually specifies a DllImport attribute.  Note that the keyword here is usually.   The compiler doesn’t mandate you to specify one.  It merely generates a warning when the following is compiled into a library or application.

  • TestSC.cs(3,16): warning CS0626: Method, operator, or accessor 'A.A()' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation.  

public class A
{
     extern static A();
     static int i =2;
     public static void Main() { return; }

}

Upon execution of the application it throws a TypeLoadException:
Unhandled Exception: System.TypeLoadException: Could not load type 'A' from assembly 'TestSC, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because the method '.cctor' has no implementation (no RVA).
  1. When present in a class force the compiler to omit beforefieldinit declaration.  The following text discusses ramifications.

Verbatim from C# Specification v.1.0: If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class

Program #1
//Whether field i or j would be initialized first is implementation dependant.
class A
{
     static int i = 2;
}

class B
{
     static int j = 5;
}


On ILDASM

.class public auto ansi beforefieldinit A
       extends [mscorlib]System.Object
{
} // end of class A


Program #2
class Test
{
     public static void Main()
     {
          System.Console.WriteLine(“j = {0}, i={1}”,B.j,A.i);
     }
}
class A
{
     static int i = 2;
     static A() {};
}

class B
{
     static int j = 5;
     static B() {};
}

Since B.j is used first, field ‘j’ is guaranteed to get initialized first.

On ILDASM

.class public auto ansi A
       extends [mscorlib]System.Object
{
} // end of class A


     





Note that there’s no beforefieldinit in the declaration:

  1. Can have a return statement with no expression terminating it.

Usage:
  1. Can be used to assign computed static fields and static readonly fields

  2. To make sure that the type is not marked with beforefieldinit.


Explicit Interface Implementation

I was looking through my study notes when I came across several short programs I wrote sometimes ago that highlight important features of the C# language.  I’ll be posting the same in the coming posts with examples of when to use them.

//Explicit interface definition
//Be able to call an interface method ONLY through that interface

using System;

public class TestInterface : ITestInterface
{
     public void Method1()
     {
          Console.WriteLine("Method 1 Called");
     }     

     public void CallMethod1()
     {
          Method1();
     }
     public void CallMethod2()
     {
          ((ITestInterface)this).Method2();
          Method2();
     }


     ///ITestInterface implementation
     ///Two separate implementations of methods, both explicit and implicit
     

     //Explicit interface implementation
     void ITestInterface.Method2()
     {
          Console.WriteLine("Method 2 Called through ITestInterface");
     }

     //Implicit interface implementation
     public void Method2()
     {
          Console.WriteLine("Method 2 called through the class reference");
     }
}

public class InterfaceCaller
{
     public static void Main()
     {
          TestInterface ti = new TestInterface();
          ti.CallMethod1();
          ti.CallMethod2();
          ti.Method2(); //No such method compiler error if implicit interface implementation is omitted.
     }
}




public interface ITestInterface
{
     void Method1();
     void Method2();
}


Usage Scenarios:

  • Explicit Interface Implementation: Restrict the method access to be through the defined interface only.

  • While writing wrappers you want to restrict your team members to work only with the interface that you provide.  You may have earmarked the class for refactoring later.

  • Both EII & III: Provide two different implementations of the same method.

  • For me personally, this is dangerous.  Unless your specific situation demands that you employ this feature, it is better to leave this alone.

Thursday, October 19, 2006

TIFF support for IE

IE 6.0 does not have support for TIFF built-in. Here's an useful ActiveX control from Alterna that enables TIFF support. Right click on the TIFF to view, select "Open With" and choose "Internet Explorer".

Thursday, July 20, 2006

Bluetooth for VOIP

I've been in UK for the past couple of months on a business trip. What best way to be in touch with friends if not VOIP? The glich: No built-in mic on my laptop. When I was looking for a mic, I stumbled on a Bluetooth earpiece on Amazon. I also own a Sony Ericsson w800i mobile phone that needed this for quite sometime. Suddenly the brain wave: Why can't I use this bluetooth earpiece for VOIP? Amazon rescued me once again with the "People who bought this also bought" refrain. Belkin Bluetooth Adapter! I'm so impressed! I strongly recommend BBA for anybody who wants to use their bluetooth earpiece for VOIP. In 50 pounds, I was online in no time at all.

Tuesday, July 11, 2006

A Downloader

I was enthralled by the peugeot parking game . I wanted to have this game on my local machine so I could play this whenever I wanted to. I also have the faced the same problem when I'm browsing PDF documents on the Net that disallows saving. I wrote this simple C# program that did it for me. Thanks to .Net SDK 2.0, it's just a couple of lines of code: using System; using System.Net; using System.IO; public class Downloader { public static void Main(string [] args) { if(args.Length == 0 || args.Length > 2) { Console.WriteLine("Usage: Downloader <Url to download> <new filename>"); return; } WebClient client = new WebClient(); string filename = @".\" + Path.GetFileName(args[0]) ; if(args.Length ==2) filename = args[1]; client.DownloadFile(args[0], filename); } } Now, I can play the game whenever I want :)

Tuesday, June 20, 2006

Posting in Tamil

My system did not have any Tamil fonts. I installed Microsoft's Language Interface Pack. I'm able to use Tamil as a keyboard...ஆனால், now my damned windows interface has gone for a toss. I've been trying with the Language & Regional Options applet to change my UI language back to English to no avail! Windows XP is supposed to be having a "UI / menu language" listbox on the language tab. It's not there, I swear!

MS Word Woes

I've been editing a word document that I need to submit by EOD. This document is having lots of screenshots documenting an existing application. Now, word has an annoying habit of showing only blank spaces for the screenshots. I click them and it shows me an outline (a placeholder where the image is supposed to be!) The background refreshes are so bad that I have to click on the image each time and scroll up and down a couple of times to get it to show. It's maddening and I am getting madder by the minute.