question on anonymous type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • timor.super@gmail.com

    question on anonymous type

    Hi all,


    I have a question on anonymous type

    I can write :
    using (StreamWriter writer = new StreamWriter(.. .))

    and I can write this too :
    using (var writer = new StreamWriter(.. .))

    What is the best ? Which option should I choose ?
    The first seems to be more readable ...

    Thanks in advance for your answer
  • Martin Bonner

    #2
    Re: question on anonymous type

    On Jun 3, 11:03 am, timor.su...@gma il.com wrote:
    Hi all,
    >
    I have a question on anonymous type
    >
    I can write :
    using (StreamWriter writer = new StreamWriter(.. .))
    >
    and I can write this too :
    using (var writer = new StreamWriter(.. .))
    >
    What is the best?
    Which option should I choose ?
    It's a matter of taste usually.
    The first seems to be more readable ...
    Then use that. Personally, I don't like the duplication involved in
    writing "StreamWrit er" twice (and it gets worse when I want to say
    Dictionary<stri ng,List<Entry>o r some other equally complicated
    generic) ... but it's up to you.

    Note that var becomes more important when you start using LINQ
    expressions (where the actual types can be rather difficult to write
    down).

    Comment

    • Peter Morris

      #3
      Re: question on anonymous type

      I can write :
      using (StreamWriter writer = new StreamWriter(.. .))
      >
      and I can write this too :
      using (var writer = new StreamWriter(.. .))
      >
      What is the best ? Which option should I choose ?
      In this case I see no benefits in either, except "var" is quicker to type.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: question on anonymous type

        On Jun 3, 11:03 am, timor.su...@gma il.com wrote:
        I have a question on anonymous type
        Actually, you have a question on implicitly typed local variables.
        Anonymous types are the types created when you write code like this:

        new { Name="Jon" }

        The two features are often used together, but don't have to be.
        I can write :
        using (StreamWriter writer = new StreamWriter(.. .))
        >
        and I can write this too :
        using (var writer = new StreamWriter(.. .))
        >
        What is the best ? Which option should I choose ?
        The first seems to be more readable ...
        Well, the first contains redundant information - but it's more
        expicit. It's largely a matter of taste though. I've found myself
        using implicitly typed local variables quite a bit with no loss of
        readability.

        Jon

        Comment

        • timor.super@gmail.com

          #5
          Re: question on anonymous type

          Thanks all for your answer.

          That's mean that in IL language, both instructions are equivalent ?


          On 3 juin, 12:25, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On Jun 3, 11:03 am, timor.su...@gma il.com wrote:
          >
          I have a question on anonymous type
          >
          Actually, you have a question on implicitly typed local variables.
          Anonymous types are the types created when you write code like this:
          >
          new { Name="Jon" }
          >
          The two features are often used together, but don't have to be.
          >
          I can write :
          using (StreamWriter writer = new StreamWriter(.. .))
          >
          and I can write this too :
          using (var writer = new StreamWriter(.. .))
          >
          What is the best ? Which option should I choose ?
          The first seems to be more readable ...
          >
          Well, the first contains redundant information - but it's more
          expicit. It's largely a matter of taste though. I've found myself
          using implicitly typed local variables quite a bit with no loss of
          readability.
          >
          Jon

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: question on anonymous type

            On Jun 3, 1:10 pm, timor.su...@gma il.com wrote:
            Thanks all for your answer.
            >
            That's mean that in IL language, both instructions are equivalent ?
            Yes. Don't forget that C# 3 still compiles to IL run by the 2.0 (or
            2.0SP1) CLR. The new features of C# 3 don't require runtime support.
            Anonymous types are just compiled into normal types with names which
            are valid in IL but invalid in C#. Implicitly typed local variables
            just have their types inferred by the compiler and used as normal.

            Jon

            Comment

            • timor.super@gmail.com

              #7
              Re: question on anonymous type

              Ok, thanks for your answer

              Best regards

              On 3 juin, 14:27, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
              On Jun 3, 1:10 pm, timor.su...@gma il.com wrote:
              >
              Thanks all for your answer.
              >
              That's mean that in IL language, both instructions are equivalent ?
              >
              Yes. Don't forget that C# 3 still compiles to IL run by the 2.0 (or
              2.0SP1) CLR. The new features of C# 3 don't require runtime support.
              Anonymous types are just compiled into normal types with names which
              are valid in IL but invalid in C#. Implicitly typed local variables
              just have their types inferred by the compiler and used as normal.
              >
              Jon

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: question on anonymous type

                On Jun 3, 6:03 am, timor.su...@gma il.com wrote:
                Hi all,
                >
                I have a question on anonymous type
                >
                I can write :
                    using (StreamWriter writer = new StreamWriter(.. .))
                >
                and I can write this too :
                    using (var writer = new StreamWriter(.. .))
                >
                What is the best ? Which option should I choose ?
                The first seems to be more readable ...
                >
                Thanks in advance for your answer
                Hi,

                I would go for the first one, in this case using an anonymous type is
                unnecesary, somewhere I read a post that lamented that MS allowed such
                a construction.
                BTW, the above is not an anonymous type expression

                Comment

                • Marc Gravell

                  #9
                  Re: question on anonymous type

                   I would go for the first one, in this case using an anonymous type is
                  unnecesary, somewhere I read a post that lamented that MS allowed such
                  a construction.
                  Somewhere there is a line, and I don't claim to know where /exactly/
                  it is - but sometimes even in this scenario, "var" genuinely improves
                  the code. For example, what if it was a
                  KeyContainerPer missionAccessEn tryCollection*, or maybe a
                  Dictionary<Some LudicrouslyLong TypeName, AnotherLongType Name>...
                  repeating all that lot in the same expression (which is a pre-
                  requisite for "var) is just fluff.

                  *=yes, this isn't IDisposable, but that isn't the point I'm trying to
                  make...

                  Marc

                  Comment

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

                    #10
                    Re: question on anonymous type

                    timor.super@gma il.com wrote:
                    I have a question on anonymous type
                    >
                    I can write :
                    using (StreamWriter writer = new StreamWriter(.. .))
                    >
                    and I can write this too :
                    using (var writer = new StreamWriter(.. .))
                    >
                    What is the best ? Which option should I choose ?
                    The first seems to be more readable ...
                    I would go for the first as being more readable. Most
                    C# programmer will be used to reading the type first.

                    And you will not be typing the class name twice. The
                    IDE should propose it the second time.

                    Arne

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: question on anonymous type

                      On Jun 5, 3:34 am, Arne Vajhøj <a...@vajhoej.d kwrote:
                      What is the best ? Which option should I choose ?
                      The first seems to be more readable ...
                      >
                      I would go for the first as being more readable. Most
                      C# programmer will be used to reading the type first.
                      At the moment, yes. I think things will change as C# 3 becomes more
                      widely known.
                      And you will not be typing the class name twice. The
                      IDE should propose it the second time.
                      True, but it's not really about typing. It's about information
                      redundancy.

                      Ironically, the thing which the OP *didn't* do which I often would is
                      use a base type or interface:

                      using (TextWriter writer = new StreamWriter(.. .))

                      that adds information - it tells the reader that actually, any
                      TextWriter will do for what we need to call - it just so happens that
                      it uses StreamWriter at the moment. At the point you're adding this
                      extra information, there is no longer redundancy for implicit typing
                      to remove.

                      Jon

                      Comment

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

                        #12
                        Re: question on anonymous type

                        Jon Skeet [C# MVP] wrote:
                        On Jun 5, 3:34 am, Arne Vajhøj <a...@vajhoej.d kwrote:
                        >>What is the best ? Which option should I choose ?
                        >>The first seems to be more readable ...
                        >I would go for the first as being more readable. Most
                        >C# programmer will be used to reading the type first.
                        >
                        At the moment, yes. I think things will change as C# 3 becomes more
                        widely known.
                        I doubt it.

                        C# 3 will become widely known.

                        But most C# developers will have learned C# in earlier versions
                        and many of them will also use other languages from let us call
                        it "the C syntax family".
                        >And you will not be typing the class name twice. The
                        >IDE should propose it the second time.
                        >
                        True, but it's not really about typing. It's about information
                        redundancy.
                        I can not see that redundancy as a big problem. The compiler
                        will catch it if inconsistent changes are made.

                        Arne

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: question on anonymous type

                          Arne Vajhøj <arne@vajhoej.d kwrote:
                          At the moment, yes. I think things will change as C# 3 becomes more
                          widely known.
                          I doubt it.

                          C# 3 will become widely known.

                          But most C# developers will have learned C# in earlier versions
                          and many of them will also use other languages from let us call
                          it "the C syntax family".
                          Yes, I use Java day to day professionally now - and regularly miss
                          "var".

                          If people ignore the C#-specific features of C# 3 just because they're
                          not used to them, they'll be really missing out.
                          And you will not be typing the class name twice. The
                          IDE should propose it the second time.
                          True, but it's not really about typing. It's about information
                          redundancy.
                          I can not see that redundancy as a big problem. The compiler
                          will catch it if inconsistent changes are made.
                          The problem with redundancy isn't the possibility for inconsistency -
                          it's the lack of information density. It takes more space redundantly
                          specifying information, so there's more to wade through when reading
                          the code.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Web site: http://www.pobox.com/~skeet
                          Blog: http://www.msmvps.com/jon.skeet
                          C# in Depth: http://csharpindepth.com

                          Comment

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

                            #14
                            Re: question on anonymous type

                            Jon Skeet [C# MVP] wrote:
                            Arne Vajhøj <arne@vajhoej.d kwrote:
                            >>At the moment, yes. I think things will change as C# 3 becomes more
                            >>widely known.
                            >I doubt it.
                            >>
                            >C# 3 will become widely known.
                            >>
                            >But most C# developers will have learned C# in earlier versions
                            >and many of them will also use other languages from let us call
                            >it "the C syntax family".
                            >
                            Yes, I use Java day to day professionally now - and regularly miss
                            "var".
                            >
                            If people ignore the C#-specific features of C# 3 just because they're
                            not used to them, they'll be really missing out.
                            If the only think they miss out is stuff like this that has no
                            functional impact, then they will survive.
                            >>>And you will not be typing the class name twice. The
                            >>>IDE should propose it the second time.
                            >>True, but it's not really about typing. It's about information
                            >>redundancy.
                            >I can not see that redundancy as a big problem. The compiler
                            >will catch it if inconsistent changes are made.
                            >
                            The problem with redundancy isn't the possibility for inconsistency -
                            it's the lack of information density. It takes more space redundantly
                            specifying information, so there's more to wade through when reading
                            the code.
                            The possibility of inconsistency is the classic reason to avoid
                            redundancy.

                            Disk space is cheap.

                            And I find it hard to believe that the usage of var instead of
                            explicit classname should take longer time to read.

                            Arne

                            Comment

                            • Tim Jarvis

                              #15
                              Re: question on anonymous type

                              Arne Vajhøj wrote:
                              At the moment, yes. I think things will change as C# 3 becomes more
                              widely known.
                              >
                              I doubt it.
                              >
                              C# 3 will become widely known.
                              >
                              But most C# developers will have learned C# in earlier versions
                              and many of them will also use other languages from let us call
                              it "the C syntax family".
                              I think though that var is a special case because in some circumstances
                              its the only effective way to reference a type, anonymous types gained
                              from projection out of a linq query for example.


                              var report = from line in SomeEnumeration
                              select new
                              {
                              Value = line.SomeValue,
                              AnotherValue = line.AnotherVal ue
                              }

                              foreach (var reportline in report)
                              {
                              //...
                              }


                              I this will become common enough for var to be considered "normal" C#
                              syntax.

                              Cheers Tim.



                              --

                              Comment

                              Working...