Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350

    Nelson's Corner: Type Inference in C#

    Hey guys.

    Although I'm not really a C# guy, I was taking a quick look at Nelson's LINQ videos (which look quite promising btw) and I actually have a question on type inference using the var-keyword.

    In the Introduction, it is mentioned that this is illegal:
    Code:
    var foo = 1, bar = 2;    //WRONG
    Because you can have only one declaration per var-keyword, this needs to be
    Code:
    var foo = 1;
    var bar = 2;
    Can you give a rationale for that behavior? I was mainly wondering because C++0x introduces type inference using the auto-keyword, which seems to work exactly the same, but afaik without this limitation. I suspect C# does this to avoid some kind of syntax ambiguities, but I really couldn't figure it out myself.

    Thanks.

  2. #2
    Join Date
    Mar 2004
    Posts
    0
    Quote Originally Posted by ComicSansMS View Post
    Hey guys.

    Although I'm not really a C# guy, I was taking a quick look at Nelson's LINQ videos (which look quite promising btw) and I actually have a question on type inference using the var-keyword.

    In the Introduction, it is mentioned that this is illegal:
    Code:
    var foo = 1, bar = 2;    //WRONG
    Because you can have only one declaration per var-keyword, this needs to be
    Code:
    var foo = 1;
    var bar = 2;
    Can you give a rationale for that behavior? I was mainly wondering because C++0x introduces type inference using the auto-keyword, which seems to work exactly the same, but afaik without this limitation. I suspect C# does this to avoid some kind of syntax ambiguities, but I really couldn't figure it out myself.

    Thanks.
    I don't think it's so much a technical limitation. Ultimately, the C# compiler _could_ just turn that one statement into 3 statements that would be equivalent. However, the "normal" multi-variable declaration requires that all of the variables that you declare are the same type, and people would expect that the implicit version would behave the same. With implicit typing, this is so much harder to do.

    So let's assume that multi-variable declarations do work and they behave as you'd expect (all variables declared must have the same type). How would this work?

    Code:
    interface IAmAnInterface {
    }
    
    class AClass : IAmAnInterface { }
    class BClass : IAmAnInterface { }
    
    class Program {
        void Main() {
            var obj1 = new AClass(), obj2 = new BClass();
        }
    }
    because obj1 and obj2 must be the same type, per our expectations of this syntax, they have to be casted into the parent type they both share - which in this case is IAmAnInterface. This behavior introduces more problems, however, as outlined below:

    Code:
    interface IAmAnInterface { }
    interface IAmAnotherInterface { }
    
    class AClass : IAmAnInterface { }
    class BClass : IAmAnotherInterface { }
    
    class Program {
        void Main() {
            var obj1 = new AClass(), obj2 = new BClass();
        }
    }
    Now. Should they be turned into "IAmAnInterface" - or "IAmAnotherInterface"?

    So my understanding is that it boils down to this: make the syntax behave differently then people expect (by allowing multiple variables different types in a single declaration) or introduce a nightmare of type resolution.


  3. #3
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    Quote Originally Posted by <> View Post
    So my understanding is that it boils down to this: make the syntax behave differently then people expect (by allowing multiple variables different types in a single declaration) or introduce a nightmare of type resolution.
    Or go a third way, like C++0x does: Don't allow implicit type conversions in this case at all.
    I'm quite surprised C# allows this here, since it severely complicates the type inference mechanism for both users and compilers. But then on the other hand, there's much less to worry about upcasts in C# than in C++ (type-slicing, anyone?)

    Thanks for clearing things up

    edit: The second sentence actually makes no sense, as there is no implicit upcast necessary in C#. I was still with Nelson's example back there.
    Last edited by ComicSansMS; 11-25-2010 at 03:36 PM.

  4. #4
    Join Date
    Oct 2006
    Posts
    1,548
    Why Couldn't

    Code:
    var obj1 = new AClass(), obj2 = new BClass();
    be transformed into

    Code:
    AClass obj1 = new AClass(); BClass obj2 = new BClass();
    I mean, from your explanation, the compiler already knows what they are, they're an AClass and a BClass.

    Why do they have to be the same type?
    [quote][\quote]

  5. #5
    Join Date
    Mar 2004
    Posts
    0
    Quote Originally Posted by wforl View Post
    Why Couldn't

    Code:
    var obj1 = new AClass(), obj2 = new BClass();
    be transformed into

    Code:
    AClass obj1 = new AClass(); BClass obj2 = new BClass();
    I mean, from your explanation, the compiler already knows what they are, they're an AClass and a BClass.

    Why do they have to be the same type?
    Because that conflicts with the expected behavior of that syntax. The entire point of that syntax is to declare and initialize multiple variables of the same type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •