Implicitly typed local variables, why local only?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rene

    Implicitly typed local variables, why local only?

    Could someone tell me why is not possible to have Implicitly typed* field
    level* variables? For example, why won't the following code compile?

    class Program
    {
    var myString = "abc";

    static void Main(string[] args)
    {
    }
    }

    If I try to compile this I get the following error: "The contextual keyword
    'var' may only appear within a local variable declaration".

    Why do I get that error? I mean, if I move the variable declaration inside
    the "Main" method things compile perfectly fine so it's obvious that the
    compiler is able to infer the type but why does it only work inside a
    method?

    Thanks.

  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Implicitly typed local variables, why local only?

    Rene wrote:
    Could someone tell me why is not possible to have Implicitly typed*
    field level* variables? For example, why won't the following code compile?
    >
    class Program
    {
    var myString = "abc";
    >
    static void Main(string[] args)
    {
    }
    }
    >
    If I try to compile this I get the following error: "The contextual
    keyword 'var' may only appear within a local variable declaration".
    >
    Why do I get that error? I mean, if I move the variable declaration
    inside the "Main" method things compile perfectly fine so it's obvious
    that the compiler is able to infer the type but why does it only work
    inside a method?
    I would definitely not like a public field of type var - documentation
    of it would be let us call it slightly frustrating.

    :-)

    Arne

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: Implicitly typed local variables, why local only?

      Rene wrote:
      Could someone tell me why is not possible to have Implicitly typed*
      field level* variables? For example, why won't the following code compile?
      >
      class Program
      {
      var myString = "abc";
      >
      static void Main(string[] args)
      {
      }
      }
      >
      If I try to compile this I get the following error: "The contextual
      keyword 'var' may only appear within a local variable declaration".
      >
      Why do I get that error? I mean, if I move the variable declaration
      inside the "Main" method things compile perfectly fine so it's obvious
      that the compiler is able to infer the type but why does it only work
      inside a method?
      >
      Thanks.
      >
      The var keyword is mainly intended for use with anonymous classes, and
      an anonymous class is limited to the method where it's created.

      I think that the var keyword was limited to methods simply to reduce the
      misuse of it. You should not use the var keyword for something as
      trivial as declaring a string variable, as the code doesn't get more
      readable from it.

      In some cases it's useful, for example, instead of:

      Dictionary<stri ng, Dictionary<stri ng, List<int>>tree = new
      Dictionary<stri ng, Dictionary<stri ng, List<int>>>();

      you can use:

      var tree = new Dictionary<stri ng, Dictionary<stri ng, List<int>>>();

      Other uses may reduce the readability, for example:

      var x = 42.0;

      You need to know a bit about how literal values work, to realise whether
      the type of the variable becomes a byte, short, int, long, float, double
      or decimal.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Implicitly typed local variables, why local only?

        On Oct 8, 2:53 am, "Rene" <a...@b.comwrot e:
        Could someone tell me why is not possible to have Implicitly typed* field
        level* variables? For example, why won't the following code compile?
        All of this is IMO:

        There's no technical reason why it couldn't be done, but there's a big
        difference between local variables and member variables, as all of the
        uses of local variables are in the (hopefully short) piece of code
        from their declaration to the end of their scope. In other words, you
        have all of the context available at the same place. That makes it
        less of a problem for it to be not-necessarily-absolutely-obvious what
        the type is - you can emphasize the "what the code does" rather than
        "here are the details of how it accomplishes it". The type of a member
        variable is a bit more important, and it's harder to clearly see all
        of its uses in the same place.

        Jon

        Comment

        Working...