Google Native Client!?

Today I need your help!

What are the differences between the new technology called native client from google compared to existing technologies like Java from Sun and .Net from microsoft?

The only benefit I figured out is performance. But wouldn’t it burst another security hole into the browser?

Didn’t we already had such a ‘native client’ with ActiveX? And this will be plattform dependend, right?

Or is this only a marketing gag?


Debug your NUnit Tests in VS Express 2005

Today it was a lucky day and I found out how I can make my unit tests working in VS Express 2005.

Normally you can attach the debugger to the nunit-GUI – but not in the Express Version!

This is really annoying. So look here for the workaround.

If you are new to C# it is a little bit different as in Java to create a test case.

You can do the following

  1. Install NUnit
  2. Create a new project with your sources. E.g. call it HelloWorld. (choose Windows Application)
  3. Add a new project with your tests to the solution. E.g. call it HelloWorldTest. Right click the solution and click ‘New Project’ (choose class library)
  4. Then right click HelloWorldTest and ‘Add Reference’. Browse until you find \ProgramFiles\NUnit\bin\nunit.framework.dll
  5. Add some unit test to HelloWorldTest
  6. Apply the mentioned hack above on HelloWorldTest.csproj
  7. Right click HelloWorldTest and click ‘Specify as StartUp Project’
  8. Press F5 and you should see the NUnit-GUI running your test cases. Now you can set breakpoints and evaluate expression in the test class (like you would do in HelloWorld).

Dependency Injection – 3 Questions!

Normally the reason why I write blogs is to inform other people about interesting things I found or I am thinking about. Today I will ask some questions and wait for your answer 😉

One of my latest posts was about the spring rich client. Because of this project and picocontainer I read a lot about design patterns. The most obvious design pattern in spring is called dependency injection. This design pattern helps the software architect to loosly couple components. It could work like follows (just for the JSE-developers under us):

class ClassA {
MyInterface objectA;
public void setMyInterface(MyInterface objA) {objectA = objA;}

where we could have defined the interface like this:

interface MyInterface{ String toString(); }

Now, ClassA defines a dependency on an instance of MyInterface and the framework (for Java e.g. picocontainer, spring, juice or others) will inject an instance of an implementation of MyInterface into ClassA. It is up to the configuration if objA is a newly created instance or if it is a ‘singleton’. (MyInterface could be a class as well.)

For example if you call

ClassA classA = framework.getBean(ClassA.class); //code is similar to picocontainer; not to spring!

You will get back an instance of class ClassA, where objectA is not null! The dependency was defined by the method (setMyInterface) – this is called setter injection. Other kinds of injections are:

  • annotation-based injection:
    class ClassA { @Inject Object objectA; }
  • constructor injection:
    class ClassA { Object objectA;
    public ClassA(ObjectA objA) {objectA = objA;}

Where picocontainer campaigns for constructor and spring for setter injection (but both projects offer at least the mentioned kinds of injection).

It is correct that you can create your own small framework or even set up the objecs by hand to achieve the same: dependency injection. But I guess you are lazy and will choose one of the open source frameworks.

Now, all is explained to understand my 3 questions. Hopefully someone out there will have an answer!

1. How should I design a library?

Let me explain. You want to sell a nice designed, but complex library. The library offers a lot of functionality and you want to split it into loosly coupled components. That means you have to use dependency injection (DI), but you don’t know if the customer has or wants such a DI-framework.

So, if you use e.g. the annotation-based injection it would be nearly impossible for the customer to use your library; with setter or constructor injection it is quite difficult to set up the objects for using your library.

I don’t know what to do: Should I really use dependency injection here? Or should I use a (dynamic) service locator instead; to decouple my components within the library?

Martin Fowler says:

“It (dependency injection) tends to be hard to understand and leads to problems when you are trying to debug. […] This isn’t to say it’s a bad thing, just that I think it needs to justify itself over the more straightforward alternative (service locator).”

A common reason people give for preferring dependency injection is that it makes testing easier. The point here is that to do testing, you need to easily replace real service implementations with stubs or mocks. However there is really no difference here between dependency injection and service locator: both are very amenable to stubbing. I suspect this observation comes from projects where people don’t make the effort to ensure that their service locator can be easily substituted.

“So the primary issue is for people who are writing code that expects to be used in applications outside of the control of the writer. In these cases even a minimal assumption about a Service Locator is a problem.”

So: What would you do? Would you use a service locator or dependency injection inside the library?

And this leads me to the next important question:

2. Why is a singleton an antipattern?

I stumbled over this question while I used picocontainer. They say you have to avoid singletons, because it is difficult to test and to replace. My question is: why? Imagine the following singleton:

class LogFactory{
public static Logger getLogger(Class clazz) { … }

Now it is really easy to replace the implementation of the Logger interface e.g. if one defines a setImpl method:


So what’s soo wrong with a singleton?

3. How can I avoid making all classes public?

I don’t know if it is the case for autowiring in spring. But in picocontainer you have to set-up the configuration for the wiring of the classes by yourself e.g. in a separate class (with Java; no xml! compiler checks references! yeah!) or in a separate file with a scripting language (or xml) and nanocontainer. That means you could specify the implementations of the interface by:

framework.setImpl(MyInterface.class, FirstImpl.class);

But wouldn’t it mean that I have to add the modifier public to the class FirstImpl? (To let picocontainer call the default constructor.) This can’t be good design to allow the user of my library to see the implementations of MyInterface.

Whats wrong with my use case and my conclusion?

C# 4 Java Programmers

“Today” I learned C# and I will post some intersting facts about Java and C#. But please, this is just a draft. Maybe this wikipedia article is a better place for you.

And please correct me, if I posted mistakes or invalid facts about C#. Or if you missed a great feature of C# or an example of the facts, then please let me know.


