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# 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)

    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#: 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# 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)

    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)

    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)

    software piracy in India

    • 28 Comments

    I always tried to figure out that in spite of the fact that India will become one of the largest PC markets in the future and that India SW engineers are everywhere, why is there no mainstream software in Indian languages. Yea there is Windows Hindi, and I read somewhere that Windows Bengali is also coming up, but thats after years of existance of Windows in other not so prevalent languages. I always got the answer that this is mainly due to SW piracy. Its so prevalent here that almost no one pays for software and hence the SW companies do not have revenue motive to localize their software for India. A further study revealed that 63% of al PCs in India ship with pirated Windows!!!! A person who is ready to use a pirated OS, will surely not pay for any other software he installs on that box. So this can lead to millions of dollars lost in revenue for the SW companies.

     

    Its so bad here that frankly till date I have rarely seen a home user using a legitimate copy of Windows. Even when I have seen that, it was in branded PCs which came pre-installed with the OS. The moment I congratulated someone for using a legitimate OS, within a minute he figures out that he was made to pay for the OS by the PC vendor, and he felt bad that he could have got the pc for less if he had a way for not opting for the OS.

     

    This issue is not only with home users, its every where, its in cyber cafe's, in SOHO, even in mid size organization and colleges. PC vendors are adding fuel to fire by shipping PCs with Linux pre-inslalled on them. This serves as a simple solution to buy a branded PC at a lower cost. People buy this PC, goes home and installs Windows from pirated CDs which cost them around Rs.100, thats a little more than $2. So now you have a branded PC running Windows sans the price.

     

    Many have suggested that low cost software is a solution to this. I absolutely do not agree with this. It may be a part of a solution but not the solution in itself. SW piracy has become so prevalent that no one feels like paying for it. You are considered a fool if you pay. What is required is a change in the way of thinking, user education and pride of ownership. People need to know that its equivalent to stealing, and is a punishable offense. This is a bad thought though, with 63% of all PCs shipping with pirated OS, it brings most my friends and relatives who own a PC in this category...

  • I know the answer (it's 42)

    C# 3.0: I do not like Anonymous types

    • 26 Comments

    This is the my fifth post in the series of posts I am making on the new features in C#3.0. See the previous posts on var, extension methodlambda expressions and object-collection initializers

    Before getting into why I do not like (actually hate) anonymous types in C#. Let's see what is this anonymous types and whats so special with this anonymity.

    Anonymous types

    Anonymous type is syntactic sugar to reduce typing. Let's say I need to create a structure or class to contain some data regarding customers. This is how you do it in C# 1.0

    class Customer

    {

    private string name;

    public string Name { get { return name; }
    set { name = value; }}
     

    private string address;

    public string Address { get { return address; }
    set { address = value; } }

    }

     

    Customer cust = new Customer();

    cust.Name = "Abhinaba Basu";

    cust.Address = "Hyderabad, India";

    Or using the new Object initialization syntax of C#3.0 as

    var cust = new Customer{Name = "Abhinaba Basu",
    Address =
    "Hyderabad, India"};

    This is a very repitative code pattern of creating data only containers (class or struct) for the related data types. This is can be done with the Anonymous method syntax as

    var cust1 = new { Name = "Abhinaba Basu",
    Address =
    "Hyderabad, India" };

    Note that in this statement I have not named a type and hence the compiler emits code to generate a new class definition whose name is not know to us and hence is a Anonymous type. The following code is generated. 

    public sealed class <Projection>f__0

    {

    // Methods

    public <Projection>f__0();

    public override bool Equals(object);

    public override int GetHashCode();

    public override string ToString();

    // Properties

    public string Address { get; set; }

    public string Name { get; set; }

    // Fields

    private string _Address;

    private string _Name;

    }

    Here <Projection>f__0 is the generated class' name. For each member a private field and the corresponding property is generated. The class also generates proper implementation to override Equals, GetHashCode and ToString methods based on each of the fields. The rest of the code is same as that used for Object initialization.

    If we have two such declaration that match in the field types then the same class will be used for both of them.

    var cust1 = new { Name = "Abhinaba", Address = "Hyderabad" };

    var cust2 = new { Name = "Ankur", Address = "Noida"};

    if (cust1.GetType() == cust2.GetType())

    Console.WriteLine("Cust1 and Cust2 are of the same type {0}",

    cust2.GetType().ToString());
    // output:

    // Cust1 and Cust2 are of the same type
    // AnonymousTypes.Program+<Projection>f__0

    There are couple of interesting point here

    • The whole thing works only for classes where you do not need to add methods
    • You have to accept the compilers implementaion of the overrides.
    • This works in situation where you are actually not interested to know the name of the generated class
    • All the fields in the class are implicitely typed (var). So the compiler figures out the type to use (Age is Int32)

    You can also use nested declaration as in

    var cust = new { Name = "Abhinaba Basu",
    Address =
    "Hyderabad, India",

    Phone = new { AreaCode = 33, Number = 444444}};

    Why I do not like anonymous types

    Again due to my upbringing in C/C++ I did not like implicit types when I saw it. This is taking the same idea to an extreme. Now not only are we letting the compiler choose the type for me I am also letting it generate new types!!!

    Even without my pre-disposition I think that this construct address some special programming cases and should not have been included in the language. It'll just add to the surface area and learning curve and mainly go unused. The unusability comes from the following reasons

    • Flexibility is lost. Today I have a data type that has only data and right now I do not care about the exact type. If in the future I need to add some method to it (say to change the Equals override) I need to change all the places where I used the anonymous types and convert them to named types.
    • I decide to add a new field to the class which is used by some of the object and not by some. At the same time I need all of the objects to be of the same type (maybe because I compare them) then I need to go and make changes at all the object allocation code
      var cust = new { Name = "Abhinaba Basu", Address = "Hyderabad, India", phone }
    • Readability of the code is lost. If I have an API like void Show(var value) and I call it as follows
      var cust = new { Name = "foo", Address="bar");
      Show(cust);
      then this does not convey much. The alternative is much more readable when void Show (Customer cust) is defined and used as follows
      Customer cust = new Customer { Name = "foo", Address="bar");
      Show (cust);
  • 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# 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)

    C# 3.0 : I don't like vars

    • 25 Comments

    Due to my upbringing in C/C++ somehow I feel uneasy whenever I see some like

    var a = 5;

    I guess this is ok in scripting languages. So when I heard that C# 3.0 is going to support implicit types for local variables that made me feel uneasy. I installed the PDC C# 3.0 bits (from the link mentioned before) and tried the following

    var a = 5;

    var s = "hello";

    Console.WriteLine(a.GetType());

    Console.WriteLine(s.GetType());

    The output was as expected a was System.Int32 and s System.String. However C# 3.0 is supporting implicit types and not the variant data type that many non-typed languages support. So the following will not compile

    var a = 5;

    a = "hello";

    Since C# is a strongly typed language once the implicit type has been inferred during compilation it does not change.

    While I do agree for things like LINQ implicit type is absolutely required but elsewhere as in the examples above vars make code more unreadable for me. Wherever I'll see a line of code with var I scroll up-down  to figure out what type it exactly is. Specially in some situation the type of variable has a lot to do with performance so if I see some thing like

    var al = Func1();
    ....
    ....
    for (i = 0; i < 10000000; i++)
    foreach (int el in al)
         total += el;

    I'd right click on al and say goto definition and see its a var. At this point its super important for me to figure out what's its type. If its an ArrayList and not a List<> then I have a huge perf degradation. Check out Rico Mariani's blog to find out why.

    <edited some stuff like the example above to add clarity based on the comments>
    < Fixed some syntax errors >

  • 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)

    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)

    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)

    Windows Phone 7 Series Programming Model

    • 23 Comments

    Bidar - Barid Shahi tombs

    Just sometime back I posted on the MIX 2010 announcements. One of the major aspects of the announcement was

    “The application model supported on Windows Phone 7 series will be managed only and will leverage Silverlight, XNA and the .NET Framework”

    That’s a mouthful and includes 3 framework names in once sentence :). This was already disclosed and has resulted in some flutter over the web and twitter. Let me try to explain how these 3 interplays in the application model with the following diagram

    image

    Managed only

    First of all the application model allows users to write only managed code (to repeat native code is not allowed). That means they do not have direct access to any native APIs and cannot use platform-invoke to call into native user or system code. So all resources including phone capabilities have to be accessed using the various managed APIs provided.

    Two Flavors of applications (XNA and Silverlight)

    There are 2 variants or flavors of applications users can write, XNA Games and Silverlight applications. Obviously the former is for writing games and the later is for your typical phone applications (nothing stops you from writing a Silverlight animation based game though). So you cannot mix-match SL and XNA UI in the same application.

    However, do note that the traditional NETCF development using WinForm based UI is not supported.

    Common Services available to both

    That said, beyond the UI rendering and controls there is the common services which both SL and XNA applications can use. This includes all the phone capabilities like microphone, location, accelerometer, sound, media, networking, etc... Some of these features come from SL and others from XNA but land up in the common pool usable by either types of applications.

    Core Runtime is .NET Compact Framework 3.7

    The core runtime for both SL and XNA applications is .NET Compact Framework (henceforth called NETCF). This is the team I work for.  .NETCF is a highly portable and compact (as in smaller size and footprint) implementation of the .NET specification. It is designed to run on resource constrained devices and on disparate HW and SW configurations.I will have a post later on the detailed architecture or .NETCF.

    Over the last two years a lot of work has gone into .NETCF to scale it in terms of features, performance and stress to meet the requirements of Windows Phone and other newer platforms on which it is being targeted. We also added a lot of features which I hope you will enjoy. Some of the features is just to reach parity with the desktop CLR and others are device specific and not available on the desktop.

    Over this blog in the course of the coming months I’ll try to share with you what all we did to reach here.

  • 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)

    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)

    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)

    Vista Rocks!!!

    • 20 Comments

    Vista screen shotAnd so does IE 7 and Office 12.....

    I am ashamed to admit that even though I work in Microsoft I never tried out the cool new products. Being a bit low on bug-count I decided to try out Vista (a.k.a. longhorn). I tried ugrading one of the not-so-high-end boxes (3GHz, 1GB ram) to Vista from WinXP. The installation went through smoothly without any glitches and after some time I was the proud owner of a Vista desktop :).

    Within couple of hours of fidling around I was super-happy with what I saw. Not only the UI has become sooooo cool the functionality is simply awsome. I loved the new metallic (??) progress bar. I loved the integrated search. I genereally navigate using keyboards. I loved the fact that I could hit the Win key and directly type the name of the program I was looking for. I typed winword and it showed me MS Word as well as all emails and files containing those words.

    Vista is way ahead in security, with various security plugins in IE and Windows-defender turned on. The system was very stable and I decided to push my luck harder and went ahead with installing Office 12. This simply blew my mind off. The coolest features I hit upon is that your callender, upcoming appointments and pending emails are visible in the main email window of Outlook. The new formatting-preview  of Word, where you hover you mouse on the font size and word shows how the text'd look with the formatting applied was also nice.

    Vista also fixes the old allegation on Windows that you need to go to Start to shut-down. Now there is no text on the Start button. On the default theme its just a round button....

    Right now I am writing this blog in IE7 running on Vista. All in all I was very impressed and plan to shift to Vista as my primary OS over the next few weeks....

  • 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)

    Reverse string

    • 20 Comments

    A friend of mine forwarded me this nice little problem he got asked in some interview.

    Write code to reverse a string such that the words are reversed as in "We apologise for the inconvenience" becomes "inconvenience the for apologise We".

    Obviously (is there fun otherwise?) the problem had to be solved for a memory constraint device (constant space).

    I thought I'd share the solution as its kind of neat.

    The solution is to do it in two passes. In the first pass the whole string is reversed and in the second each word is reversed again.

    After reversing the whole string: "ecneinevnocni eht rof esigolopa eW"
    Reversing word at a time:           "inconvenience eht rof esigolopa eW"
    ...
    Finally:                                         "inconvenience the for apologise We"

    A simple version of code (non-Unicode and considering only space as an word separator) doing this is as follows

     

    // reverses text in between two pointers
    void
    Reverse(char *c1, char *c2) { while(c1 < c2) { char ch = *c1; *c1++ = *c2; *c2-- = ch; } }


    // reverses the complete string void Reverse(char *str) { if (!str) return; printf("'%s' ===> ", str);
    if(strlen(str) > 0) {
    // get the complete string reversed in pass 1 Reverse(str, str + strlen(str) - 1); char *c1 = str, *c2 = str + 1; do {
    // find word boundary for(;*c2 != ' ' && *c2; c2++);
    // reverse each word
    Reverse(c1, c2 - 1); if (!*c2) break; // reached end of string c1 = ++c2; }while(*c2); } printf("'%s'\n", str); return; }
  • I know the answer (it's 42)

    Interesting Linked-List problem

    • 20 Comments

    Here's another problem which was given to me by Amit. I had lots of fun solving this :) 

    image

    Consider two link list as shown above which are joined at some point. The problem statement is as follows

    "Given pointers to two single-linked list, find out if they are joined and at which node they are joined. Use constant space to solve this and assume that the nodes cannot be modified."

    From the experience of the previous similar problem I posted let me first explain what is meant by constant space . Constant space means that the algorithm should use constant amount of space and its memory allocation should NOT increase as the length of the list increases. This means ironing out every byte allocation is a NOT a goal (constant space doesn't mean least amount of space). Just ensure that the allocation doesn't increase with the length.

    Non-Solution 

    The above two constrains rules out the two most common brute force algorithms that come to mind

    1. Add another bool/bit field to the nodes and while traversing the list mark the node by setting this field. Check out while traveling in second pass through the other pointer whether any node has this field set which would give the common node. There are some possible optimization over this like traversing the two lists in parallel so that not all the nodes be touched in the first pass.
    2. Create list/hash-tables of the nodes for look up later

    My-Solution

    I call this my solution because I'm sure there are better solutions to this :). This is O(n) solution.

    The pseudo-code

    1. Traverse the first list looking for the last node and compute the list's length (say len1)
    2. Traverse the second list looking for the last node and compute it's length (say len2)
    3. The last nodes of the two list has to be the same if they have joined anywhere in the middle. If not then the lists are not joined and hence return NULL
    4. Find whichever is the longer list (using len1 and len2) and increment the root pointer of that list by abs(len1 - len2). This would ensure that both the pointers are equidistant from joined node.
    5. Make another pass through the list by incrementing both the pointers and at every step ensure if they point to the same node. Since both are equidistant from the joined node (see step 4) this check will succeed and return the joined node.

    The C++ code looks something as follows

    // Data-structure for the node
    typedef
    struct Node { Node(int pVal) { val = pVal; p = NULL; } int val; Node *p; } *PNODE; PNODE GetJoinedNode(PNODE LL1, PNODE LL2) { // handle null lists if(LL1 == NULL || LL2 == NULL) return NULL; PNODE t1 = LL1, t2 = LL2; int len1 = 1, len2 = 1; // Find first list's length and it's last node for(;t1->p != NULL; t1 = t1->p) ++len1; // find second list's length and it's last node for(;t2->p != NULL; t2 = t2->p) ++len2; // last node not same means no joins in the middle if (t1 != t2) return NULL; // Advance the longer list by the difference in length so that // the pointers are equidistant from the join PNODE* t = len1 > len2 ? &LL1 : &LL2; AdvanceBy(t, abs(len1 - len2)); // last pass to find the join. for(;LL1 != LL2; LL1 = LL1->p, LL2 = LL2->p); return LL1; } void AdvanceBy(PNODE *pNode, int val) { for(int i = 0; i < val; ++i) (*pNode) = (*pNode)->p; }
  • I know the answer (it's 42)

    localized C#

    • 19 Comments
    C# and .NET supports Unicode and it’s super-cool that you can use variables and literals in your own language and get it to build and run. I never tried that out until I read Brad Adams post. I wrote the following program in C# using Hindi to name variables and namespaces.
    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace मेराप्रोग्राम
    {
    class प्रोग्राम

    {
    static void Main(string[] arg)
    {
    for (int संख्या = 0; संख्या < 10; संख्या++)
    {
    Console.WriteLine("क्रम {0}", संख्या);
    }
    }
    }
    }

    Michael Kaplan's post prompted me to share this code out. IMO most developers are forced to learn English. The reason is evident from the code above. Even if all documentations are available in localized version, I still need to know what's the meaning of the term "using", "for" and "class".

    I always thought that I'd hack the Mono csharp compiler to create a language that'll be truly localized as in have localized keywords and support localized numbers as well. I looked into the mono-sources long back and remember seeing the file cs-tokenizer.cs which looked promising as it contained the list of keywords. Once VSTS is shipped I think I'll have some time to try that out.

  • 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#: 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.

Page 1 of 15 (374 items) 12345»