Home |  JournalSeek |  SoftwareSeek |  GenomeSeek |  Expression |  Developer |  TakeOnIt
Return to Genamics Home

13. Versioning

Solving versioning issues has been a major consideration in the .NET framework. Most of these considerations apply to assemblies. There are some quite impressive capabilities such as running multiple versions of the same assembly in the same process.

The C# language prevents software failures when new versions of code (most notably the .NET libraries) are made. The C# Language Reference explains this in detail, but I've summarized the problem with a highly condensed example:

In Java, supposing we deploy a class called D which derives a class distributed with the VM, called B. Class D has a method called foo which B does not have at the time of distribution. Later, an update is made to class B, which now includes a foo method, and the new VM is installed on the computer running software which uses class D. The software using class D may malfunction because the new implementation of class B will make a virtual call to class D, performing an action with an implementation totally unintended by class B. In C#, class D's foo would have been declared without the override modifier (which expresses the programmer's intention), so the runtime knows to make Class D's foo hide Class B's foo, rather than override it.

An interesting quote from the C# reference manual was: "C# addresses this versioning problem by requiring developers to clearly state their intent". Although the use of the word override is one way to state intention, it could also be automatically generated by the compiler by checking to see if a method performs (rather than declares) an override at the time of compilation. This means you can still have Java-like languages (which don't use virtual and override keywords) and still cope with versioning correctly.

Also see Field Modifiers

14. Parameter Modifiers

"ref" parameter modifier

C# (as opposed to Java) lets you pass parameters by reference. The most obvious example to illustrate the point of this is the general purpose swap method. Unlike in C++, you must specify when calling as well as when declaring a method which takes ref parameters:

public class Test
{
    public static void Main ()
    {
        int a = 1;
        int b = 2;
        swap (ref a, ref b);
    }

    public static void swap (ref int a, ref int b) {
       int temp = a;
       a = b;
       b = temp;
    }
}

"out" parameter modifier

There is also an "out" keyword, which is a natural complement to the ref parameter modifier. While the ref modifier requires that a value is definitely assigned before passed into a method, the out modifier requires that the method's implementation definitely assigns the parameter a value before returning.

"params" parameter modifier

The params modifier may be added to the last parameter of a method so that the method accepts any number of parameters of a particular type. For example:

public class Test
{
    public static void Main () {
        Console.WriteLine (add (1, 2, 3, 4).ToString());
    }

    public static int add (params int[] array) {
       int sum = 0;
       foreach (int i in array)
           sum += i;
       return sum;
    }
}

One of the most surprising things when you're learning Java is not being able to pass by reference. It turns out though that after a little while you seldom miss this functionality, and write code which doesn't use it it. When I went over the C# specification for the first time, I often thought "Why have they got this or that functionality, I can code without it". With a little introspection, I realized this wasn't really an argument to show some functionality wasn't useful, but was more an argument to show that you get conditioned into getting by without it.

Java did a good thing when it simplified how parameters could be passed when you consider what C++ does. In C++, parameters of a method and a method call pass in values or references, which in addition to having pointers, can lead to unnecessarily complicated code. C# makes passing by reference explicit for both the method and the method call, which greatly alleviates any confusion, thus achieving the same goal as Java, but with greater expressiveness. It is clearly a theme of C# to not hedge programmers into situations where they require a round-about way of achieving something. Remember those Java tutorials that suggest that to overcome the pass-by-reference problem, you should pass a 1-element array to holding your value, or make another class to hold that value?


Previous     Next


C# 3.0 in a Nutshell (April 2007) - by Joseph Albahari and Ben Albahari.









Add To Favorites
Email This Page



Side Panel
Privacy Policy About Us Contact Us