Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C# 4.0

C# 4.0

Scheduled Pinned Locked Moved The Lounge
csharpquestiondiscussionannouncement
233 Posts 75 Posters 619 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jon Rista

    The methods implemented as part of IEnumerable are now intrinsic to the .NET framework. Any decent developer would avoid using identifiers already used in the framework. Your argument about IEnumerable.Where is the same as saying Developer B creates a new class called String... Simple solution, don't do that. Almost any language feature can be missused. Don't missuse them.

    S Offline
    S Offline
    Sunny Ahuwanya
    wrote on last edited by
    #194

    Not every developer writing C# code targets the .NET framework. I used IEnumerable.Where as an example which everyone would understand but it could be any set of extension methods from XYZ company.

    Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

    1 Reply Last reply
    0
    • H higelino

      The good answer to 'constness' is spec#. The 'constness', the 'nonnullness', the 'checkedexceptionness' , the 'immutabilityness' etc ... are all describable with contracts. I think Spec# will be a real revolution in the .NET world. In fact the .NET infrastructure should evolve to include contract metadata so that every .NET language will manage contracts in a similar way. Try it here http://research.microsoft.com/SpecSharp/[^] ... and cry because I am sure that c#4 won't include spec# :-(

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #195

      higelino wrote:

      I am sure that c#4 won't include spec#

      Yes, I bet it won't. Unfortunately. :(

      Kevin

      1 Reply Last reply
      0
      • J Jamie Nordmeyer

        So now that C# 4.0 is being talked about, I was wondering what people thought would be good additions to the language. Sorry if this is a repost, but I went through several pages, and didn't see anything, so... What I'd frankly love to see would be tuples. Rather than having to use multiple 'out' parameters, you'd just return multiple values:

        public int,int MinMax(int[] numbers)
        {
        int min, max;
        // Code to calculate min/max

        return min, max;
        }

        What do you think? What would be good for the next version?

        Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

        U Offline
        U Offline
        User 3973690
        wrote on last edited by
        #196

        A couple of things I would like is better control over memory. The automatic garbage collection is nice and I understand that it's usualy better just to let it be so it can collect when needed, but in some cases I know the memory can and must be freed like when processing a large import file. Also I would like to be able to "Destroy" an object in certain situations. By that I mean to have one method that can nullify all refrences to a particualar object. Another thing would be multipule inheritance, or at least some psuedo-composite type scenario. My company seels a website management tool which runs as a smart client, so I write a lot of UI code that is shared between web and windows. We have our own MVC style system so much of the "Controller" and "Model" code is common to both web and win, but we also have several methods we have to add to both web and windows controls to support that system. Our only option now is to use interfaces, but 90% of the implementation of those interfaces for each control could be shared, but is currently copied since both our web and windows controls can't derive off of a base class.

        S 1 Reply Last reply
        0
        • J James Lonero

          Interesting. I thought that C# originally was to be more OO than C++. Even in C++, you can write a completely procedural language type program (the C language). Maybe, Microsoft, in their zeal to make it easier to write software, is turning C# into an all in one language where you can get a job done any way you want. Maybe, at some later date, the keyword class will not be deprecated (not a first class item). Already, the anonymous type and var keyword are reducing the OO aspect of the language. Then, maybe, the hardcore OO engineers will move to Java.

          S Offline
          S Offline
          Sunny Ahuwanya
          wrote on last edited by
          #197

          James Lonero wrote:

          Maybe, Microsoft, in their zeal to make it easier to write software, is turning C# into an all in one language where you can get a job done any way you want.

          C++?

          Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

          1 Reply Last reply
          0
          • J Jon Rista

            C# is what I call a progressive language. It started out in a very simple, Java-like form in v1.0. But it has continually evolved into a richer, stronger, more expressive language that has only served to do one thing: Improve my productivity. Hard-core OO programming is a very limiting form of software engineering. Objects don't solve everything in the simplest way...on the contrary, rich object models with full inheritance and associative relationships can get in the way of providing a simple, effective, easily maintained solution. You have to use the right tool for the job, and objects are most assuredly NOT the right tool for every job. I for one am extremely grateful that Microsoft did not allow C# to stagnate like Sun allowed Java to. I use C# for what it is...a progressive, evolving language that has an EXTENSIVE amount of analysis and design behind it to ensure that features that are added provide benefit...not detriment. The end result, IMO, is an extremely powerful, expressive, flexible language that allows me to quickly solve problems without always having to resort to a complex OO mechanism. Sometimes, an enumerable collection of simple structures (tuples say), is all one needs to get the job done.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #198

            I certainly agree with that.

            1 Reply Last reply
            0
            • Y Yortw

              1. Retry keyword, from VB.NET (structured error handling) 2. Dyanmic intefaces, from VB.NET 3. AppActivate function, from VB.NET 4. Non-beta version of the parallel task library 5. Better WPF designers 6. Better user experience when working on single code file shared between .NET Framework and .NET Compact Framework projects 7. Improved keyboard/focus and dynamic control creation support in .NET Compact Framework (support for ActiveControl, ControlAdded/Removed events etc). 8. Fix for the (very rare) bug caused by compiler optimisations on the String.IsNullOrEmpty function. 9. A version of the various TryParse functions that returns the default value for expected type, instead of returning true/false with an out parameter. 10. TryParse on System.Enum. Probably a lot of other stuff too, but that's all I can think of off the top of my head :-D Tuples would also be cool :cool:

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #199

              Yortw wrote:

              8. Fix for the (very rare) bug caused by compiler optimisations on the String.IsNullOrEmpty function.

              I thought they already fixed that in 3.0 or 3.5.

              Y 2 Replies Last reply
              0
              • A Anastasiosyal

                Namely i'd like to see the following: 1. Implementing Interfaces by delegation to fields 2. Anonymous type return values 3. Some Duck-typing or Structural Subtyping support 4. Safe Null Dereferencing Operator as someone already mentioned (eg) Customer?.Orders?.Last?.Address I have expanded more on this some time ago here: http://anastasiosyal.com/archive/2008/07/19/4-features-for-c-4.0.aspx

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #200

                Anastasiosyal wrote:

                3. Some Duck-typing

                From what I've read about duck-typing, I don't think it's a good idea; it has no way of verifying the "like a duck" part. If I have an object that walks and quacks, but not like a duck you'd better not call it a duck.

                1 Reply Last reply
                0
                • S Sunny Ahuwanya

                  Jon Rista wrote:

                  Portability? Where are you going to "port" a C# or VB.NET app to...they only run on the .NET platform anyway.

                  Wrong. Look up Portable.NET[^] and Mono[^] There are also other compilation techniques out there. See IL2CPU on Cosmos[^] and Bartok used for the Singularity project.[^] I don't understand why programmers can't seem to separate C# from .NET. C# is a programming language with a syntax and rules. The C# life cycle of your project ends after you successfully compile your project. The framework you are using then executes the compiled binaries (and it doesn't have to be IL).

                  Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

                  J Offline
                  J Offline
                  Jon Rista
                  wrote on last edited by
                  #201

                  I don't think people separate them, because they are so tightly integrated. C# is a language, but it is tightly integrated with the .NET framework. Thats part of what makes it such a great platform...the C# compiler simplifies the use of the framework. Its the same with the Singularity project. Their version of C# was even more advanced than anything we have today. The C# from Singularity was a blend of C# 2.0, Cω, Spec#, with some additional features none of them had. In particular, Singularity C# made message contracts first class language citizens similar to interfaces. The C# from Singularity is far from portable in the sense your talking about...it was tightly integrated with the Singularity platform. Its also extremely powerful, and its features will probably find their way into the public C# spec at some point (woot!). When it comes to portability between different frameworks like Mono. C# is an open, standard specification, and each version can be implemented by any third party...they just tend to be a bit delayed. I know for a fact that Mono 2.0 will include a fully compliant C# 3.0 compiler, which means code you may have written over the past year (it hasn't even been a year yet) will be portable to Mono. That includes LINQ, Extension methods, anonymous types, etc. If you require cross-platform portability, I don't think the problem is that its not possible...you just can't take advantage of new language versions until the third-parties catch up.

                  S 1 Reply Last reply
                  0
                  • S SlingBlade

                    The idea is so good they already implemented it in 3.5! ;P Perhaps I should stop using 2.0. I played with 3.5 briefly to check out WPF and Linq and didn't particularly see much use for them so I decided to stay away because of the performance drawbacks that came with WPF and didn't want to be tempted to use it. I missed the IEnumerable extensions and probably many more features that could come in handy. Am I the only one that WPF runs like molasses for? If not are there any other features to stay away from in order to perform decent on lowend and older machines? P.S. - Thanks for the Line Counter add-in! I've been using it for a couple months now.

                    modified on Friday, October 3, 2008 2:30 AM

                    J Offline
                    J Offline
                    Jon Rista
                    wrote on last edited by
                    #202

                    Glad you like the Line Counter add-in. I've been poking at it lately, trying to improve it and make it work with VS2008 too. Hopefully another version will be released soon. As for C# 3.0/.NET 3.5, there are LOTS of useful things. WPF is certainly more resource heavy than basic windows forms, but its only one of the many new things available. You also have WCF, WF, LINQ (which isn't just for querying databases...it queries any enumerable type which is GREAT, you'll love it), anonymous types, partial methods, expression trees, class initializers, HashSet, and more. The LINQ features, which is where IEnumerable.Any comes from...its a part of LINQ, are probably the most useful addition to the language.

                    S 1 Reply Last reply
                    0
                    • J Jon Rista

                      I don't think people separate them, because they are so tightly integrated. C# is a language, but it is tightly integrated with the .NET framework. Thats part of what makes it such a great platform...the C# compiler simplifies the use of the framework. Its the same with the Singularity project. Their version of C# was even more advanced than anything we have today. The C# from Singularity was a blend of C# 2.0, Cω, Spec#, with some additional features none of them had. In particular, Singularity C# made message contracts first class language citizens similar to interfaces. The C# from Singularity is far from portable in the sense your talking about...it was tightly integrated with the Singularity platform. Its also extremely powerful, and its features will probably find their way into the public C# spec at some point (woot!). When it comes to portability between different frameworks like Mono. C# is an open, standard specification, and each version can be implemented by any third party...they just tend to be a bit delayed. I know for a fact that Mono 2.0 will include a fully compliant C# 3.0 compiler, which means code you may have written over the past year (it hasn't even been a year yet) will be portable to Mono. That includes LINQ, Extension methods, anonymous types, etc. If you require cross-platform portability, I don't think the problem is that its not possible...you just can't take advantage of new language versions until the third-parties catch up.

                      S Offline
                      S Offline
                      Sunny Ahuwanya
                      wrote on last edited by
                      #203

                      Jon Rista wrote:

                      C# is a language, but it is tightly integrated with the .NET framework.

                      Is there any keyword in the C# language that suggests the use of a particular framework? What is to stop any one with the talent and time to write a C# compiler/framework that targets a currently unsupported platform?

                      Jon Rista wrote:

                      you just can't take advantage of new language versions until the third-parties catch up.

                      So what do i do if I need to port the code right now?

                      Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

                      J 1 Reply Last reply
                      0
                      • S Sunny Ahuwanya

                        Jon Rista wrote:

                        C# is a language, but it is tightly integrated with the .NET framework.

                        Is there any keyword in the C# language that suggests the use of a particular framework? What is to stop any one with the talent and time to write a C# compiler/framework that targets a currently unsupported platform?

                        Jon Rista wrote:

                        you just can't take advantage of new language versions until the third-parties catch up.

                        So what do i do if I need to port the code right now?

                        Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

                        J Offline
                        J Offline
                        Jon Rista
                        wrote on last edited by
                        #204

                        "Is there any keyword in the C# language that suggests the use of a particular framework?" - Sunny Well, LINQ syntax comes to mind. The C# compiler translates LINQ into expression trees, which are a part of the .NET 3.5 framework. LINQ is syntactic sugar for a complex framework feature. Seems to be bound to a particular framework to me. Which is all good with me...I'd rather write LINQ syntax than build expression trees manually. "What is to stop any one with the talent and time to write a C# compiler/framework that targets a currently unsupported platform?" - Sunny Never said it would stop anyone...hence my discussion of Mono. :rolleyes: "So what do i do if I need to port the code right now?" - Sunny If you needed to write portable code, why did you dive in and use C# 3.0? Come on man, have some common sense. If A, then B. If you need portable code, then use a portable language version. It's a simple concept. Don't go all about being the antagonist nay-saying useful language features that, apparently, you want to use, and are just miffed that the third-party projects like Mono havn't caught up with yet. It's like your a little kid, "If I can't have it, no one will!!"

                        S 1 Reply Last reply
                        0
                        • S S Senthil Kumar

                          Vikram A Punathambekar wrote:

                          but how would you implement a runtime check?

                          The fact that a const method is being called will have to be embedded in the IL and the type verifier will have to lookup the called method's metadata (atleast once) to know whether it is still const. It's doable in theory, but will probably be impracticably slow in practice. And it'll probably have to throw an exception if it finds a mismatch. I wish there was a better mechanism for const though - even in C++, I hated the cascading effect of const. In a way, it is like checked exceptions in Java i.e., throw a new exception in a method at the lowermost level, and the throws clause of every method that directly or indirectly calls it will have to modified.

                          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                          V Offline
                          V Offline
                          Vikram A Punathambekar
                          wrote on last edited by
                          #205

                          Yeah, that's how I thought it would be implemented, if it were.

                          S. Senthil Kumar wrote:

                          in C++, I hated the cascading effect of const

                          I can't see how you could make it non-cascading: if a const method were to call a non-const method (which then proceeds to modify the object), that would totally break the contract it promises its calling methods.

                          S. Senthil Kumar wrote:

                          the throws clause of every method that directly or indirectly calls it will have to modified

                          Only if the excception is not handled. I am a big fan of Java's checked exceptions. :)

                          Cheers, Vıkram.


                          "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

                          S 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Bad example. How is that better than: lookingFor = "%" + lookingFor ; lookingFor = lookingFor + "%" ; lookingFor = "%" + lookingFor + "%" ; The standard way is much clearer (at least in this case).

                            P Offline
                            P Offline
                            Pawel Krakowiak
                            wrote on last edited by
                            #206

                            PIEBALDconsult wrote:

                            How is that better than: lookingFor = "%" + lookingFor ; lookingFor = lookingFor + "%" ; lookingFor = "%" + lookingFor + "%" ; The standard way is much clearer (at least in this case).

                            I was using the example code to filter SubSonic queries (which look much like LINQ extension methods) and it definitely looks better there. By the way, you know that C# strings are immutable and the above code you provided creates additional string objects just to discard them right after they are created and make them wait for the garbage collector to do its job? ;) Very inefficient, I'd say.

                            P 1 Reply Last reply
                            0
                            • J Jon Rista

                              "Is there any keyword in the C# language that suggests the use of a particular framework?" - Sunny Well, LINQ syntax comes to mind. The C# compiler translates LINQ into expression trees, which are a part of the .NET 3.5 framework. LINQ is syntactic sugar for a complex framework feature. Seems to be bound to a particular framework to me. Which is all good with me...I'd rather write LINQ syntax than build expression trees manually. "What is to stop any one with the talent and time to write a C# compiler/framework that targets a currently unsupported platform?" - Sunny Never said it would stop anyone...hence my discussion of Mono. :rolleyes: "So what do i do if I need to port the code right now?" - Sunny If you needed to write portable code, why did you dive in and use C# 3.0? Come on man, have some common sense. If A, then B. If you need portable code, then use a portable language version. It's a simple concept. Don't go all about being the antagonist nay-saying useful language features that, apparently, you want to use, and are just miffed that the third-party projects like Mono havn't caught up with yet. It's like your a little kid, "If I can't have it, no one will!!"

                              S Offline
                              S Offline
                              Sunny Ahuwanya
                              wrote on last edited by
                              #207

                              Jon Rista wrote:

                              Well, LINQ syntax comes to mind. The C# compiler translates LINQ into expression trees, which are a part of the .NET 3.5 framework. LINQ is syntactic sugar for a complex framework feature. Seems to be bound to a particular framework to me.

                              The foreach keyword binds to .NET's IEnumerable pattern, the lock keyword binds to .NET's Monitor.Enter/Monitor.Exit methods. There is nothing new in the C# 3 compiler translating LINQ's query expressions to .NET 3.5's expression trees. Anybody developing a compiler targeting a particular framework will decide how they want these keywords to work. Do they bind to a framework library? Do they generate their own code? etc.

                              Jon Rista wrote:

                              Come on man, have some common sense. If A, then B. If you need portable code, then use a portable language version. It's a simple concept. Don't go all about being the antagonist nay-saying useful language features that, apparently, you want to use, and are just miffed that the third-party projects like Mono havn't caught up with yet. It's like your a little kid, "If I can't have it, no one will!!"

                              LOL. You are right, but bear in mind that developers routinely port code someone else wrote. If I'm given a task to port C# 3 code to a C# 2 platform. I'm not going to sit around waiting for the third party platform to catch up. I should be able to do it because the .NET framework for both platforms are binary compatible. If I run into expression trees and Linq query syntax. I'll simply write a substitute class that performs the same functionality (provided the original author properly packaged those statements in classes). You have said good things about C# 3 and you have been correct (I personally love lambda expressions), however, we should call a spade a spade and that black sheep is called Extension methods. If their is something wrong with a product, the consumers have every right to voice their concerns and my concern is nobody except Microsoft knows how to properly use Extension methods. If you notice they: a) Package extension methods in exclusive namespaces. b) Extension methods usually implement an algorithm (not a function) c) Don't tell anyone else how to properly use it (I for one haven't seen a best practices paper on extension methods) The goto keyword is in every major programming language but it has been repeatedly advised not to be used unless y

                              J 1 Reply Last reply
                              0
                              • S Sunny Ahuwanya

                                Jon Rista wrote:

                                Well, LINQ syntax comes to mind. The C# compiler translates LINQ into expression trees, which are a part of the .NET 3.5 framework. LINQ is syntactic sugar for a complex framework feature. Seems to be bound to a particular framework to me.

                                The foreach keyword binds to .NET's IEnumerable pattern, the lock keyword binds to .NET's Monitor.Enter/Monitor.Exit methods. There is nothing new in the C# 3 compiler translating LINQ's query expressions to .NET 3.5's expression trees. Anybody developing a compiler targeting a particular framework will decide how they want these keywords to work. Do they bind to a framework library? Do they generate their own code? etc.

                                Jon Rista wrote:

                                Come on man, have some common sense. If A, then B. If you need portable code, then use a portable language version. It's a simple concept. Don't go all about being the antagonist nay-saying useful language features that, apparently, you want to use, and are just miffed that the third-party projects like Mono havn't caught up with yet. It's like your a little kid, "If I can't have it, no one will!!"

                                LOL. You are right, but bear in mind that developers routinely port code someone else wrote. If I'm given a task to port C# 3 code to a C# 2 platform. I'm not going to sit around waiting for the third party platform to catch up. I should be able to do it because the .NET framework for both platforms are binary compatible. If I run into expression trees and Linq query syntax. I'll simply write a substitute class that performs the same functionality (provided the original author properly packaged those statements in classes). You have said good things about C# 3 and you have been correct (I personally love lambda expressions), however, we should call a spade a spade and that black sheep is called Extension methods. If their is something wrong with a product, the consumers have every right to voice their concerns and my concern is nobody except Microsoft knows how to properly use Extension methods. If you notice they: a) Package extension methods in exclusive namespaces. b) Extension methods usually implement an algorithm (not a function) c) Don't tell anyone else how to properly use it (I for one haven't seen a best practices paper on extension methods) The goto keyword is in every major programming language but it has been repeatedly advised not to be used unless y

                                J Offline
                                J Offline
                                Jon Rista
                                wrote on last edited by
                                #208

                                I'm still not convinced that extension methods are this evil black sheep. Were beyond issues of portability now, and into issues of coding preference, I think. a) Package extension methods in exclusive namespaces. aa) Where would you package them, and why would that place be better than an exclusive namespace? b) Extension methods usually implement an algorithm. bb) First off, implementation of the method isn't the point, its the feature itself. Second, don't all methods implement an algorithm, however simple that algorithm may be? c) Don't tell anyone else how to properly use it. cc) Who defines what proper use is? In what usage instances is one way proper, and what other instances is it improper? Again, I think a little common sense works here. Extension method best practice #1: SIDE EFFECT FREE. Follow that rule, and I think your use of extension methods will be quite enjoyable, regardless of where they are packaged or whether they implement an algorithm or....well, isn't that it...algorithms? Another interesting tidbit about C# 3.0/.NET 3.5. Since .NET 3.5 is just a bunch of new API's on top of the .NET 2.0 runtime, and C# is a translator as well as a compiler, the end result is still CLI that is compatible with .NET 2.0. So long as the appropriate assemblies are available, a .NET application written with C# 3.0/.NET 3.5 should run on a system with only .NET 2.0 installed. Sounds pretty portable to me. ;) Speaking of goto, I use it in two cases:

                                foreach (...)
                                {
                                foreach (...)
                                {
                                foreach (...)
                                {
                                if (exitCondition)
                                goto DoneWithLoops;
                                }
                                }
                                }

                                DoneWithLoops:
                                // Finish up

                                Thats a lot cleaner than having a bunch of flags that have to be checked in every loop if a complete exit from all loops is needed. C# translates complete exits from nested loops into a goto anyway in a lot of cases, so the above code is whats going to happen anyway.

                                switch (...)
                                {
                                case 1:
                                {
                                // Do something that augments default
                                goto default;
                                }
                                case 2:
                                {
                                // Do something unique
                                break;
                                }
                                default:
                                {
                                // Do common stuff
                                break;
                                }
                                }

                                J S 2 Replies Last reply
                                0
                                • J Jon Rista

                                  I'm still not convinced that extension methods are this evil black sheep. Were beyond issues of portability now, and into issues of coding preference, I think. a) Package extension methods in exclusive namespaces. aa) Where would you package them, and why would that place be better than an exclusive namespace? b) Extension methods usually implement an algorithm. bb) First off, implementation of the method isn't the point, its the feature itself. Second, don't all methods implement an algorithm, however simple that algorithm may be? c) Don't tell anyone else how to properly use it. cc) Who defines what proper use is? In what usage instances is one way proper, and what other instances is it improper? Again, I think a little common sense works here. Extension method best practice #1: SIDE EFFECT FREE. Follow that rule, and I think your use of extension methods will be quite enjoyable, regardless of where they are packaged or whether they implement an algorithm or....well, isn't that it...algorithms? Another interesting tidbit about C# 3.0/.NET 3.5. Since .NET 3.5 is just a bunch of new API's on top of the .NET 2.0 runtime, and C# is a translator as well as a compiler, the end result is still CLI that is compatible with .NET 2.0. So long as the appropriate assemblies are available, a .NET application written with C# 3.0/.NET 3.5 should run on a system with only .NET 2.0 installed. Sounds pretty portable to me. ;) Speaking of goto, I use it in two cases:

                                  foreach (...)
                                  {
                                  foreach (...)
                                  {
                                  foreach (...)
                                  {
                                  if (exitCondition)
                                  goto DoneWithLoops;
                                  }
                                  }
                                  }

                                  DoneWithLoops:
                                  // Finish up

                                  Thats a lot cleaner than having a bunch of flags that have to be checked in every loop if a complete exit from all loops is needed. C# translates complete exits from nested loops into a goto anyway in a lot of cases, so the above code is whats going to happen anyway.

                                  switch (...)
                                  {
                                  case 1:
                                  {
                                  // Do something that augments default
                                  goto default;
                                  }
                                  case 2:
                                  {
                                  // Do something unique
                                  break;
                                  }
                                  default:
                                  {
                                  // Do common stuff
                                  break;
                                  }
                                  }

                                  J Offline
                                  J Offline
                                  Jon Rista
                                  wrote on last edited by
                                  #209

                                  I think this article makes my point for me: http://blogs.msdn.com/wesdyer/archive/2007/03/01/immutability-purity-and-referential-transparency.aspx[^]

                                  1 Reply Last reply
                                  0
                                  • P Pawel Krakowiak

                                    PIEBALDconsult wrote:

                                    How is that better than: lookingFor = "%" + lookingFor ; lookingFor = lookingFor + "%" ; lookingFor = "%" + lookingFor + "%" ; The standard way is much clearer (at least in this case).

                                    I was using the example code to filter SubSonic queries (which look much like LINQ extension methods) and it definitely looks better there. By the way, you know that C# strings are immutable and the above code you provided creates additional string objects just to discard them right after they are created and make them wait for the garbage collector to do its job? ;) Very inefficient, I'd say.

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #210

                                    Yes, I know that, but the implementation of SqlLike (or whatever) may be no better, and adds a function call as well (which may or may not get inlined). And perhaps pre-condition checking as well. Plus that's only true of the last example. There's no way to tell which is "more efficient" from just looking at the examples, and such concerns are likely to be needless anyway.

                                    1 Reply Last reply
                                    0
                                    • J Jon Rista

                                      I'm still not convinced that extension methods are this evil black sheep. Were beyond issues of portability now, and into issues of coding preference, I think. a) Package extension methods in exclusive namespaces. aa) Where would you package them, and why would that place be better than an exclusive namespace? b) Extension methods usually implement an algorithm. bb) First off, implementation of the method isn't the point, its the feature itself. Second, don't all methods implement an algorithm, however simple that algorithm may be? c) Don't tell anyone else how to properly use it. cc) Who defines what proper use is? In what usage instances is one way proper, and what other instances is it improper? Again, I think a little common sense works here. Extension method best practice #1: SIDE EFFECT FREE. Follow that rule, and I think your use of extension methods will be quite enjoyable, regardless of where they are packaged or whether they implement an algorithm or....well, isn't that it...algorithms? Another interesting tidbit about C# 3.0/.NET 3.5. Since .NET 3.5 is just a bunch of new API's on top of the .NET 2.0 runtime, and C# is a translator as well as a compiler, the end result is still CLI that is compatible with .NET 2.0. So long as the appropriate assemblies are available, a .NET application written with C# 3.0/.NET 3.5 should run on a system with only .NET 2.0 installed. Sounds pretty portable to me. ;) Speaking of goto, I use it in two cases:

                                      foreach (...)
                                      {
                                      foreach (...)
                                      {
                                      foreach (...)
                                      {
                                      if (exitCondition)
                                      goto DoneWithLoops;
                                      }
                                      }
                                      }

                                      DoneWithLoops:
                                      // Finish up

                                      Thats a lot cleaner than having a bunch of flags that have to be checked in every loop if a complete exit from all loops is needed. C# translates complete exits from nested loops into a goto anyway in a lot of cases, so the above code is whats going to happen anyway.

                                      switch (...)
                                      {
                                      case 1:
                                      {
                                      // Do something that augments default
                                      goto default;
                                      }
                                      case 2:
                                      {
                                      // Do something unique
                                      break;
                                      }
                                      default:
                                      {
                                      // Do common stuff
                                      break;
                                      }
                                      }

                                      S Offline
                                      S Offline
                                      Sunny Ahuwanya
                                      wrote on last edited by
                                      #211

                                      aa) If I didn't notice Microsoft placed all related extension methods in an (almost) extension-method only namespace. I would be placing regular methods and etension methods in the same namespace.

                                      Jon Rista wrote:

                                      b) Extension methods usually implement an algorithm. bb) First off, implementation of the method isn't the point, its the feature itself. Second, don't all methods implement an algorithm, however simple that algorithm may be?

                                      I mean most LINQ extension methods implement a generic algorithm that works with any object as opposed to a function that performs an action on one object. On most blogs i've read, developers want to extend a type with a function they want so badly instead of considering deriving from that type and adding the function. cc) Since Microsoft developed this feature and must have researched it intensively, they should have at least warned us about it.

                                      Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry

                                      J 1 Reply Last reply
                                      0
                                      • J Jamie Nordmeyer

                                        I saw someone comment on that on another forum. Basically, you'd have something like this (using his sample syntax):

                                        int? x = Company?.Person["Bob"]?.Age;

                                        If Company or Company.Person["Bob"] were null, then x would be set to null, rather than getting an exception. I likes.

                                        Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                                        K Offline
                                        K Offline
                                        Ken_GHill
                                        wrote on last edited by
                                        #212

                                        no more null refs!!!!

                                        M 1 Reply Last reply
                                        0
                                        • J Jon Rista

                                          Glad you like the Line Counter add-in. I've been poking at it lately, trying to improve it and make it work with VS2008 too. Hopefully another version will be released soon. As for C# 3.0/.NET 3.5, there are LOTS of useful things. WPF is certainly more resource heavy than basic windows forms, but its only one of the many new things available. You also have WCF, WF, LINQ (which isn't just for querying databases...it queries any enumerable type which is GREAT, you'll love it), anonymous types, partial methods, expression trees, class initializers, HashSet, and more. The LINQ features, which is where IEnumerable.Any comes from...its a part of LINQ, are probably the most useful addition to the language.

                                          S Offline
                                          S Offline
                                          SlingBlade
                                          wrote on last edited by
                                          #213

                                          Yeah, I'll definately have to upgrade to VS2008 and start using the new stuff. That's cool that you have been poking at the line counter. I wouldn't mind seeing a toolbar added and the ability to calculate the contents of a folder. Calculating basedon namespaces, classes, interfaces etc. would be nice too but I imagine that would be a lot more work than folders.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups