I know the answer (it's 42)

A blog on coding, .NET, .NET Compact Framework and life in general....

Posts
  • I know the answer (it's 42)

    C# : Enum and overriding ToString on it

    • 50 Comments

    I saw two posts on Enums today on Eric Lipperts and Chris Rathjen's blog. Enums are significantly different from the other types and people run into unusal problems while working with them.

    C++

    The CLI enum is considerably different from the native C++ enum and therefore you need to be careful how you define and use them in Managed C++. In new syntax (C++/CLI whidbey) the following code compiles even though both the enums define the same name Cool.

    enum class Coolness // class indicates this is a C++/CLI enum

    {

    NotSoCool,

    Cool,

    SuperCool

    };

    enum class TempRange

    {

    Cool,

    Hot,

    VeryHot,

    };

    In old syntax (pre whidbey) you need to define the enum as __value enum Coolness. In case you drop the class then you are using the native C++ enum and according to C++ specification the enumerator declarations are promoted to the same scope as the enumerated type declaration and the code will fail to compile pointing out a redefinition of Cool.

    C#

    In C# we do need not care about these as there is only one definition of enum which is that of the CLI enum.

    Common things like the first enum member get the value 0, the value of a subsequent member is calculated by adding 1 to the member textually preceeding it, you can assign values explicitely to a enum member are well-known. However there are some not so well known usages as well.

    enum Coolness : byte

    {

    NotSoCool = 5,

    Cool,

    VeryCool = NotSoCool + 7,

    SuperCool

    }

    Coolness coolType = Coolness.VeryCool;

    Console.WriteLine("Underlying type: {0}", Enum.GetUnderlyingType(coolType.GetType()));

    Console.WriteLine("Type Code : {0}", coolType.GetTypeCode());

    Console.WriteLine("Value : {0}", (byte)coolType);

    By default the compiler uses Int32 to store the enum members. Here we are asking it to use byte. VeryCool uses a reference to the NotSoCool and will get the value 12. So the out put of the code above will be

    Underlying type: System.Byte
    Type Code      : Byte
    Value          : 12

    Since all enums have System.Enum as the abstract base type, a lot of funcionality becomes available to get details about the enum type.

    If you want to print the value of the enum then it can be done in the following ways

    Console.WriteLine(coolType.ToString("G")); // name of the constant

    Console.WriteLine(coolType.ToString("F")); // name of the constant

    Console.WriteLine(coolType.ToString("X")); // value is hex

    Console.WriteLine(coolType.ToString("D")); // value in decimal

    Output:
    VeryCool VeryCool 0C 12

    F and G gives the same value in this case. They differ based on whether FlagsAttribute is applied to it.

    You can also get a array filled with either the value (GetValues) or names (GetNames) of all the constants in the enum.

    string[] names = Enum.GetNames(typeof(Coolness));

    int index = 0;

    foreach (Coolness coolVal in Enum.GetValues(typeof(Coolness)))

    {

    Console.WriteLine("{0,-10} => {1}", names[index++],

    coolVal.ToString("D"));

    }

    This prints

    NotSoCool  => 5
    Cool       => 6
    VeryCool   => 12
    SuperCool  => 13

    You can also query the name of the constant in the enum that has the specified value or whether a value is defined in the enum or not. The following will print cool and 5 is not defined.

    Console.WriteLine(Enum.GetName(typeof(Coolness), 6));

    if(!Enum.IsDefined(typeof(Coolness), (byte)7))

    Console.WriteLine("5 is Not Defined");

    Overriding ToString()

    You cannot override the ToString of the enum type. So in case you wanted to display "Not so cool" instead of NotSoCool when someone called ToString on your enum type then you cannot do that simply by overriding the ToString.

    This is a common issue that comes up frequently when you want to show values in reports, web pages, XML where you want to put in human readable text for enum values. Commonly people use non-generic solution of maintaining arrays of these descriptions and get text out of them by indexing using the enum value or some other things like storing it in a hashtable and using the ToString value as the key to get the desciption out.

    A generic solution would be to apply custom attributes on the enum constants and write static methods to get the desciption. See the modified code below

    using System;

    using System.Reflection;

    namespace FunWithEnum

    {

    enum Coolness : byte

    {

    [Description("Not so cool")]

    NotSoCool = 5,

    Cool, // since description same as ToString no attr are used

    [Description("Very cool")]

    VeryCool = NotSoCool + 7,

    [Description("Super cool")]

    SuperCool

    }

    class Description : Attribute

    {

    public string Text;

    public Description(string text)

    {

    Text = text;

    }

    }

    class Program

    {

    static string GetDescription(Enum en)

    {

    Type type = en.GetType();

    MemberInfo[] memInfo = type.GetMember(en.ToString());

    if (memInfo != null && memInfo.Length > 0)

    {

    object[] attrs = memInfo[0].GetCustomAttributes(typeof(Description),
    false
    );

    if (attrs != null && attrs.Length > 0)

    return ((Description)attrs[0]).Text;

    }

    return en.ToString();

    }

    static void Main(string[] args)

    {

    Coolness coolType1 = Coolness.Cool;

    Coolness coolType2 = Coolness.NotSoCool;

    Console.WriteLine(GetDescription(coolType1));

    Console.WriteLine(GetDescription(coolType2));

    }

    }

    }

    Using this approach is pretty generic because for all enum constants that have this attribute applied to it, the desciption will be picked up from it and for those that do not have the attribute the common ToString() method will be called to get the description. However, the GetDescription uses reflection and can be slow.

    The method shown above needs an explicit method call. If you prefer enum.GetDescription() then you need to use extension method as outlined here.

  • I know the answer (it's 42)

    C# 3.0: Lambda expressions. I don't like it that much

    • 59 Comments

    This is the my third post on the series of post I am making on C#3.0 after it got declared on PDC. See the previous two here and here.

    It's not that I do not like lambda expression, I just don't like that its being supported in C#. Lets first see what its like and then I'll clarify on why I do not like it. If you are just interested on why I do not like it, go to the end of this post.

    What is Lambda Expression

    In C#2.0 anonymous methods were introduced ( :(  I'm already using past tense on a product that is still not formally released). It required a little cumbersome syntax to create in-line function block like the ones supported by many functional languages. Lambda expressions in C# along with type inference give a cleaner (????) way of declaring and using anonymous methods.

    Lambda expressions are based on lambda calculus. Many functional languages like Lisp use lambda notations to define functions. Typical lambda expressions in C# can contain both expressions and or statement blocks.

    x => 2 * x // this is a expression
    y => {return 2*x; } // this is a statement block

                        //
    and not supported in the PDC bits.

    Here the => is the lambda operator.

    The following delegate and lambda expression are equivalent and the lambda expression here is automatically converted into a corresponding delegate.

    delegate T Func<T>(T t);
    Func
    <int> funcDel = delegate(int x)
                        {
                            return
    x + x;
                        };

    Func<int> lambda = x => x + x;

    Console
    .WriteLine(funcDel(5));
    Console.WriteLine(lambda(5));

    For the lambda expression the type can be explicitly specified or it can be implicit and the complier can figure it out. So the above expression could be written as

    Func<int> lambda = (int x) => x + x;

    Lambda with Extension methods

    Writing predicate functions for the extension methods becomes real easy with  lambda expressions. The System.Query namespace comes with nifty extension methods which use predicates. The following using the Where extension method prints out all the integers in the array which is less than 3

    int[] a = new int[] { 1, 2, 2, 3, 3, 5, 6 };
    var lessThan3 = a.Where(x => x < 3);
    foreach(var val in
    lessThan3)
        Console.WriteLine(val);

    Type Inference and Lambda Expression

    When a generic method is called without explicitly giving its type then the compiler can infer the type. In the following case both calls to GenericFunc is equivalent and will print System.Int32

    static void GenericFunc<T>(T t)
    {
        Console
    .WriteLine(t.GetType().ToString());
    }

    GenericFunc(5); // Type is inferred
    GenericFunc<
    int>(5);

    Just like any other expression when a lambda expression is passed as a parameter to a generic function it is used to infer the type. Lets take the following example which is a modified version of the one used in the C# 3.0 spec

    delegate R Func<A, R>(A a);

    // the generic function
    static
    Z FuncInfer<X, Y, Z>(X value, Func<X, Y> f1, Func<Y, Z> f2)
    {
        return
    f2(f1(value));
    }

    var size = FuncInfer(@"c:\vcvars32.bat", s => new FileInfo(s), t => t.Length);

    Lets see how the type inference happens here.

    • Since the first parameter is a string, compiler looks at it and infers X to be a string.
    • For the first lambda expression the input type is X so s becomes a string. Since it returns FileInfo the return type of it and hence Y becomes FileInfo.
    • For the second lambda expression Y is the input type and so t is FileInfo.
    • The second lambda returns FileInfo.Length so Z becomes long and hence size is a long and contains the size of the file passed as the first argument.

    Why I don't like lambda expression in C#

    C# has originally developed from C++ /Java and is (was :^) ) a strongly typed object-oriented language. The new features being introduced like closure, continuation (yes, yes very limited continuation) in C#2.0 and now type inference, lambda expressions are de-generating the language. Even though there are people go gaga about continuation and lexical closures these are fundamentally functional language features and should be left to that. Introduction of bits and pieces of functional language features are not going to add value to C# and at the same time the surface area of C# is growing beyond what most developer can grasp.

    Every language has a domain of application and an audience. Most people who work in C# developing web-services and web-based applications today are not necessary computer science grads. For them these concepts are a huge variation from the way C# used to behave. Functional programming is for the academia and not from the industry and should be left as such.

    If I am asked, whether I'll use these language features, my answer will be maybe, and that is because the code written by me will be reviewed and maintained by people in MS who are fairly conversant with these. However, I think these features add burden to the learning curve of people who are not super interested in C# and should be avoided in favor of easier language constructs’ that might take couple of lines of code more, but would be more readable and maintainable.

    These are just my opinions and I'm sure the designers of C# had given a lot of thought before adding them, but I strongly feel that these features are just adding more surface area to the language and will mostly go unused by the masses.

  • I know the answer (it's 42)

    Ruby: Webserver in 70 lines of code

    • 21 Comments

    <Updated the sources to add logging and default file index.html handling. Now the code is about 90 lines :(>

    I decided to write a http-server in Ruby on Windows to see how much code it requires as I have been reading about how Ruby gets your work done much easily and much faster. Some of the new things in C# 2.0 /3.0 have been already around in Ruby for some time and they make coding in Ruby fun and very interesting. I'll share my experiences about a some of the features in Ruby that I'd like to see in C#.

    This is a no-frills minimal implementation which any hacker can break in about 15 minutes :) So I deployed it over the intranet. I hosted my personal site from http://www.geocities.com/basuabhinaba on the server and it worked first time.  The code below should work without much modifications, just replace the IP address xxx.xxx.xxx.xxx to the one on your machine. I was amazed at how soon I was able to code this thing up in Ruby (doesn't say much about code quality though :) )

    Features

    1. Multi-threaded server
    2. Allows adding one base local folder from which pages are served. Request out side this folder would be refused (I hope it'll be refused)
    3. Supports common file formats like html, jpeg, gif, txt, css. I'll add more with time or I may just decide to use the Win32 API to read ContentType from the registry so that everything works
    4. Support for page not found

    Issues

    1. No security at all!!!! other than redumentary code to stop users from accessing files outside of the base folder
    2. No concept of default file (e.g. index.html) or directory listing in case request comes to access a folder
    3. There is some code in this that is windows specific
    4. No logging support for now

    Finally the Code

    It took me about 70 lines of code to get this to work.

    require 'socket'
    
    class HttpServer
      def initialize(session, request, basePath)
        @session = session
        @request = request
        @basePath = basePath
      end
    
      def getFullPath()
        fileName = nil
        if @request =~ /GET .* HTTP.*/
          fileName = @request.gsub(/GET /, '').gsub(/ HTTP.*/, '')
        end
        fileName = fileName.strip
        unless fileName == nil
          fileName = @basePath + fileName
          fileName = File.expand_path(fileName, @defaultPath)
          fileName.gsub!('/', '\\')
        end
        fileName << "\\index.html" if  File.directory?(fileName)
        return fileName
      end
    
      def serve()
        @fullPath = getFullPath()
        src = nil
        begin
          if File.exist?(@fullPath) and File.file?(@fullPath)
            if @fullPath.index(@basePath) == 0 #path should start with base path
              contentType = getContentType(@fullPath)
              @session.print "HTTP/1.1 200/OK\r\nServer: Makorsha\r\nContent-type: #{contentType}\r\n\r\n"
              src = File.open(@fullPath, "rb")
              while (not src.eof?)
                buffer = src.read(256)
                @session.write(buffer)
              end
              src.close
              src = nil
            else
              # should have sent a 403 Forbidden access but then the attacker knows that such a file exists
              @session.print "HTTP/1.1 404/Object Not Found\r\nServer: Makorsha\r\n\r\n"
            end
          else
            @session.print "HTTP/1.1 404/Object Not Found\r\nServer: Makorsha\r\n\r\n"
          end
        ensure
          src.close unless src == nil
          @session.close
        end
      end
    
      def getContentType(path)
        #TODO replace with access to HKEY_CLASSES_ROOT => "Content Type"
        ext = File.extname(path)
        return "text/html"  if ext == ".html" or ext == ".htm"
        return "text/plain" if ext == ".txt"
        return "text/css"   if ext == ".css"
        return "image/jpeg" if ext == ".jpeg" or ext == ".jpg"
        return "image/gif"  if ext == ".gif"
        return "image/bmp"  if ext == ".bmp"
        return "text/plain" if ext == ".rb"
        return "text/xml"   if ext == ".xml"
        return "text/xml"   if ext == ".xsl"
        return "text/html"
      end
    end
    
    def logger(message)
      logStr =  "\n\n======================================================\n#{message}"
      puts logStr
      $log.puts logStr unless $log == nil
    end
    
    basePath = "d:\\web"
    server = TCPServer.new('XXX.XXX.XXX.XXX', 9090)
    logfile = basePath + "\\log.txt"
    $log = File.open(logfile, "w+")
    
    loop do
      session = server.accept
      request = session.gets
      logStr =  "#{session.peeraddr[2]} (#{session.peeraddr[3]})\n"
      logStr += Time.now.localtime.strftime("%Y/%m/%d %H:%M:%S")
      logStr += "\n#{request}"
      logger(logStr)
    
      Thread.start(session, request) do |session, request|
        HttpServer.new(session, request, basePath).serve()
      end
    end
    log.close
    

     

  • I know the answer (it's 42)

    C# 2.0: Loading plugins at run-time using late binding

    • 29 Comments

    I was working on a home project for creating a motion-detector that works using a webcam. Instead of using a baby-monitor I am planning to run this on an old computer. I'll point the web-cam to my daugthers' crib and the program would monitor both sound and her motions and ring an alarm in case she moves or makes any noise.

    While working on this I wrote some code to support plugins to the application so that I can choose and use any motion-detection algorithm that I want. Some time back there was some questions on our internal DL about using late-binding to load and execute code. So I thought I'd blog about the code I used for the plugin loading and executing.

    I used the following steps for this

    • List all dlls is a directory (plugins directory)
    • Load all assemblies from this dir
    • Iterate through all the types in the assembly and look if the type implements the plugin interface
    • Create an instance of the type that implement the interface and store it in a List

    To do all of the above I wrote the following generic method that given any interface and a folder name can create a List of instances of all types that implement the plugin interface.

    using System.Reflection;

    using System.Collections.Generic;

    public List<T> GetPlugins<T>(string folder)

    {

    string[] files = Directory.GetFiles(folder, "*.dll");

    List<T> tList = new List<T>();

    Debug.Assert(typeof(T).IsInterface);

     

    foreach (string file in files)

    {

    try

    {

    Assembly assembly = Assembly.LoadFile(file);

    foreach (Type type in assembly.GetTypes())

    {

    if (!type.IsClass || type.IsNotPublic) continue;

    Type[] interfaces = type.GetInterfaces();

    if (((IList)interfaces).Contains(typeof(T)))

    {

    object obj = Activator.CreateInstance(type);

    T t = (T)obj;

    tList.Add(t);

    }

    }

    }

    catch (Exception ex)

    {

    LogError(ex);

    }

    }

    return tList;

    }

    With this method I can write code to show the list of plugins with their description in a menu or list control as follows

    string exeName = Application.ExecutablePath;

    string folder = Path.Combine(Path.GetDirectoryName(exeName), "Plugins");

    List<IMotionDetector> list = GetPlugins<IMotionDetector>(folder);

     

    m_listPlugin.Items.Clear(); // list box

    foreach (IMotionDetector detector in list)

    {

    string name = detector.GetPluginName();

    string desc = detector.GetPluginDescription();

    string str = string.Format("{0}, {1}, {2}",

    detector.GetType().FullName, name, desc);

    m_listPlugin.Items.Add(str);

    }

    The interface I used is defined as

    using System;

    using System.Drawing;

    namespace MotionDetector

    {

    // Event fired by the plugin on detecting motion

    public delegate void MotionEvent(object sender, EventArgs args);

    public interface IMotionDetector

    {

    // Plugin information - Since plugin det

    string GetPluginName();

    string GetPluginDescription();

     

    void SetImage(Bitmap bitmap);

    void Reset();

    event MotionEvent Motion;

    }

    }

    Since I used generics I marked the blog title with C#2.0.

  • I know the answer (it's 42)

    Team Build RSS Feed

    • 17 Comments

    <Update: After some complains that it is difficult to cut paste code out of the blog, I have zipped and placed the source here>
    <Update: Fixed so that you no longer need to copy the Microsoft.TeamFoundation.Build.Common.dll. Thanks to a Watson bug we got to this>

    A lot of programmers have the disease of trying to code everything for themselves. Sometime back I had a bout of the same disease.

    We wanted a tool which would notify the user when a Team Build is fired/completed. Having used bug-tracker tools before which polls bug databases and shows pop-up notifications I immediately got excited and wrote my own super-mutithreaded application called BuildTicker. It could do all the fancy things like track multiple Team Foundation Servers in different threads and show translucent pop-ups like Outlook 2003, had fancy owner-drawn UI. I even made the tool extendable so that I can plugin a bug-tracker for Team Foundation work-item tracking in future. All the while I kind of missed the point that all we need is a way to syndicate the information and rarely there is a way better than using existing technology and standards. JeffLu's source control RSS feed kind of got me to my senses. So here I am with a RSS feed for team build. This kind of retires the fancy BuildTicker I wrote before....

    How to use

    1. Unzip the sources into Team Foundation Server folder as in %Program Files%\Microsoft Team Foundation 2005\Web Services\Build\v1.0
    2. Add people whom you want to be able to see the feed in the Reader group for the team project.
    3. Use a feed reader like RSS Bandit which supports windows authentication. And for the feed url use http://<TFSServer>:<port>/Build/v1.0/rss.aspx?TeamProject=ProjectName replace the part marked in blue with your TFS and project name

    The Source

    <removed: Use the link to the source zip above >

    Note:

    This feed reader does not have the DOS attack blocker as in Jeff's feed reader....

  • I know the answer (it's 42)

    C#: try and retry

    • 17 Comments

    In many situation when something fails (exception is thrown) in the try block you want to retry that again. This could be a network timeout exception, or some other resource unavailability where you expect the same piece of try block code to succeed if you execute it again.

    How you do it in Ruby

    Let us assume there is some networkAccess() function which sometime throws a TimeoutException. In Ruby you can write code as follows.

    begin                  # try in C#
      networkAccess()
    rescue Exception => ex #same as catch (Exception ex) in c#
      retry
      puts "Finally quiting"
    end

    Here the retry keyword makes the preceeding try block (starting with begin in case of Ruby) to be executed again. This code can be further enhanced to create a custom Exception class which has a public attribute which is used to inform the caller whether it can call the failing routine again. In ruby this'd be

    class TimeoutException < Exception
      attr :okToRetry       # public attribute in C#
      def initialize(value) # constructor in c#
        @okToRetry = value
      end
    end
    // simulate a network access failure which succeeds on 3rd attempt
    

    def
    networkAccess() $val += 1 raise TimeoutException.new($val < 3) #throw new TimeoutException end $val = 0 begin networkAccess() rescue TimeoutException => ex #catch (TimeoutException ex) retry if ex.okToRetry puts "Finally quiting" end

    Here the failing callee function networkAccess raises (throws) an exception using a public attribute in the exception class to signal the caller to indicate if it can be called again.

    In C#

    In C# unfortunately (??) there is no support for retry. We can debate whether it'd be useful, but I feel it would make the exception handling more complete and expressive if it did. There are two workarounds. One is using goto :) and other is what we notmally use, by converting the exception to a return value and calling the failed routine again based on that.

    TryLabel:

    try

    {

    downloadMgr.DownLoadFile("file:///server/file", "c:\\file");

    Console.WriteLine("File successfully downloaded");

    }

    catch (NetworkException ex)

    {

    if (ex.OkToRetry)

    goto TryLabel;

    }

    If you compare this with the retry in Ruby the difference in the expressiveness is evident.

    The other options is to create a wrapper around this call and convert the exception to some return code and call that wrapper again based on whether its ok to call the failing function

    public static bool Wrapper(DownloadManager downloadMgr)

    {

    try

    {

    downloadMgr.DownLoadFile("file:///server/file", "c:\\file");

    return true;

    }

    catch (NetworkException ex)

    {

    Console.WriteLine("Failed to download file: {0}", ex.Message);

    return (!ex.OkToRetry);

    }

    }

    static void Main(string[] args)

    {

    while (!Wrapper(downloadMgr)) ;

    }

    Here the wrapper converts the failure in DownloadFile to false return code and the caller goes on calling wrapper as long as return code is false.

  • I know the answer (it's 42)

    C#: Anonymous methods are not closures

    • 26 Comments

    <Edit: See the extended discussion at the end of which I conclude Anonymous methods are indeed lexical closures!!! >

    Anonymous methods in C# are just anonymous methods and do not represent true lexical closure. There are a lot of samples and code out there explaining why. I use a simple example in my mind to remember why it is not the same.

    First lets see how the sample works in a language like Ruby which implements true lexical closure.

    $arr = Array.new

    # function funcGen fills the array arr with generated

    # functions (closures) that print an integer i. i varies

    # from 0 to val (10) when the function is generated

    def funcGen(val)

    0.upto(val) do |i| # same as for(i=0;i<val;i++)

    $arr[i] = Proc.new { # Proc.new creates the closure

    print i # body of the closure

    print ' '

    }

    end

    end

    funcGen(10) # call funcGen to fill the array
    # each function in the array arr is called to print

    # the value of i

    $arr.each do |val|

    val.call

    end

    Definition of closure requires that the lexical state of the closure when it's created is preserved. So all the variables (environment) used by the closure at the timeof  its creation should be captured at a point in time. Since Ruby implementes true closure and it does this the result of running the above code is

    0 1 2 3 4 5 6 7 8 9 10

    If we see the same sample in C# it'll be some thing like

    using System;

    namespace AnonymousMethod

    {

    delegate void Func();

    class Program

    {

    static Func[] funcArr = new Func[10];

    static void fillFunc(int count)

    {

    for (int i = 0; i < count; i++)

    {

    funcArr[i] = delegate()

    {

    Console.Write("{0} ", i);

    };

    }

    }

    static void Main(string[] args)

    {

    fillFunc(funcArr.Length);

    for (int i = 0; i < funcArr.Length; i++)

    {

    funcArr[i]();

    }

    }

    }

    }

    However, even though the same logic as in Ruby is implemented in this C# code the result is totally different and is

    10 10 10 10 10 10 10 10 10 10

    So in this case the value of i when the anonymous method was created is NOT used and the last modified value of i is used. This clearly indicates that lexical environment of all these methods are not closed. In anonymous methods these read-only variables are shared between the outer method and all the other anonymous mehtods. This sample can be tweaked a little to get it to match that of Ruby. The modified sample is as below with all changes marked in bold

    using System;

    namespace AnonymousMethod

    {

    delegate void Func();

    class Program

    {

    static Func[] funcArr = new Func[10];

    static void fillFunc(int count)

    {

    for (int i = 0; i < count; i++)

    {

    int j = i;

    funcArr[i] = delegate()

    {

    Console.Write("{0} ", j);

    };

    }

    }

    static void Main(string[] args)

    {

    fillFunc(funcArr.Length);

    for (int i = 0; i < funcArr.Length; i++)

    {

    funcArr[i]();

    }

    }

    }

    }

    With this change in code the output will get changed and will be the same as the Ruby program (0 1 2 3 4 5 6 7 8 9). The difference in the result further strengthens the fact that true closure is not implemented by C#. Inspection of the generated assembly with Reflector clearly shows why...

    In either case to encapsulate the anoymous method and the variable i it referes to the compiler generates a class

    [CompilerGenerated]

    private sealed class <>c__DisplayClass2

    {

    public <>c__DisplayClass2();

    public void <fillFunc>b__0()

    {

    Console.Write("{0} ", this.i);

    }

    public int i;

    }

    The method in bold is the anonymous method.

    In the first case while parsing the fillFunc method the compiler sees the following code

    for (int i = 0; i < count; i++)

    {

    funcArr[i] = delegate()

    {

    Console.Write("{0} ", i);

    };

    }

    and figures out that a variable i from the outer scope is used by the anonymous method and the compiler emits the following code

    private static void fillFunc(int count)

    {

    Func func1 = null;

    Program.<>c__DisplayClass2 class1 = new Program.<>c__DisplayClass2();

    class1.i = 0;

    while (class1.i < count)

    {

    if (func1 == null)

    {

    func1 = new Func(class1.<fillFunc>b__0);

    }

    Program.funcArr[class1.i] = func1;

    class1.i++;

    }

    }

    It is obvious from this code that only one object class1 of the generated class is created and is shared between all the anonymous methods and the loop generating the methods. So if anyone of the methods are called later, the last modified value of i (= 10) will be returned.

    For the second case on seeing

    static void fillFunc(int count)

    {

    for (int i = 0; i < count; i++)

    {

    int j = i;

    funcArr[i] = delegate()

    {

    Console.Write("{0} ", j);

    };

    }

    }

    The compiler generates the following code

    private static void fillFunc(int count)

    {

    for (int num1 = 0; num1 < count; num1++)

    {

    Program.<>c__DisplayClass1 class1 = new Program.<>c__DisplayClass1();

    class1.j = num1;

    Program.funcArr[num1] = new Func(class1.<fillFunc>b__0);

    }

    }

    Here the class used to encapsulate the anonymous method is very similiar to the one used in the first case. However, since the method uses a variable inside the loop, for each iteration a new object is created. This results is each anonymous methods having its own copy of the object and hence the value of j in it is same as it was at the time of the method creation.

  • I know the answer (it's 42)

    C#: Object cloning is a bit confusing now

    • 18 Comments

    Some developers believe that object cloning (creating new object which are copies of the original) is easier in C# than in other languages like C++. However, I do not agree with this. Somewhere I had read that a good language is one that places more hardship on framework (class) designers and less on client developers. This is where we find the principle getting violated.

    C++

    Lets consider a class that uses dynamically allocated memory and needs deep-copy when it is cloned. In the C++ world the developer of the class had to know things like copy-constructor and = operator overloading (and when they get called) to ensure that this happens correctly. A well-behaved class would look like

    class MyClass
    {
    public:
    MyClass()
    // ctor

    {

    numListLen = 10;
    numList =
    new int[numListLen];
    }
    MyClass(
    const MyClass& mc) // copy ctor
    {
    numListLen = mc.numListLen;
    numList =
    new int[numListLen];
    for(int i = 0; i < numListLen; i++)
    numList[i] = mc.numList[i];
    }
    MyClass&
    operator=(const MyClass& mc)
    {
    if(this != &mc) // take care of mc = mc assignement
    {
    delete[] numList;
    numList =
    new int[numListLen];
    for(int i = 0; i < numListLen; i++)
    numList[i] = mc.numList[i];
    }
    return *this; // take care of mc1 = mc2 = mc3
    }
    virtual ~MyClass()
    {
    delete []numList;
    }
    int* numList;
    int numListLen;
    };

    All this code just to ensure that copy happens as planned. So people call it complex. However, the client uses this class as.

    MyClass mc1, mc2; // class Ctor
    mc2 = mc1; // calls = operator overload
    MyClass mc3 = mc1; // calls copy-ctor

    mc1.numList[2] = 2;// mc2, mc3 are left untouched

    So the client programmer simply need not care about shallow/deep copy. He assigns and creates object and all of the deep-copy stuff are handled under the hood.

    C#

    On C# you cannot overload the assignment operator and copy-ctor is not supported. The object cloning is supported using the ICloneable interface. Consider the following code that implements this interface

    class MyClass : ICloneable
    {
    public MyClass()
    {
    deepRef =
    new AnotherClass();
    }
    public object Clone() // ICloneable implementation
    {
    MyClass mc = this.MemberwiseClone() as MyClass;
    mc.deepRef =
    this.deepRef.Clone() as AnotherClass;
    return mc;
    }
    public AnotherClass deepRef;
    };

    The code looks much simpler. However the catch is that the client writer needs to ensure that he never creates an object copy by direct assignment. So if he does the following there goes a bug into his code

    MyClass mc1 = new MyClass();
    MyClass mc2 = mc1; // Bug - shallow copy made

    mc1.deepRef.x = 10;// and mc2.deepRef gets modified

    So the client needs to call MyClass mc2 = mc1.Clone() as MyClass; to ensure deep copy. Since humans do forget bugs like this creep in.

    Another issue is that ICloneable came in the pre-generic (read pre-historic) era. So its not type safe and you need ugly type-casting all over the code.

    Ruby

    ruby which was once a upon a time my favorite language sucks big time in deep-copy. You need to do something as below to get a deep copy

    class Object
       def deepcopy
         Marshal::load(Marshal.dump(self))
       end
     end
  • I know the answer (it's 42)

    C# 2.0: Using different versions of the same dll in one application

    • 31 Comments

    Lot of things have become really easy to do in C#2.0. I needed to load 2 versions of the same class library dll in my application. The format of output file had changed between versions. So I need to deserialize an xml using the older version and then convert it to the newer version and then serialize it out.  So at the same time I needed to load both the old and new dlls. Lets assume that you have the following implementation of the class libraries

    Older version

    using System;

    namespace MyLibrary

    {

    public class MyClass

    {

    public static string method()

    {

    return "From old version ClassLibrary2";

    }

    }

    }

    Newer version

    using System;

    namespace MyLibrary

    {

    public class MyClass

    {

    public static string method()

    {

    return "From new version ClassLibrary2";

    }

    }

    }

    The structure of both libraries are exactly the same and they have the same namespace and classes and only differ in implementation. If I add reference to both the class library dlls and try to access the methods in the client using the following code

    using System;

    using System.Text;

    namespace ClientApp

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.WriteLine(MyLibrary.MyClass.method());

    }

    }

    }

    Compilation will fail with the error "The type 'MyLibrary.MyClass' exists in both 'Somepath\ClassLibrary.dll' and 'AnotherPath\ClassLibrary.dll'". The reason being that the access becomes ambiguous. 

    This is where the new namespace alias qualifier and the extern alias comes into the picture. First we write the client application (program.cs) as

    extern alias oldVer;

    extern alias newVer;

    using System;

    using System.Text;

    namespace ClientApp

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.WriteLine(oldVer::MyLibrary.MyClass.method());

    Console.WriteLine(newVer::MyLibrary.MyClass.method());

    }

    }

    }

    In the code-snippet above all the important bits are marked in bold. The extern alias declaration creates a namespace parallel to the global namespace. Pre C#2.0 all namespaces were rooted at the global namespace, which is no more the case. We are telling the compiler that two other namespace oldVer and newVer exists which are externally defined. We now use the :: operator to access inside the namespace alias. We could have used the regular . qualifier but the :: qualifier ensures that the identifier on the left-hand of the :: qualifier is an extern or using alias.

    The next step is to define the external alias. We can do it using the command line as follows

    csc /r:oldVer=Somepath\ClassLibrary.dll /r:newVer=AnotherPath\ClassLibrary.dll program.cs

    So the contents of the older version is loaded under the oldVer alias and the new version under newVer. So all access ambiguity is removed.

    The same thing can also be acheived using the VS IDE. Add the reference to both the dlls in your client application solution. Then in the Solution Explorer under the reference node select the first (old version) class library. In the property window change Aliases field from global to oldVer. Make similar change after selecting the newer class library as well. You are then good to go....

  • I know the answer (it's 42)

    optional arguments in C#

    • 20 Comments

    <Note: Optional arguments are now available as of C# 2010 /> 

    One of the things I missed a lot when I moved to C# is optional arguments. In C++ optional arguments are used a lot. Code as below is a common sight.

    void foo(int reqdParam, int optParam = 0)

    {

    // ...

    }

     

    foo(5); // gets compiled as foo(5,0)

    foo(5, 10);

    The reason it is not included in C# is mainly due to versioning problem. Optional arguments are handled in most programming languages by inserting the default value of the optional argument at the call site. So for the above code foo(5) is compiled as foo(5, 0).

    The versioning issue comes to play if the call site and the method are in different assemblies. In the next version of the method the default value of optParam may change from 0 to 1 and 0 can become an unsupported value. However the calling code will still contain 0 and hence we may get a run-time issue. The way to get around is re-compiling all the assemblies that contains calls to the method and this simply does not scale.

    Another way of handling optional argument would be to automatically generate method overloads based on optional arguments. So the above code on compilation would yield something like

    void foo(int reqdParam)

    {

    foo(reqdParam, 0)

    }

    void foo(int reqdParam, int optParam)

    {

    // ...

    }

     

    foo(5); // calls the first overload

    foo(5, 10); // calls the actual function

    This is versioning safe. However this is not used by most languages including C#. I do not know why this is not used. This is versioning safe and at the same time gives all the benefits of optional arguments. Side effects would be code-bloat, inclusion of these methods in the call-stack.

  • I know the answer (it's 42)

    What’s this .NET Compact Framework thingy?

    • 13 Comments
    Bidar - On the way back to hyderabad

    If you are following the latest Windows Phone stories than you have surely heard about the .NET Compact Framework (NETCF) which is the core managed runtime used by the Windows Phone 7 programming model. This is an implementation of the .NET specification that works on devices and a disparate combination of HW and SW.

    image

    In case you are hearing about NETCF for the first time, then hopefully this post will help you to understand it’s architecture and how it is different from the desktop .NET.

    The Architecture

    At the heart of NETCF is the execution engine. This contains the usual suspects including the JIT, GC, Loader and other service providers (e.g. Marshalling that marshals native objects to managed and vice versa, type system).

    The entire Virtual machine is coded against a platform agnostics Platform Abstraction Layer or the PAL. In order to target a platform like say Nokia S60 we implemented the PAL for that platform. Stringent policy of not taking any platform dependency outside PAL and ensuring in PAL we only use features that are commonly available on most modern embedded OS allows NETCF to be highly portable. Currently the PAL supports a variety of OS and processor architecture. The ones in Red are those that are used for Silverlight, the others are available for the legacy .NETCF 3.5.

    The other system dependent part is the JITter which compiles the MSIL into the platform dependent instructions. For every supported processor architecture there’s a separate implementation of JIT.

    This entire runtime is driven by a host. E.g. for Silverlight on S60 it’s the Silverlight host running as a plugin inside the Nokia browser, on Windows Phone it’s the Windows Phone task host. These hosts uses the runtime services to run managed code. The host interacts with the runtime over the hosting interfaces.

    Managed code can either be the framework managed code like BCL and other Silverlight/XNA framework managed code or the user code that is downloaded from the web/application-store. Even though the framework managed code can interact with the underlying system including the execution-engine and the OS (via Platform-Invoke) the user managed code is carefully sandboxed. If you see the arrows moving out of the user managed code it is evident that user managed code can call only into the framework managed code (which in turn can call into the system post security verification). User code is not allowed to access any native resources (including P/Invoke).

    The host knows about the UI and it’s rendering and uses reverse Invoke to call into the managed code. E.g. if a user clicks on a XAML button the Silverlight host using the XAML object tree and rendering logic (hit-testing) figures out which object got clicked on and it uses reverse-pinvoke to call into the corresponding managed objects (which provides the event handler). That managed code is then verified, security checked, jitted and run by NETCF.

    Portability

    NETCF is highly portable. As far as I know it’s one of the very few (or is it the only one :)) runtime shipping out of Microsoft that supports Big-Endian processor architecture (Xbox360 uses Big-Endian processor). It is designed for resource constrained devices and is battery friendly. For many time-space trade-offs it tilts towards conserving space (e.g. Code-pitching which I’ll cover later) and at the same time works well on Xbox360 running high-end games. The list of processor/OS I have given above is just illustrative and it actually works or there are POC (proof-of-concept) of it working on really esoteric SW+HW combinations.

    Some CF Facts

    1. Design Goals
      1. Keep the runtime highly portable, that is the main NETCF USP
      2. Designed for resource constrained devices
      3. Battery friendly (use less power)
    2. Nothing comes for free. The above design results in some unique NETCF features (more about that later) and some limitations as a consequence.
    3. Some of the well known platforms powered by NETCF runtime is Windows Mobile 6.5 and below, Windows Phone 7, XNA (on Xbox 360), Zune, Media Room (pdf), Silverlight on Nokia S60.
    4. Currently shipping versions are 3.7 on Windows Phone and 3.5 elsewhere.
    5. Windows Phone runs on both ARM (the real phone devices) and x86 when running on the emulator (which is a x86 VM). NETCF becomes a natural choice because it runs on both these processors.
  • I know the answer (it's 42)

    C#: Fast string comparison optimization

    • 23 Comments

    Sometime back I had blogged about using reference comparison to speed up ordinal string comparisons. Essentially this means that if you know for sure that your string is interned and you want to do an ordinal string comparison you can use the following optimization

    if (command == "START")

    Console.WriteLine("Starting Build...");

    else if (command == "STOP")

    Console.WriteLine("Stopping Build...");

     

    // Optimization

    if (Object.ReferenceEquals(command,"START"))

    Console.WriteLine("Starting Build...");

    else if (Object.ReferenceEquals(command, "STOP"))

    Console.WriteLine("Stopping Build...");

    This works only if the string command is an interned string. If its not (e.g. its accepted from command lines or is constructed) you can intern it using something like string internedCmd = string.IsInterned(command) ?? string.Intern(command); and then use the interned version of the same string.

    However an email to the internal CSharp alias made me look again. The email stated that the static method String.Equals(a,b) is more efficient than instance method String.Equals(a). I looked in into the code and it appears that for static Equals we already do the reference comparison first, while for instance Equals we jump right to string comparison.

    // Static Equals called from overloaded operator ==
    public static bool Equals(string a, string b) { if ((object)a == (object)b) { return true; } if ((a != null) && (b != null)) { return string.EqualsHelper(a, b); } return false; }

    This might get fixed in future .NET version when both Equals will do the reference comparison first. However, the optimization I suggested does not hold because its already been done in the framework right now.

    So currently in the following snippet the second version is faster than the first

    string a = ...;
    string b = ...;




    a.Equals(b);
    string.Equals(a, b); // faster
  • I know the answer (it's 42)

    C# 2.0: Generics, default(T) and compare to null weirdness

    • 25 Comments

    <Added additional stuff after a discussion on internal CSharp user list>

    I was going through the generics spec in C#2.0 and found something really weird.

    default(t)

    Consider the following code.

    class MyGenClass<T>

    {

    public void Method(ref T t)

    {

    t = null;

    }

    }

    This will not compile because because T may be a value type as well and there is no implicit conversion of null to a value-type. So to handle such situation where you want to reset a type in a generic class the default-value expression was instroduced.

    class MyGenClass<T>

    {

    public void Method(T t)

    {

    t = default(T);

    }

    }

    If the type in default-value expression at run-time evaluates to be a reference type then it'll return null, if its a value-type then it will return the value-types default value (essentially new T() which boils down to re-setting all bits to 0).

    null comparison weirdness

    Peculiarly though, you are allowed to compare t with null.

    class MyGenClass<T>

    {

    public void Method(T t)

    {

    if (t == null)

    Console.WriteLine("null");

    // do some processing*/

    }

    }

    I got a bit confused here, as I always thought that you cannot compare value-types to null. Then I tried the following code.

    int i = 5;

    if (i == null)

    Console.WriteLine("null");

    and it compiled fine, with a warning that i == null will always be false. But the weird thing is that it compiled and I just cannot figure out why does the compiler allow comparing a value type with null. However, I accepted it and thought I figured out that since value-type can be compared to null it explains why in the generic class I was able to compare t to null. Things got even weirder when I tried to put a value-type constraint on the generic class

    class MyGenClass<T> where T: struct

    {

    public void Method(T t)

    {

    if (t == null)

    Console.WriteLine("null");

    // do some processing*/

    }

    }

    Compilation failed, with the statement that == operator cannot be applied to T. Since compiler cannot validate that == operator is overloaded on T it fails. But what I cannot figure out is that even in the case where there were no constrains the compiler in no way can validate the same thing, then why did it allow the comparison with null.

    Time to send an email to internal CSharp alias to figureout whats going on....


    I did finally send the email to CSharp usergroup and after some discussions this is what came out.

    The comparison of the form int a = 5; if (a==null) was invalid in VS 2003, but this has changed with nullable types. The int gets converted to a nullable type and the lifted operator bool operator ==(int? x, int? y); is used (see 2.0 spec 24.3.1) .

    This answers why null comparison works in value types like int. Why null comparison is allowed in generic classes has a more involved reasoning. First of all there is an explicit rule in the spec which goes as

    There are special rules for determining when a reference type equality operator is applicable. For an equality-expression with operands of type A and B, […] if B is the null type, […] A is a type parameter and at runtime, the type parameter is a value type, the result of the comparison is false.

    This rule makes the comparison valid. However, I was not convinced about the reasoning behind the rule. Special cases like this break the flow and intuition one develops in a language. Moreover, it the above rule is valid then I'd argue the following code should also be valid

    class MyGenClass<T> where T: struct

    {

    public void Method(T t)

    {

    if (t == null)

    Console.WriteLine("null");

    // do some processing*/

    }

    }

    Here for the comparison t == null, T should have been auto-converted to its nullable counterpart T? and the comparison should have gone through (evaluated to false). I think the fact that a generic class with no constraint allows an expression and a generic class with additional constraint do not allow the same is un-intuitive.

     

  • I know the answer (it's 42)

    Team Build Ticker: parting gift

    • 21 Comments

    I had blogged earlier about the BuildTicker tool which sits on the system tray and notifies of builds happening on the server. I have received many requests to get this tool. So here it goes. Get the executable from here and the sources from here. Since I am no longer working on the TFS team you can consider this as my parting gift :)

    Disclaimer

    1. This tool was never formally tested. However it is being used by me everyday.
    2. It comes with no support. In case you hit issues report it in this blog and I'll try to resolve it here.

    Features

    1. Does not need TF client to run
    2. Runs from System Tray
    3. Can monitor multiple TF servers and Build Types
    4. Shows pop-ups to notify of builds
    5. Tray animation to indicate successful/failed builds
    6. Integrated to the TF web-ui and you can click it to bring up a Team Build web-report
    7. Auto start(after restarts)

    Known Issues

    1. First time you run it, it starts silently in the tray without bringing up the configure dialog.
    2. The pop-up takes away focus (can be majorly irritating when you are typing in some window). However the good news is that after the pop-up closes in a second or two the focus is restored.
    3. The application needs to be restarted sometimes when you change the TF and Build Types it is supposed to monitor
    4. Doesn't maintain the list of builds it has captured till date.

    How to use

    1. Unzip to a folder and run the executable
    2. Right click on the Build Ticker icon in the tray and choose Open Build Ticker
    3. Choose Server and enter your server and  build type details
    4. Close the dialog. Sometimes it might be required to restart the application at this point
    5. Start getting notifications

    However, I'd recommend that you use the Rss Feed generator and point a windows authentication capable feed-reader like RssBandit to it.

  • I know the answer (it's 42)

    C#: Comparison operator overloading and spaceship operator

    • 15 Comments

    Lets consider I have a class Employee which has  Name and JobGrade fields. I want to overload the comparison operators for this class so that it can participate in all types of comparison including <. <=, ==, >=, != and Equals. I want to translate the comparison in-between two Employee objects to be a comparison between the JobGrade . Since I do not want to write the comparison logic each time, typically I'd implement the comparison in one method and from comparison operator overloading methods call this common method. So in C# I'd do something like.

    class Employee

    {

    public Employee(string name, int jobGrade){

    Name = name;

    JobGrade = jobGrade;

    }

    public string Name;

    public int JobGrade;

     

    public static bool operator <(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) < 0;

    }

    public static bool operator >(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) > 0;

    }

    public static bool operator ==(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) == 0;

    }

    public static bool operator !=(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) != 0;

    }

    public override bool Equals(object obj){

    if (!(obj is Employee)) return false;

    return this == (Employee)obj;

    }

    public static bool operator <=(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) <= 0;

    }

    public static bool operator >=(Employee emp1, Employee emp2){

    return Comparison(emp1, emp2) >= 0;

    }

    public static int Comparison(Employee emp1, Employee emp2){

    if (emp1.JobGrade < emp2.JobGrade)

    return -1;

    else if (emp1.JobGrade == emp2.JobGrade)

    return 0;

    else if (emp1.JobGrade > emp2.JobGrade)

    return 1;

    return 0;

    }

    }

    This is kind of huge as I have to overload each comparison operator individually even though what I want is to just make Employee object comparable to any other Employee object. What happens is as follows

    1. public static bool operator <=(Employee emp1, Employee emp2) gets compiled to a method named bool op_LessThanOrEqual(Employee, Employee). Similiar naming convention is used for the other operators.
    2. On seeing the code emp1 <= emp2 the compiler emits code to call the method op_LessThanOrEqual

    This makes overloading operators individually a requirement.

    What if

    In some languages like Ruby and also Perl (I am not that sure on perl) there is a concept of space ship operator <=>. This operator must return less than 0, equal to 0, greater than 0 based on whether the expression on the left is less than, equal-to or greater than that on the right (similiar to IComparable:CompareTo). If C# compiler supports the concept of the space-ship operator then we can simply overload this one operator and expect the compiler to emit code to call this operator for all comparison. So if C# supports this then the above code would look like

    class Employee

    {

    public Employee(string name, int jobGrade){

    Name = name;

    JobGrade = jobGrade;

    }

    public string Name;

    public int JobGrade;

     

    public override bool Equals(object obj){

    if (!(obj is Employee)) return false;

    return this == (Employee)obj;

    }

    // same in the lines of IComparable:CompareTo

    public static int operator <=>(Employee emp1, Employee emp2){

    if (emp1.JobGrade < emp2.JobGrade)

    return -1;

    else if (emp1.JobGrade == emp2.JobGrade)

    return 0;

    else if (emp1.JobGrade > emp2.JobGrade)

    return 1;

    return 0;

    }

    }

    In this case what'll happen is as follows

    1. public static int operator <=>(Employee emp1, Employee emp2) gets compiled to bool op_SpaceShip(Employee, Employee)
    2. On seeing emp1 <= emp2 the compiler emits code to call the method op_SpaceShip(emp1, emp2) <= 0
    3. On seeing emp1 != emp2 the compiler emits code to call the method op_SpaceShip(emp1, emp2) != 0
    4. Or generically emp1 op emp2 gets compiled to op_SpaceShip(emp1, emp2) op 0

    Interfaces like IComparable already exists which needs the class to implement CompareTo which works exactly like <=> operator overloaded as above. If only the compiler directly made calls to this then the need for one to implement this interface and then make calls to this method gets removed.

    The overloaded <=> acts as a filler. In case <= and => are already overloaded for a class then calls are made to these methods and for operators not overloaded like == and != calls are made to <=>.

    Its not important (atleast to me) how we achieve this and can include ways like have a method CompareTo in Object in the same lines of Equals and make the compiler emits calls to it based on the operator getting used or use explicite <=> operator overloading. Either way the need to overload all 5 operators should be eliminated.

  • I know the answer (it's 42)

    using UIAutomation

    • 23 Comments

    With Beta 2 most developers inside Microsoft has started playing with Vista. Among a lot of uber-cool features one of the interesting feature is UIAutomation. This is the next-gen assistive/UI-Automation technology that succeeds Active Accessibility (MSAA).

    I tried it out a bit and I liked its power and reach. However, the programming model is very unconventional and un-intuitive. I really hope that the folks have done enough user-study to understand if its understandable by average developers.

    Lets take a look.

    Why UIA

    I don't have the official statement but my guess would be that MSAA failed to keep up with the expectations. MSAA served its purpose well. However it was designed for assistive purposes to help out visually impaired people or people with other disabilities. Soon everyone started using it to drive/automate UI and ran into all sorts of limitations. The fact that it was not documented well added to the confusion and let to lot of incorrectly implemented piece of UI.

    UIA was designed grounds up to serve both automation and assistive needs.

    What is UIA

    Its a layer over all MS UI technologies and provides a single interface to automate all kinds of user interface. You can potentially write generic code to automate an application and run the same bits to drive an application written in Avalon (WPF), WinForm, ASP.NET (inside IE), Win32 etc. WPF controls implement UIA natively and all others implement MSAA and UIA reads into all of them and provides a seamless experience.

    UIA has a plugin provider model where you can register client-side providers which acts as bridges and allows UIA to read into any assistive technology out there. So potentially you can create a bridge that'd allows UIA to read into Java Accessible applications. Currently it works with WPF, Win32, MSAA, Office controls. Since a lot of frameworks like Flash supports MSAA, it'd should work with UIA seamlessly.

    Features

    Multiple features make UIA very interesting. Specially the fact that it represents the whole desktop as a singly rooted tree. Its not relevant which technology is used to implement an UI, everything is expressed in terms of an AutomationElement in the desktop tree. So Visual Studio which is a native Win32 IDE comes under the desktop node and all the managed pieces inside VS appear as its children. Using crossbow if you host XAML (Avalon) inside a WinForm control it'll appear seamlessly in that tree. All the background work of converting technology specific details are done by UIA.

    UIA also provides way to search the tree to locate nodes based on search criteria. It has it own query mechanism.

    UIA supports notification of user actions using Events similar to the MSAA WinEvents.

    Code

    The primary step of automating UI is to locate specific controls that match a search criteria and then drive it. We'll first define three overloads of a function named GetElement that locates a control based on criteria's passed to it

    //Search for a elment just by name starting from root
    private static AutomationElement GetElement(AutomationElement root, string name, bool recursive)
    {
    PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name);
    return root.FindFirst(recursive ? TreeScope.Descendants : TreeScope.Children, condName);
    }
    //Search for the first occurance of a given type of control
    private static AutomationElement GetElement(AutomationElement root, ControlType controlType)
    {
    PropertyCondition condType = new PropertyCondition(AutomationElement.ControlTypeProperty, controlType);
    return root.FindFirst(TreeScope.Descendants, condType);
    }

    So the first one gets me any control with a given name (as in label, text) and the second gives me a control of a given type like ControlType.Button

    Even though I don't use it here, its trivial to combine multiple conditions. So in case I wanted to use a condition that combines both of the above I would have done

    AndCondition andCond = new AndCondition(condName, condType);

    Another interesting feature in UIA is that each of the AutomationElements supports patterns. The supported patterns vary from control to control and can be used to drive them. So a button has a invoke pattern which is equivalent to clicking it and edit boxes have a Value pattern that can be used to set values in them. So the following clicks on a button and sets the value of a edit box

    AutomationElement btn = GetElement(...);

    InvokePattern invPattern = btn.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;

    invPattern.Invoke();

    AutomationElement edit = GetElement(...);

    ValuePattern valPattern = edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

    valPattern.SetValue("abc"); 

    So with all of the above I can locate any button in say a calculator window and drive it. We can use the following code to drive calculator to do the calculation 7 * 7 - 7 = 42. At the end we'll also verify that the result in the calculator indeed matched the expected the most important answer 42. The code expects to have calculator already running.

    static void Main(string[] args)
    {
    // Locate calculator window
    AutomationElement calculator = GetElement(AutomationElement.RootElement, "Calculator", false);
    // locate the button 7
    AutomationElement btn7 = GetElement(calculator, "7", true);
    InvokePattern btn7InvPat = btn7.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    btn7InvPat.Invoke();
    // locate the button *
    AutomationElement btnMult = GetElement(calculator, "*", true);
    InvokePattern btnMultInvPat = btnMult.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    btnMultInvPat.Invoke();
    // hit on 7 again
    btn7InvPat.Invoke();
    // locate and invoke -
    AutomationElement btnMinus = GetElement(calculator, "-", true);
    InvokePattern btnMinusInvPat = btnMinus.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    btnMinusInvPat.Invoke();
    // hit on 7 again
    btn7InvPat.Invoke();
    // locate and invoke =
    AutomationElement btnEq = GetElement(calculator, "=", true);
    InvokePattern btnEqInvPat = btnEq.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    btnEqInvPat.Invoke();
    // get the result edit box and verify the value is indeed 42
    AutomationElement editResult = GetElement(calculator, ControlType.Edit);
    string result = editResult.GetCurrentPropertyValue(ValuePattern.ValueProperty, false).ToString();
    Debug.Assert(result != "42.", result);
    }

    Code Quality

    The UIA code looks very ugly and is not strongly typed. With generics in place there is no excuse for writing a framework that requires casting almost every other line. I think I'll use another post to discuss why I do not like the code.

  • I know the answer (it's 42)

    C#: I miss case fall through

    • 31 Comments

    We all have heard time and again that the default case fall through is not good (as in C/C++) and C# does not allow it. There has been enough debate on this but I still differ on this. I used to like it a lot and somehow when I do need to use it the following puts me off big time!!!

    switch(value)

    {

    case 0:

    Console.WriteLine("In case 0\n");

    goto case 1;

    case 1:

    Console.WriteLine("In case 1\n");

    goto case 2;

    case 2:

    Console.WriteLine("In case 2\n");

    break;

    }

    I kind of do not buy the statement that case-statement fall through results in issues in the maintainence phase. Its pointed out that fall-through can lead to the following

    1. Developer forgets to add the break at the end and un-intentional fall-through may happen
    2. Case statements cannot be easily reordered as the fall-through execution may vary...

    With C lying around for 30 years and C++ over 20 years most developers have got used to adding break at the end of case. In case C# wanted to do something for new developers or for developers moving in from languages like VB, a warning could have been added in case break is missing, asking the developer if fall-through is the intent. This is very similiar to the warning given for the following code

    bool hello;

    // lots of code....

    if (hello = true) // this is why some people use true == hello

       Console.WriteLine(arg);

    Warning 1 Assignment in conditional expression is always constant; did you mean to use == instead of = ?

    What I also do not understand is the requirement for the break statement at the end of each case in C# even though it does not support fall-through.

    switch (arg)

    {

    case "0":

    Console.WriteLine(0);

    //break;

    case "1":

    Console.WriteLine(1);

    break;

    }

    The above code fails to compile and you need to uncomment the //break to get it to compile. The error is "Control cannot fall through from one case label ('case "0":') to another" Since the language clearly states that fall-through do not happen then why do I need to add the extra break, it has no purpose and I should not be required to add it. This looks like to have been added to aid C/C++ developers to understand that fall through does not happen. I feel that this error should have been the warning I had mentioned above....

    There are a lot of interesting or weird uses of fall-through and the fact that switch can be interlaced with statement blocks (thats in the next post...).

  • I know the answer (it's 42)

    C#: structs and Interface

    • 18 Comments

    The fact that a struct can implement an interface is well known and so is the fact that casting a value type into an interface leads to boxing of the value type. This is because methods in interfaces are defined as virtual and to resolve virtual references, vtable (method table) look up is required. Since value types do not have pointers to vtable they are first boxed into a reference type and then the look up happens.

    This boxing leads to some performance penalty. See Rico Mariani's performance quiz for an example.

    The fact that such boxing takes place on casting to interfaces can lead to subtle issues in code. Consider the following

    interface IPromotion {

    void promote();

    }

    struct Employee : IPromotion {

    public string Name;

    public int JobGrade;

    public void promote() {

    JobGrade++;

    }

    public Employee(string name, int jobGrade) {

    this.Name = name;

    this.JobGrade = jobGrade;

    }

    public override string ToString() {

    return string.Format("{0} ({1})", Name, JobGrade);

    }

    }

    class Program

    {

    static void Main(string[] args)

    {

    Employee employee = new Employee("Cool Guy", 65);

    IPromotion p = employee;

    Console.WriteLine(employee);

    p.promote();

    Console.WriteLine(employee);

    }

    }

    Here the output would be
    Cool Guy (65)
    Cool Guy (65)

    So even after calling p.promote() the value of JobGrade in employee does not increase. The reason is that on implicitly casting employee to IPromotion p a temporary boxed object is created and p.promote updates the JobGrade in this temporary object and not on original employee value type. Since after this usage the temporary boxed object is not refered to, the update to it is lost and it is garbage collected.

    If however we change the code and make Employee a class the output would become as expected

    Cool Guy (65)
    Cool Guy (66)

    The reason being that now the boxing does not take place and the update happens on the original employee object.

  • I know the answer (it's 42)

    C# 3.0: I like Extension Methods

    • 24 Comments

    After the declaration of C# 3.0 I went ahead and installed the PDC bits. After reading through the language spec. I was very very very unhappy. I mean we were just getting excited over C#2.0 supporting generics, anonymous mehtods, Nullable types and then suddenly someone ruins all the fun by showing us C#3.0. C#2.0 already appears stale. Some great blogs on this are from Cyrus and Matt Warren

    The good part is that since I work in Microsoft, soon (after VS 2005 release I guess) we'd move to C#3.0 to dogfood it. So that means I'll not be required to wait for the super enhancements in C# for long!!!!

    I thought I'd try out the features of C#3.0 and write some stuff on it. Here goes the second one (the first is already there)

    Extension Methods

    With extension methods you can attach additional functionalities to an existing type even if you do not have access to it. For example I can write an  extension method Print() which prints each element of any collection on a different line and then invoke it on any collection such as

    List<string> first = new List<string>();

    first.AddRange (new string[] {"Hello", "how"});

    first.Print();

    Importantly note is that even though Print() is not a member of List<> but is still called as if its a method. There are a couple of restrictions in defining the extension method Print. All of which are marked in bold in the example below

    public static class Extensions

    {

    public static void Print<T>(this ICollection<T> col)

    {

    foreach(T t in col)

    Console.WriteLine(t);

    }

    }

    static void Main(string[] args)

    {

    List<string> first = new List<string>();

    first.AddRange (new string[] {"Hello", "how"});

    first.Print();

    }

    The method has to be a static method in a static class. The first parameter to the method has to be qualified using this and this first parameter indicates the types on which this extension method can be applied. Its important to note that this is just syntactic sugar and both the following calls are equivalent and legal.

    first.Print();

    Extensions.Print(first);

    So whats the benefit? I think this makes code more elegant and this can be used very much like C++ STL algorithms. Lets say I write a merge algorithm that merges two collections and then I'd be able to call this merge algorithm on any collection in a very elegant way. Following is the implementation of a merge algorithm

    using System;
    using
    System.Collections.Generic;
    // Will compile only with C#3.0 complilers
    namespace
    ExtensionMethodDemo
    {
        public static class
    Extensions
        {
            public static void Merge<T>(this ICollection
    <T> first, ICollection<T> second)
            {
                foreach(T t in
    second)
                    first.Add(t);
            }
       
    }

        class Program
        {
            static void Main(string
    [] args)
           
                List<string> first = new List<string
    >();
                first.AddRange (
    new string[] {"Hello", "how"
    });

                List<string> second = new List<string
    > ();
                second.AddRange (
    new string[] {"are", "you", "doing"
    });

                first.Merge(second);
            }
        }
    }

    Moreover the class libarary already ships with some standard extension methods (like standard algorithms of STL) and you can directly use them. Consider the following

    int[] a = new int[] {1, 2, 2, 4, 3};

    Console.WriteLine(a.Distinct().Count());

    Here two of the methods Distinct and Count are used and the combined result is that we get the number of distinct elements in the array and that is 4. This is really really cool.

    As a real life sample I wrote a pretty print extension method that prints the directory listing is a pretty fashion.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Query;
    using System.IO;

    namespace ExtDemo
    {
       
        public static class Extensions
        {
            public
    static void PrettyPrint(this
    IEnumerable<FileInfo> fInfo)
            {
                ConsoleColor defColor = Console.ForegroundColor;
                string format = "{0, -17} {1,10} {2}";
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(format,
    "Date", "Size (kb)", "File name");
                Console.ForegroundColor = defColor;
                foreach(FileInfo file in fInfo)
                Console.WriteLine(format, file.CreationTime.ToString(
    "dd/MM/yyyy hh:mm"), (float)file.Length / 1024, file.Name);
            }
        }
        class Program
        {
            static void Main(string[] args)
           
                DirectoryInfo
    dirInfo = new DirectoryInfo(@"c:\");
                dirInfo.GetFiles().PrettyPrint();
            }
        }
    }

    The C# 3.0 spec clearly calls out that since this is not a very discoverable feature and is somewhat misleading (very much like operator overloading) and so should be used sparingly. I think that this'll be more and more used by class libraries to implement STL like algorithms that can be run on containers.

  • I know the answer (it's 42)

    Animation and Text in System tray using C#

    • 13 Comments

    Application running from the system tray is very common these days. Mostly these applications show an icon in the system tray (beside the clock) and almost always have a pop-up context menu.

    Adding additional features like showing text in the tray or show animations is also possible with little code. In this post I have discussed a class I wrote to accomplish this. If you are interested in using this enhanced SysTray class and do not care much about how it works jump to the last section Using SysTray.

    First things first: How do you show an icon in the system tray

    With .NET doing this is pretty simple and there is no need to play around with NOTIFYICONDATA and Shell_NotifyIcon of the Win32 world.

    You just need to create a instance of  System.Windows.Forms.NotifyIcon and fill out the relevant fields as in

    private NotifyIcon m_notifyIcon;

    m_notifyIcon = new NotifyIcon();

    m_notifyIcon.Text = text; // tooltip text show over tray icon

    m_notifyIcon.Visible = true;

    m_notifyIcon.Icon = icon; // icon in the tray

    m_notifyIcon.ContextMenu = menu; // context menu

    That’s it and the icon appear in the system tray and on right clicking on it the menu is popped up.

    Extending the NotifyIcon

    NotifyIcon is sealed so you cannot extend by inheritance. So I created a class SysTray which extends NotifyIcon by creating an instance of it in a field. The constructor takes in the tooltip text, the default Icon and the context menu.

    This class also adds other methods to show animation and text as explained below.

    Showing Text

    An application can only put in an icon in the tray. So the workaround is (there’s always one) to convert the text into an icon. ShowText API of the SysTray class just does this.

    public void ShowText(string text, Font font, Color col)

    {

        Brush brush = new SolidBrush(col);

     

        // Create a bitmap and draw text on it

        Bitmap bitmap = new Bitmap(16, 16);

        Graphics graphics = Graphics.FromImage(bitmap);

        graphics.DrawString(text, m_font, brush, 0, 0);

     

        // Convert the bitmap with text to an Icon

        IntPtr hIcon = bitmap.GetHicon();

        Icon icon = Icon.FromHandle(hIcon);

        m_notifyIcon.Icon = icon;

    }

    What we are doing here is creating a bitmap, drawing the text to be shown on it and then converting it to an icon. Since NotifyIcon can show an Icon in the tray it’ll show the textual icon just fine.

     

    Showing Animation

    Showing animation in the tray is equally easy by making an array of icons each of which represent an animation frame and then switching the icons in the tray on timer events. SysTray accepts the image frame in different formats

    public void SetAnimationClip (Icon [] icons)
    Here icon needs to be an array of 16x16 icon each for a frame


    public void SetAnimationClip (Bitmap [] bitmap)
    Here bitmap needs to be an array of 16x16 bitmap each for a frame

    public void SetAnimationClip (Bitmap bitmapStrip)
    bitmapStrip is a bitmap strip of size n16x16. Where n is the number of frames. SysTray class extracts the individual frames from this strip and converts it to icons and uses it for the animation. 

    The last method is the easiest to use. For this you need to prepare an animation strip image. The image needs to have each frame of animation side by side. Each frame is 16x16 pixels and the color to render transparent is the common background of all the frames. Let’s take

    The caller prepares this image by making it transparent and then calling SetAnimationClip

     

    private void button1_Click(object sender, System.EventArgs e)

    {

        m_sysTray.StopAnimation();

        Bitmap bmp = new Bitmap("tick.bmp");

        // the color from the left bottom pixel will be made transparent

        bmp.MakeTransparent();

        m_sysTray.SetAnimationClip(bmp);

        m_sysTray.StartAnimation(150, 5);

    }

     

    SetAnimationClip uses the following code to create the animation frame

    public void SetAnimationClip (Bitmap bitmapStrip)

    {

        m_animationIcons = new Icon[bitmapStrip.Width / 16];

        for (int i = 0; i < m_animationIcons.Length; i++)

        {

            Rectangle rect = new Rectangle(i*16, 0, 16, 16);

            Bitmap bmp = bitmapStrip.Clone(rect, bitmapStrip.PixelFormat);

            m_animationIcons[i] = Icon.FromHandle(bmp.GetHicon());

        }

    }

     

    To animate the frame StartAnimation starts a timer and in the timer the icons are changed to animate the whole sequence.

     

    public void StartAnimation(int interval, int loopCount)

    {

        if(m_animationIcons == null)

            throw new ApplicationException("Animation clip not set with    

                                            SetAnimationClip");

     

        m_loopCount = loopCount;

        m_timer.Interval = interval;

        m_timer.Start();

    }

     

    private void m_timer_Tick(object sender, EventArgs e)

    {

        if(m_currIndex < m_animationIcons.Length)

        {

            m_notifyIcon.Icon = m_animationIcons[m_currIndex];

            m_currIndex++;

        }

        ....

    }

     

    Using SysTray

     

    This is how you use the class

    1. Create and wire up your menu
          ContextMenu m_menu = new ContextMenu();                                   

          m_menu.MenuItems.Add(0, new MenuItem("Show",new

                               System.EventHandler(Show_Click)));

    2. Get an icon you want to show statically in the tray.

    3. Create a SysTray object with all the required information
          m_sysTray = new SysTray("Right click for context menu",
                      new Icon(GetType(),"TrayIcon.ico"), m_menu);
    4. Create image strips with the animation frames. For 6 frame strip the image will have a width of 6*16 and height as 16 pixels

          Bitmap bmp = new Bitmap("tick.bmp");
          // the color from the left bottom pixel will be made transparent

          bmp.MakeTransparent();
          m_sysTray.SetAnimationClip(bmp);
    5. Start animation indicating how many times you need to loop the animation and the frame delay
          m_sysTray.StartAnimation(150, 5);
    6. To stop animation call
          m_sysTray.StopAnimation();

    Sources - SysTray Implementation: SysTray.cs

     

     

    using System;

    using System.Windows.Forms;

    using System.Drawing;

     

    namespace Abhinaba.SysTray

    {

        /// <summary>

        /// SysTray class that can be used to display animated icons or text in the system tray

        /// </summary>

        public class SysTray : IDisposable

        {

            #region Constructor

            /// <summary>

            /// The constructor

            /// </summary>

            /// <param name="text">The toolip text</param>

            /// <param name="icon">The icon that will be shown by default, can be null</param>

            /// <param name="menu">The context menu to be opened on right clicking on the

            ///                    icon in the tray. This can be null.</param>

            public SysTray(string text, Icon icon, ContextMenu menu)

            {

                m_notifyIcon = new NotifyIcon();

                m_notifyIcon.Text = text; // tooltip text show over tray icon

                m_notifyIcon.Visible = true;

                m_notifyIcon.Icon = icon; // icon in the tray

                m_DefaultIcon = icon;

                m_notifyIcon.ContextMenu = menu; // context menu

                m_font = new Font("Helvetica", 8);

     

                m_timer = new Timer();

                m_timer.Interval = 100;

                m_timer.Tick += new System.EventHandler(this.m_timer_Tick);

     

            }

            #endregion // Constructor

     

            #region Public APIs

            /// <summary>

            /// Shows text instead of icon in the tray

            /// </summary>

            /// <param name="text">The text to be displayed on the tray.

            ///                    Make this only 1 or 2 characters. E.g. "23"</param>

            public void ShowText(string text)

            {

                ShowText(text, m_font, m_col);

            }

            /// <summary>

            /// Shows text instead of icon in the tray

            /// </summary>

            /// <param name="text">Same as above</param>

            /// <param name="col">Color to be used to display the text in the tray</param>

            public void ShowText(string text, Color col)

            {

                ShowText(text, m_font, col);

            }

            /// <summary>

            /// Shows text instead of icon in the tray

            /// </summary>

            /// <param name="text">Same as above</param>

            /// <param name="font">The default color will be used but in user given font</param>

            public void ShowText(string text, Font font)

            {

                ShowText(text, font, m_col);

            }

            /// <summary>

            /// Shows text instead of icon in the tray

            /// </summary>

            /// <param name="text">the text to be displayed</param>

            /// <param name="font">The font to be used</param>

            /// <param name="col">The color to be used</param>

            public void ShowText(string text, Font font, Color col)

            {

                Bitmap bitmap = new Bitmap(16, 16);//, System.Drawing.Imaging.PixelFormat.Max);

     

                Brush brush = new SolidBrush(col);

     

                Graphics graphics = Graphics.FromImage(bitmap);

                graphics.DrawString(text, m_font, brush, 0, 0);

     

                IntPtr hIcon = bitmap.GetHicon();

                Icon icon = Icon.FromHandle(hIcon);

                m_notifyIcon.Icon = icon;

     

            }

     

            /// <summary>

            /// Sets the animation clip that will be displayed in the system tray

            /// </summary>

            /// <param name="icons">The array of icons which forms each frame of the animation

            ///                     This'll work by showing one icon after another in the array.

            ///                     Each of the icons must be 16x16 pixels </param>

            public void SetAnimationClip(Icon[] icons)

            {

                m_animationIcons = icons;

            }

     

            /// <summary>

            /// Sets the animation clip that will be displayed in the system tray

            /// </summary>

            /// <param name="icons">The array of bitmaps which forms each frame of the animation

            ///                     This'll work by showing one bitmap after another in the array.

            ///                     Each of the bitmaps must be 16x16 pixels  </param>

            public void SetAnimationClip(Bitmap[] bitmap)

            {

                m_animationIcons = new Icon[bitmap.Length];

                for (int i = 0; i < bitmap.Length; i++)

                {

                    m_animationIcons[i] = Icon.FromHandle(bitmap[i].GetHicon());

                }

            }

     

            /// <summary>

            /// Sets the animation clip that will be displayed in the system tray

            /// </summary>

            /// <param name="icons">The bitmap strip that contains the frames of animation.

            ///                     This can be created by creating a image of size 16*n by 16 pixels

            ///                     Where n is the number of frames. Then in the first 16x16 pixel put

            ///                     first image and then from 16 to 32 pixel put the second image and so on</param>

            public void SetAnimationClip(Bitmap bitmapStrip)

            {

                m_animationIcons = new Icon[bitmapStrip.Width / 16];

                for (int i = 0; i < m_animationIcons.Length; i++)

                {

                    Rectangle rect = new Rectangle(i * 16, 0, 16, 16);

                    Bitmap bmp = bitmapStrip.Clone(rect, bitmapStrip.PixelFormat);

                    m_animationIcons[i] = Icon.FromHandle(bmp.GetHicon());

                }

            }

     

            /// <summary>

            /// Start showing the animation. This needs to be called after

            /// setting the clip using any of the above methods

            /// </summary>

            /// <param name="loop">whether to loop infinitely or stop after one iteration</param>

            /// <param name="interval">Interval in millisecond in between each frame. Typicall 100</param>

            public void StartAnimation(int interval, int loopCount)

            {

                if (m_animationIcons == null)

                    throw new ApplicationException("Animation clip not set with SetAnimationClip");

     

                m_loopCount = loopCount;

                m_timer.Interval = interval;

                m_timer.Start();

            }

     

            /// <summary>

            /// Stop animation started with StartAnimation with loop = true

            /// </summary>

            public void StopAnimation()

            {

                m_timer.Stop();

            }

            #endregion // Public APIs

     

            #region Dispose

            public void Dispose()

            {

                m_notifyIcon.Dispose();

                if (m_font != null)

                    m_font.Dispose();

            }

            #endregion

     

            #region Event handlers

            private void m_timer_Tick(object sender, EventArgs e)

            {

                if (m_currIndex < m_animationIcons.Length)

                {

                    m_notifyIcon.Icon = m_animationIcons[m_currIndex];

                    m_currIndex++;

                }

                else

                {

                    m_currIndex = 0;

                    if (m_loopCount <= 0)

                    {

                        m_timer.Stop();

                        m_notifyIcon.Icon = m_DefaultIcon;

                    }

                    else

                    {

                        --m_loopCount;

                    }

                }

            }

     

            #endregion // Event handlers

     

            #region private variables

     

            private NotifyIcon m_notifyIcon;

            private Font m_font;

            private Color m_col = Color.Black;

            private Icon[] m_animationIcons;

            private Timer m_timer;

            private int m_currIndex = 0;

            private int m_loopCount = 0;

            private Icon m_DefaultIcon;

     

            #endregion // private variables

        }

    }

     

    Sources - SysTray Usage: Form1.cs

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

     

  • I know the answer (it's 42)

    C#: String comparison guidelines and common usage

    • 2 Comments

    The recommendation for string comparison has been updated for Whidbey (Visual Studio 2005) and there is an excellent MSDN article on this. One of the highlights is the introduction of the clear cut enumeration that can be passed into most string comparison methods to indicate the kind of comparison you are trying to make.

    [Serializable]

    [ComVisible(true)]

    public enum StringComparison

    {

    CurrentCulture = 0,

    CurrentCultureIgnoreCase = 1,

    InvariantCulture = 2,

    InvariantCultureIgnoreCase = 3,

    Ordinal = 4,

    OrdinalIgnoreCase = 5,

    }

    The recommendation also states that for culture-agnostic comparisons use the Ordinal and OrdinalIgnoreCase comparisons. These are fast and also safe. They rely on byte matching and are excellent options for matching strings for internal (non-UI) processing.

    string.Compare(str1, str2, StringComparison.Ordinal);

    With the introduction of the guidelines, developers have become defensive and have started looking for all code that compare string and re-coding them to meet the guidelines. Let's take a look at the most common culture-agnostic string matching used in code and see if they are safe.

    string.Equals(string1, string2)

    Default interpretation for equals is Ordinal so using this is fine. In case of using any other type of comparison use in the lines of
    string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);

    string1 == string2

    In accordance to the class library design guidelines the == operator for string is overloaded and the implementation is same as for string.Equals. So this is equivalent of calling string.Equals(string1, string2, StringComparison.Ordinal) which is also fine.

    switch(string1)

    For small sized switch blocks of the form

    string myStr = "Abhinaba";

    // ...

    switch (myStr)

    {

    case "Abhinaba":

    Console.WriteLine("switch match"); break;

    default:

    Console.WriteLine("switch did not match"); break;

    }

    the code is compiled into 

    if ((myStr!= null) && (myStr == "Abhinaba"))

    {

    Console.WriteLine("switch match");

    }

    else

    {

    Console.WriteLine("switch did not match");

    }

    So this is also fine and the comparison will be a string.Equals( strA, strB, StringComparison.Ordinal) comparison.

    However, if the switch block is larger then things get complicated. A dictionary is created and lookup happens through Dictionary.TryGetValue with the string as the key. Lookup happens using code similar to

    int num1 = this.comparer.GetHashCode(key) & 0x7fffffff;

    for (int num2 = this.buckets[num1 % this.buckets.Length]; num2 >= 0; num2 = this.entries[num2].next)

    {

    if ((this.entries[num2].hashCode == num1) && this.comparer.Equals(this.entries[num2].key, key))

    {

    return num2;

    }

    }

    A quick look and a bit of poking around with reflector indicates that the result will ultimately be the same as string.Equals( strA, strB, StringComparison.Ordinal).

    InvariantCulture

    As the guideline suggests replace all InvariantCulture usage with either Ordinal or OrdinalIgnoreCase

    Be on the Safe Side

    The above discussion was mainly to figure out what to make out of common string comparison statements in existing code. Going forward I think its best to be defensive and clear in code and explicitly call the comparison methods and pass the correct StringComparison enumeration constant.

  • I know the answer (it's 42)

    C# 3.0: I love object and collection initializers

    • 29 Comments

    This is the my fourth post in the series of posts I am making on C#3.0. See the previous posts here, here and here

    Object and collection initializers are new features in C#3.0 which is syntactic sugar over how you allocate and initialize objects and collections.

    Object Initializers

    Lets consider a class

    class Customer

    {

    public string name;

    public string address;

    int age;

    public int Age { get { return age; } set { age = value; } }

    }

    if I wanted to create a object of the type customer and fill up the public variables and properties then in C#2.0 I need to do something like

    Customer customer = new Customer();

    customer.name = "Abhinaba Basu";

    customer.address = "1835 73rd Ave NE, Medina, WA 98039";

    customer.Age = 99;

    With the new object initializer syntax it possible to do all of the above in one statement as in

    var cust = new Customer{name = "Abhinaba Basu", 
    address =
    "1835 73rd Ave NE, Medina, WA 98039",
    Age = 99 };

    This not only reduces lines of code but increases flexibility a lot. So no more being forced to write 5 overloads of a contructor that just accepts these variables and assigns them to fields.This syntax works for both public fields and properties.

    In case the class contains other classes as fields then the same statement can also initialize the contained class (see code below in bold).

    class Phone

    {

    public int countryCode;

    public int areaCode;

    public int number;

    }

    class Customer

    {

    public string name;

    public string address;

    public Phone phone;

    }

    static void Main(string[] args)

    {

    var cust = new Customer{name = "Abhinaba Basu",
    address =
    "1835 73rd Ave NE, Medina, WA 98039",

    phone = new Phone {countryCode = 1, areaCode = 425,
    number = 9999999}};

    }

    This piece of code is equivalent to the following C#2.0 code

    Customer customer = new Customer();

    customer.name = "Abhinaba Basu";

    customer.address = "1835 73rd Ave NE, Medina, WA 98039";

    Phone __phone = new Phone();

    __phone.countryCode = 1;

    __phone.areaCode = 425;

    __phone.number = 9999999;

    customer.phone = __phone;

    Collection Initializers

    Collection initializer is a natural extension to object initializer. It consists of a list of initilializer for each of the elements in a collection seperated by comma and encosed in {}. For a simple List<int> it is as follows

    List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    For the Customer and Phone number class discussed above the collection initializer will look like

    List<Customer> custList = new List<Customer> {

    new Customer {

    name = "Samrat", address = "NJ, USA",

    phone = new Phone {countryCode = 91, areaCode = 999, number = 8888888}

    },

    new Customer {

    name = "Kaushik", address = "Behala, Kolkata",

    phone = new Phone {countryCode = 91, areaCode = 33, number = 3333333}

    },

    new Customer {

    name = "Rahul", address = "Kerala",

    phone = new Phone {countryCode = 91, areaCode = 00, number = 4444}

    }

    };

    Just trying to write the above piece of code in C#2.0 syntax shows how cool this new feature is.

    Why I love this

    This feature reduce typing a lot and will go a long way in helping reduce carpal tunnel syndrome :^)

    Moreover, it is very intuitive, so much so that people will start using this even without figuring out that this was not present before and they were required to allocate the collection and then individually allocate and add each item to the collection. Just after using it for a couple of days I am badly missing it in C#2.0. I just can't figure out why this was not considered for 2.0. I can guess why but I'll still complain :)

  • I know the answer (it's 42)

    Aspect Oriented Programming using .NET

    • 22 Comments

    Some time back I wrote an article on AOP using .NET languages. It's a long post so bear with me. In case you are interested only about C# go down here

    What is AOP

    Aspect Oriented Programming or AOP is an interesting concept that can be applied to many of the programming problems we solve everyday. In our Visual Studio team system code we have a lot of web-services and remoting code that essentially does the following

    public void MyMethod(int parameter)

    {

    Trace.EnteredMethod("MyMethod", parameter);

    SecurityCheck();

    // Bunch of processing

    Trace.ExitMethod("MyMethod");

    }

    This is not just peculiar to our domain but is seen across different domains. In OO programming classes and methods are designed for performing specific operations and common/duplicate functionality are factored out into common classes. However, there are cross-cutting concerns that span accross all classes and methods, like logging and security checks. OOP only partially solves this problem by requiring users to define separate classes for logging and security checks and requiring each class/methods needing these services to call them. AOP targets and solves this problem elegantly.

    AOP divides code into base-code (code for your functionality) and a new construct called aspect. Aspect encapsulates these cross-cutting concerns using the following concepts

    • join-points: The points in the structure of base-code where the cross-cutting functionality needs to execute. This is typically when specific methods are entered or exited or properties are accessed.
    • point-cut: A logical description of join-points using some specific syntax
    • advice: additional code like logging and security check that each of these methods need to perform

    The most mature AOP language is probably AspectJ which adds AOP extensions to Java. However, for this blog, I'd stick to .NET languages like AspectDNG, Aspect# and C#.

    Language support for AOP

    AOP support has to be built in to the language and/or framework because it is based on method call interception. Whenever a methods is called the framework needs to provide a stub to call some other piece of code. Though .NET CLR has this capability, but it is intrusive as you need an object to extend from MarshalByRefObject  or ContextBoundObject to allow method-interception. This is a serious limitation because each class needs to be written so that it supports AOP. Many AOP languages or language-extensions get around this limitation by using various techniques. The techniques generally fall into two broad categories runtime or dynamic weaving, compile-time or static weaving.

    Aspect# is AOP language which uses static compile time weaving. It uses its own proxy (and not CLR's proxy) called DynamicProxy. DynamicProxy is generated compile time and works differently while proxying interfaces (generates dynamic class and delegates method calls to the target of invocation) and proxying classes (generates stub class that inherits from the target).

    Aspect# provides syntax to define point-cut and call method-interceptors or advice for them. It's done as follows 

    import YourCompany.CMS.ContentProviders in YourCompanyAssembly
    import YourCompany.CMS.Aop.Interceptors
    aspect SecurityAspect for RSSContentProvider
         include Mixins.SecurityResourceImpl in MyMixinsAssembly
    pointcut method(* MyMethod(*))
               advice(TracingInterceptor)
         end



    end
    public class TracingInterceptor : IMethodInterceptor { public object Invoke(IMethodInvocation invocation) { // Trace using information from IMethodInvocation
    // like Method, MethodInvocationTarget return invocation.Proceed(); } }

    The important bits are marked in bold. The first block is the point-cut in a Ruby like syntax which specifies all methods with the name MyMethod to be included in the join-points.  The second block is the interceptor (advice) TracingInterceptor. The TracingInterceptor is a class that has to implement the IMethodInterceptor. It can call invocation.Proceed to continue with the method invocation once it's done with the tracing. So whenever the MyMethod is called TracingInterceptor.Invoke gets called.

    Other languages like AspectDNG (.NET based AOP language-extension) accomplishes this using something called IL weaving. In this the target or base-code is coded in any language that can be compiled into MSIL like C#, VB.NET, J#. So the target code can look like

    using System;

     

    public class MyClass {

    public int ProcessString(String s, out string outStr) {

    // ...  

    }

    }

    There is no special code or any type of modification needed on the base-code as evident from above which is plain-vanilla C# code. The aspect code is written as follows which can also be C# code and needs some additional assembly reference and attribute decoration for AspectDNG to pick them up

    using DotNetGuru.AspectDNG.MetaAspects;

    using DotNetGuru.AspectDNG.Joinpoints;

    using System;

    public class AspectsSample{

    [AroundCall("* MyClass::ProcessString(*)")]

    public static object YourMethodCallInterceptor(JoinPoint jp) {

    Console.WriteLine("Code before calls to '.. MyClass.ProcessString(..)'");

    object result = jp.Proceed();

    Console.WriteLine("Code after calls to '.. MyClass.ProcessString(..)'");

    return result;

    }
    }

    Here point-cut is specified using attributes like AroundCall, AroundBody. Both the base-code and the aspect code are separately compiled into different assemblies using respective compilers like csc into Target.exe and aspect.dll. Then the aspectdng.exe tool can be used which uses reflection to reach to the attribute in the aspect code to weave call to them so that a new assembly called Target-weaved.exe is created. In target-weaved.exe AspectDNG directly puts in calls to the aspects around the target code by inserting/replacing IL code wherever required.

    There are some AOP languages like Encase which apply the aspects at run time. These languages use AOP frameworks which reads in configuration files for point-cuts and at runtime generate proxy classes that intercept calls, allows advice of the aspect to execute first and then invokes the actual target. The benefit is that there is not edit-compile cycle but it faces performance issues.

    AOP in C#

    Till now we were talking about non-mainstream languages to get AOP done. However, by doing a bit extra work we can get the same functionality in C# as well. The limitation with CLR is that it allows method interception only when the classes containing the methods inherit from MarshalByRefObject  or ContextBoundObject. When a class inheriting from ContextBoundObject is activated, the .NET interceptor comes into play. It creates a trasparent-proxy and a real-proxy. The transparent-proxy gets called for all invocation of the target. The transparent proxy serializes the call stack and passes that on to the real-proxy. The real-proxy calls the first message sink which is an object implementing the IMessageSink interface. Its the duty of this first message sink to call the next until the final sink goes and calls the actual target. In this sink chaining we can insert objects which can execute our aspect advice.

    Another limitation with C# is that there is no way in C# syntax to specify join-points. We will circumvent these two limitations by inheriting the target classes from ContextBoundObject. We'll use attributes on specific classes so that all methods and field-setters in them become included into the join-points.

    using System;

    // Include the aspect framework

    using Abhinaba.Aspect.Security;


    [
    Security()]

    public class MyClass : ContextBoundObject {

    public int ProcessString(String s, out string outStr) {

    Console.WriteLine("Inside ProcessString");

    outStr = s.ToUpper();

    return outStr.Length;

    }
    }

    Here Security is an attribute defined in our Abhinaba,Aspect.Security namespace which pulls in our support for AOP and includes the current class and all its methods in the join-points. The whole AOP framework looks as follows. All the important parts are marked in bold.

    using System;

    using System.Diagnostics;

    using System.Runtime.Remoting.Messaging;

    using System.Runtime.Remoting.Contexts;

    using System.Runtime.Remoting.Activation;

    namespace Abhinaba.Aspect.Security

    {

    internal class SecurityAspect : IMessageSink {

    internal SecurityAspect(IMessageSink next)

    {

    m_next = next;

    }

     

    private IMessageSink m_next;

    #region IMessageSink implementation

    public IMessageSink NextSink

    {

    get{return m_next;}
    }

    public IMessage SyncProcessMessage(IMessage msg)

    {

    Preprocess(msg);

    IMessage returnMethod =
    m_next.SyncProcessMessage(msg);

    return returnMethod;

    }

    public IMessageCtrl AsyncProcessMessage(IMessage msg,
    IMessageSink replySink)

    {

    throw new InvalidOperationException();

    }

    #endregion //IMessageSink implementation


    #region
    Helper methods

    private void Preprocess(IMessage msg)

    {

    // We only want to process method calls

    if (!(msg is IMethodMessage)) return;

    IMethodMessage call = msg as IMethodMessage;

    Type type = Type.GetType(call.TypeName);

    string callStr = type.Name + "." + call.MethodName;

    Console.WriteLine("Security validating : {0} for {1}",
    callStr,
    Environment.UserName);

    }

    #endregion Helpers

    }

    public class SecurityProperty : IContextProperty,
    IContributeObjectSink

    {

    #region IContributeObjectSink implementation

    public IMessageSink GetObjectSink(MarshalByRefObject o,
    IMessageSink next)

    {

    return new SecurityAspect(next);

    }

    #endregion // IContributeObjectSink implementation

    #region IContextProperty implementation

    // Implement Name, Freeze, IsNewContextOK

    #endregion //IContextProperty implementation

    }

    [AttributeUsage(AttributeTargets.Class)]

    public class SecurityAttribute : ContextAttribute

    {

    public SecurityAttribute() : base("Security") { }

    public override void GetPropertiesForNewContext(
    IConstructionCallMessage ccm)

    {

    ccm.ContextProperties.Add(new SecurityProperty());

    }

    }
    }

     

    SecurityAttribute derives from ContextAttribute and MyClass derives from ContextBoundObject, due to this even before the ctor of the class is called the framework instantiates SecurityAttribute and calls the GetPropertiesForNewContext passing it a reference to IConstructionCallMessage. SecurityAttribute creates an instance of SecurityProperty and adds it to the context. This addition makes the framework call the various IContextProperty methods that SecurityProperty implements and then calls the ctor of MyClass.

    After this the first time any MyClass method or variable is referenced it calls GetObjectSink method of SecurityProperty through its IContributeObjectSink interface. This method returns a newly created instance of SecurityAspect. Till this you can consider everything as initialization code and SecurityAspect implements our main functionality for AOP advice.

    When the instance of SecurityAspect is created its constructor is passed a reference to next message sink so that all the sinks can be chained and called one after the other. After this SyncProcessMessage is called which is our main method interceptor and where all processing is done. After doing all processing like security verification the code calls the target method. Then it can refer to the return value and do post-processing. With this we have AOP implementation albeit some intrusive code as the target codes needs to be modified for AOP support.

    Possibilities

    AOP is a very generic programming method and can be used in a variety of situation. Some of them are as follows

    Sample code

    The sample solution (VS2005) including all sources are available here. It contains sources for two different aspects, one for security and one for tracing both applied on the same class. I have applied conditional compilation attribute to the tracing aspect so that on release build tracing gets disabled.

  • I know the answer (it's 42)

    C# 3.0 : using extension methods for enum ToString

    • 8 Comments

    In my previous blog I was trying to address the issue that when ToString is called on an enum the literal string for the enum constant is returned. Custom attributes can be used to tag localizable description string to the constants so that you can write functions that use reflection to get to the attribute and show that string. However this has a significant disadvantage as you need to write code as follows

    enum Coolness {

    [DescriptionAttribute("Not so cool")]

    NotSoCool = 5,

    [DescriptionAttribute("Very cool")]

    VeryCool = NotSoCool + 7,

    }

    class Program

    {

    static string GetDescription(Enum en)

    {

    // Uses reflection to get the attribute and returns it

    }

    static void Main(string[] args)

    {

    Coolness coolType = Coolness.VeryCool;

    Console.WriteLine(GetDescription(coolType));

    }

    }

    Calling GetDescription method on the enum is definitely not intuitive. This is why I had said that I love extension methods. Converting this to use C#3.0 extension method makes its intuitive and it'll be easy for the developer to remember that in the same lines as ToString there is also a ToDescription on enums.

    using System;

    using System.Reflection;

    using System.Collections.Generic;

    using System.Text;

    using System.Query;

    using System.Xml.XLinq;

    using System.Data.DLinq;

    using System.ComponentModel;

    // C# 3.0 syntax used...

    namespace FunWithEnum

    {

    enum Coolness

    {

    [DescriptionAttribute("Not so cool")]

    NotSoCool = 5,

    Cool, // since description same as ToString no attr are used

    [DescriptionAttribute("Very cool")]

    VeryCool = NotSoCool + 7,

    [DescriptionAttribute("Super cool")]

    SuperCool

    }

    static class ExtensionMethods

    {

    public static string ToDescription(this Enum en) //ext method

    {

    Type type = en.GetType();

    MemberInfo[] memInfo = type.GetMember(en.ToString());

    if (memInfo != null && memInfo.Length > 0)

    {

    object[] attrs = memInfo[0].GetCustomAttributes(
    typeof(DescriptionAttribute),

    false);

    if (attrs != null && attrs.Length > 0)

    return ((DescriptionAttribute)attrs[0]).Description;

    }

    return en.ToString();

    }

    }

    class Program

    {

    static void Main(string[] args)

    {

    Coolness coolType1 = Coolness.Cool;

    Coolness coolType2 = Coolness.NotSoCool;

    Console.WriteLine(coolType1.ToDescription());

    Console.WriteLine(coolType2.ToDescription());

    }

    }

    }

     

  • I know the answer (it's 42)

    typedef is good for your wrist but not for your social life

    • 17 Comments

    Sometimes, we find many of our favourite features missing in a language. The instant reaction is that we crib (yea, I do this a lot :-) ) and raise it as a short-coming of that language. Most language features are pretty simple to be implemented in a compiler and we miss the point that it could be that the language-designer deliberatly omitted that because it either does not fit in the language's general design or he considers it to be a bad programming practice.

    A classic example of this is typedef. C++ users are very familiar with it and have abused this to no end. While some uses of it could be ok, people typically go overbeoard using this. Sometime back I used the following

    typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, 
                  BYTE bAlpha, DWORD dwFlags);
    lpfn g_pSetLayeredWindowAttributes = 
    (lpfn)GetProcAddress(hUser32, "SetLayeredWindowAttributes");

    I think this is a legitimate use where I used typedef to create a alias for a function pointer to a Win32 api. 

    The C++ world is different, there you play around with global functions, function pointers, cross-platform issues. In C++ applications its typical to see typedef or #define being used to create types like NUMBER, TCHAR to switch types based on compilation options and also for cross-platform support where you want NUMBER to be 16-bit unsigned int irrespective of the plaform. But these things make code very un-maintainable. I have spent countless hours traversing definition chains and it irritates a poor guy trying to debug a piece of code late in the evening to find out customer_list ultimately boils down to char * after 3-4 indirections. 

    I am not against typedefs, I have used it and have benefitted from other frameworks using it and its good for your wrist (less typing == less pain). But in C# it simply doesn't fit in and so C# doesn't have typedef. This is something to happy about and not a short-coming of the language. A lot of pain-points of C++ are not present in C# and hence you do not need typedefs that much.

    using != typedef

    Actually its wrong to say C# doesn't have typedef. It does have it in the form of using. But this is limited in terms of usage and functionality. You can do the following...

    using EmpList = System.Collections.Generic.List<MyNameSpace.Employee>;

    using NUMBER = System.Int32;


    namespace
    MyNameSpace

    {

    public class Employee

    {

    }


    public
    class Class1

    {

    public Class1()

    {

    EmpList empList = new EmpList();

    NUMBER num = 5;

    }

    }
    }

    So you can create alias for int and EmpList for a generic list of employees. The reason why this does not get as bad as typdefs is because of the following reasons

    1. You can have using clause only at the top of a namespace (using must precede all other namespace elements othern than extern alias declaration).
    2. using supports aliasing only classes and namespaces and not arbitrary types
    3. And most importantly aliases defined using using is visible only in that file(compilation unit). Actually the exact same thing is true for C++ as well. However in C++ you can put it in a .h file and include it in as many .cpp file you want. C# does not have the concept of including source files and so everything is just fine.

    Not having typedef is not bad

    actually its very good.

    Just because a language does not have a feature doesn't mean that it has to be bashed. In one thread I saw someone bashing C# becuase he couldn't do using NUMBER=int; globally. What I couldn't figure out is why do you want to do this? In C# int is anyway an alias for System.Int32 and for any system on which .NET is ported (Mono/Rotor project) int will be ported for you to support 32-bit signed integer and you should not be bothered with doing it yourself. If you think NUMBER gives more clarity, think twice. Wear a maintainers hat and think if you come to that code 2 years later will NUMBER look more clear or int. Even List<Employee> is much more readable than EmpList (see below).

    Some people suggest working around this limitation (???) using inheritance as follows.

    public class EmpList : 

    System.Collections.Generic.List<typedef.Employee>

    {

    }

    I think this simply doesn't scale. For example you'll need to create EmpList constructors that match those od List<T>.

     

Page 1 of 15 (374 items) 12345»