  • All .NET languages are compatible to each other. I know that there are now some more languages for the jvm, but the jvm was published with only one language: Java.
  • package in Java will be namespace in c#; the root is “System”
  • style convention: a method starts with upper case: Method()
  • System.out.println(String) will be Console.Write(“Hello {0}!”, name);
  • Java String = c# string or String
  • Java Object = c# object or Object
  • Java boolean = c# bool
  • all types are objects (even Java primitives)
    e.g. if you write int in c# the class Int32 will be used
    the ‘c# primitives’ extend ValueType
    struct’s and enum’s are value types too
  • Unboxing: intVar = Convert.ToInt32(obj);
  • for enums only byte, short, long and int are valid (no string!)
  • int intDigit = Convert.ToInt32(Console.ReadLine()); //similar to Integer.parseInt(String); in Java
  • check arithmetic over- and underflows:
    long lngVariable = 235235252525L;
    int byteVar = checked((int)lngVariable); // raises an exception if var is not in bounds and even “checked { block; }” is possible or set properties in project->properties->Build->Advanced->Check for arithmetical….
  • more dimensional arrays in c# looks like:
    int[,] zelle = new int[4,3];
    int[,] point = new int[,]{{1,2,3},{4,5,6}}
    int[,] point = {{1,2,3},{4,5,6}}
  • the main method in C# is: static void Main(string[] args) //case sensitive!
  • Where we write in Java: for(int tempElement : intArr){block};
    we will do the same in c#: foreach(int tempElement in intArr){block};
    c#: foreach(Object tempElement in objArr){block}; //tempElement can be changed!
  • change the standard “call by value” on value types:
    call a method by: TestProc(ref intVar);
    or define a method by: TestProc(ref int intVar);
    (similar to “ref” is “out”; used to get several results from a method)
  • Java’s varargs “…”: public void sum(Integer… args) {}
    c#’s params: public long Sum(params int[] args) {}
  • operator overloading:
    public static FloorDouble operator + ( FloorDouble fd1, FloorDouble fd2 ) {
    return new FloorDouble( fd1.Value + fd2.Value );
  • c# cloning: protected Object MemberwiseClone(); or via ICloneable
  • The relationship ‘has a’ in c# is really easy:
    class ClassA { private ClassB myObj; public ClassB MyObj { get {return myObj;} }}
    or e.g.: private int age;
    public int humanAge { get { return age; } set { age = value;} }
  • Java annotations = c# attribute
  • Java native = c# unsafe (you can use fixed, *pointer, ∫, a->b, … within c#!)
  • verbatim strings begin with the special character @. Please see this doc.
    string c = "hello \t world";               // hello     world
    string d = @"hello \t world";               // hello \t world
    string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
    string h = @"\\server\share\file.txt";      // \\server\share\file.txt

Object Oriented Programming

  • sealed in c# means final class in Java
  • you can split sources in c# over several source files with the partial keyword
  • method modifier:
    Java public = c# public
    Java private = c# private or without modifier
    Java protected = C# protected
    Java without modifier = ?
    C# internal = a member is accessible from the entire assembly.
    All the objects you define inside a .cs file (you can define more than one object
    inside the .cs file, unlike Java, where you can usually define only one object) have a handle to the internal member.
    C# protected internal – think of this one as the union of protected and internal,
    as the item is is modifying is either protected or internal. This item can be
    accessed from the entire assembly, or within objects which derive from this class.
  • class modifier:
    Java public = c# public
    Java private = c# internal or without modifier
  • All methods in a c# objects are “final” by default.
  • Java @Override = c# override (different behaviour if the super method is virtual or not!)
  • c# readonly is more or less final keyword for a variable in a Java object
  • c# const is more or less final keyword for a variable in a Java class
  • c#’s ‘extend’: class ClassA : SuperClassA
  • constructor chain in c#: Line(double X) : this(X) {}
  • Java super = c# base
  • method hiding:
    class ClassA { public void TestMethod() { Console.WriteLine(“ClassA.TestMethod()”); }}
    calling ClassB.TestMethod() results in calling ClassA.TestMethod()!
    class ClassB : ClassA { private new void TestMethod() { Console.WriteLine(“ClassB.TestMethod()”); }}
  • Java instanceof is c# is: if(myObj is ClassA) {}
  • static constructor
    Java: class ClassA{ {/*here*/}}
    c#: class ClassA{ static ClassA() {/*here*/}}
  • function pointer in c#: public delegate void MyDelegate(int value1);
  • anonym method in c#: MyDelegate del = delegate(int value1) { return value1; };
    call via e.g.: del(123);
    keyword “event” in c#: public event MyDelegate MeasureError;
    (same as the listener concept in Java)
    to add a listener use: object.MyDelegate += new MeasureErrorEventHandler(MyError);
    cannot be used from a child class!
  • Java final method = c# sealed method


In c# we have real generics, with separate namespace: System.Collections.Generic (instead System.Collections)

For a direct comparison of these two packages, please visit this page of galileo computing and go to the chapter 7.4.8.

But it seems to me the Java collection API has some more Classes, e.g. today I missed a Set. Okay we can simulate it via a SortedList<Key, Value> …

  • use return default(T); to return 0 or null, because T could be a reference or a value type
  • System.Nullable<T> now we can nullable even value types! (Nullable.HasValue)
    usage: int? intVar;
  • C# IList<T> interface will be implemented by the generic type: List<T> (no generics: Array and ArrayList). Only Array allows arrays that are not zero-based.
  • (in Java it is the List<T> interface with ArrayList<T> and LinkedList<T> as implementations)
  • C# ICollection<T> interface: LinkedList<T> (doesn’t implement IList!?), Queue<T> and Stack<T> (no generics: Queue and Stack)
  • (in Java it is the Collection<T> interface)
  • C# IDictionary<K,V> interface: Dictionary<K,V> and SortedList<K,V> (no generics: Hashtable and SortedList)
  • (in Java it is the Map<K,V> interface)
  • C# abstract KeyedCollection<K,V> behaves like a dictionary and a list at the same time. Thats great!
  • Sort an array via:
    ArrayList list = ArrayList.Adapter(array);
    class Sorter : IComparer { public int Compare(object a, object b) {return 0,1,-1}}

    list.Sort(new Sorter());
  • C# has the keyword yield, which is useable if you implement IEnumerable. E.g.: IEnumerator GetEnumerator() {
    for(int i = 0; i < months.Length; i++) { yield return month[i]; }}

Some other ‘array’ classes

in C#: there is a BitVector which is variable in length and allocated on the heap (BitVector32 has only 32 bits and allocated on the stack, so its faster.)

In Java there is similar class BitSet.


  • add an existing project to the explorer: right click the explorer-> add->existing item
  • Quick Fix via: click on the small icon which pops up if you go with the mouse to an underlined error
  • in NetBeans: create a new class; extend it with the JPanel class -> not editable via visual editor
  • BUT in Visual Studio: new Class; extend it with the Panel class -> editable via DnD !!
  • There is a very small ‘button’ in the top right corner of every editor: You can drop it down and this will result in two views (horizontal splitted editors) of the current source file!
  • TODO: add more features that I found


What’s better in Java?

  • free (Open Source) and fully functional Virtual Machine
  • free (Open Source) and fully functional class library
  • free (Open Source) and fully functional IDE’s: netbeans, eclipse
  • mass of open source libraries
  • platform independed! (I know there is Mono, but when was the last time you tried to run the Visual Studio on Mono? And then look at Java: Eclipse, NetBeans, etc – all will run at least on linux and windows!)

Although I love Java there are really cool things in C#:

  • real generics!
  • instead setter/getter you can use label1.Text = “Test1”; (Accessors? )
  • XmlSerializer
  • linq
  • it is possible to overload operators
  • you can use string in switch statements!
  • you can use goto to escape from nested if statements!

Feel free to post your experiences with C#!


English documentation:

German documentation: