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 304 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.
  • S S Senthil Kumar

    Sunny Ahuwanya wrote:

    There are so many things wrong with extension methods

    Care to list some of them? I get that they can pollute the list of methods in a class and can cause calls to unintended methods, what else do you find wrong?

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

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

    Confusion. a) People talk about them becoming members of the class, they do no such thing, they just look like it. b) Someone may ask "How do I do blah with X?" Someone else may answer "Just use X.blah()" without realizing that blah is an Extension Method (perhaps internal to the company or some third-party library that the asker doesn't have). The original asker will look in intellisense and maybe even check the documentation, but not find it. Extension Methods are user-hostile.

    1 Reply Last reply
    0
    • S shiftedbitmonkey

      I disagree. This is the reason for const. To constrain an implementation. You think its a bad idea because you can't subvert it. Hmmm... while we're at it we might as well eliminate private and protected aspects of classes as well. Get rid of readonly and just let everything be completely open. And watch the bugs fly... Do you have a solid argument against const?

      I've heard more said about less.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #142

      shiftedbitmonkey wrote:

      Do you have a solid argument against const?

      Of course not, I just don't think that the reasons to include it are strong enough, so I argue ;)

      1 Reply Last reply
      0
      • S Sunny Ahuwanya

        Pawel Krakowiak wrote:

        I think of them as of an improvement and use them. Smile

        Can anyone explain to me how extension methods are an improvement? Besides helping to sell LINQ and encouraging programmers to write code in a non-portable, non object oriented manner, what is the point of extension methods?

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

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

        Sunny Ahuwanya wrote:

        Can anyone explain to me how extension methods are an improvement?

        They allow me to simply add new functionality to the existing classes, including Framework classes and I personally find them useful. I use them with Enums and String to provide some new functionality needed in a project. So their exact purpose is an improvement for me, there's nothing to add.

        P 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

          Y Offline
          Y Offline
          Yortw
          wrote on last edited by
          #144

          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:

          J P 2 Replies Last reply
          0
          • P Pawel Krakowiak

            Sunny Ahuwanya wrote:

            Can anyone explain to me how extension methods are an improvement?

            They allow me to simply add new functionality to the existing classes, including Framework classes and I personally find them useful. I use them with Enums and String to provide some new functionality needed in a project. So their exact purpose is an improvement for me, there's nothing to add.

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

            Pawel Krakowiak wrote:

            add new functionality to the existing classes

            They do nothing of the sort!

            J P 2 Replies Last reply
            0
            • B bVagadishnu

              foreach (int value in supportedValues) { if (x == value) { xIsSupported = true; break; //why keep on when you are done? :confused: } }

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

              lol, it was late and I definately could have used a break, so I just didn't bother with the break. Or should I say didn't bother editing the post and fixing the bug when I realized I had forgotten to throw in the break. All the more reason the change would be so useful. Not only could it make for less coding, less bugs too.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Use a HashSet instead of an array.

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

                I don't want to use a HashSet. The whole point of the change suggestion is flexibility to use with any type of enumerable without having to write the code to iterate through it or convert it. That and a HashSet would only be able to handle the == or != operators. What if I want to use >, <, <= or >=?

                1 Reply Last reply
                0
                • J Jamie Nordmeyer

                  here here!

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

                  A Offline
                  A Offline
                  andy_p
                  wrote on last edited by
                  #148

                  hear, hear! I had heard that proper run-time const support in the clr was far too hard or expensive, and providing a weak const keyword in c# (like c++ has got) without properly enforcing it in the clr would just be misleading. Shame though; I would have liked proper const guarantees in the clr.

                  1 Reply Last reply
                  0
                  • L Leslie Sanford

                    harold aptroot wrote:

                    Why const? What will it even do besides limit the programmer in the usage of said parameters?

                    Well, that's kind of the point. You want to limit the usage of const parameters to minimize side-effects.

                    A Offline
                    A Offline
                    andy_p
                    wrote on last edited by
                    #149

                    Leslie Sanford wrote:

                    You want to limit the usage of const parameters to minimize side-effects.

                    Shouldn't it be "You want to use the limit of const parameters to minimize side-effects"? :)

                    1 Reply Last reply
                    0
                    • P Pawel Krakowiak

                      As for the optional parameters, they say that method overloads work better in that respect. I got used to it and don't complain. Maybe one advantage (trying to agree with MS) I can see is that when you debug your C# code the debugger (Call Stack) will show you which overload was called exactly, while it may not be apparent if a default parameter value was used...

                      A Offline
                      A Offline
                      andy_p
                      wrote on last edited by
                      #150

                      maybe another advantage is that loads of defaulted parameters would slow a simple call down, but using overloaded functions it would not have to push all those unused parameters onto the stack, so it would go faster.

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Jamie Nordmeyer wrote:

                        C# 4.0

                        I haven't heard anything about it.

                        Jamie Nordmeyer wrote:

                        return min, max;

                        That syntax wouldn't be a good choice, because of the comma operator. I would just return an array of int. Though the only place I do that is a routine that parses a string to get a latitude and longitude (doubles in this case).

                        R Offline
                        R Offline
                        Ribose
                        wrote on last edited by
                        #151

                        PIEBALDconsult wrote:

                        I would just return an array of int. Though the only place I do that is a routine that parses a string to get a latitude and longitude (doubles in this case).

                        I would return a System.Drawing.PointF for latitude/longitude values, or if I needed lat/long minutes and seconds, I would make a struct for it. :)

                        ~Ribose

                        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

                          A Offline
                          A Offline
                          andy_p
                          wrote on last edited by
                          #152

                          I'd like to see member variables treated as if they were in a class-lifetime 'using' statement. I miss the deterministic destructor from c++, but this would at least allow me to put the class in a using statement and have its member variables' dispose methods called implicitly.

                          A 1 Reply Last reply
                          0
                          • A andy_p

                            I'd like to see member variables treated as if they were in a class-lifetime 'using' statement. I miss the deterministic destructor from c++, but this would at least allow me to put the class in a using statement and have its member variables' dispose methods called implicitly.

                            A Offline
                            A Offline
                            andy_p
                            wrote on last edited by
                            #153

                            I guess I just want the default implementation of Dispose() to call Dispose() on all the member variables.

                            1 Reply Last reply
                            0
                            • S S Senthil Kumar

                              Let's say you are using a library with the following code

                              // Assembly SomeLib.dll

                              class C
                              {
                              void someMethod() const {...}
                              }

                              and the code using it looks like this

                              // Assembly App.exe

                              void usingMethod(const C obj)
                              {
                              obj.someMethod(); // ok, because someMethod is const
                              }

                              Now let's say a new version of SomeLib.dll comes out and in that version, someMethod() becomes non-const. With C++, App.exe would require recompiling with the modified header file, and the compiler would be able to flag the error in usingMethod (non const method call on a const object). With .NET and binary compatibility, there's no recompiling necessary - you just drop in the new DLL and now the const guarantee is broken. It would be a silent breaking change, unless there is a runtime check.

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

                              Thanks for the explanation, but how would you implement a runtime check? And what would it do - throw an exception that the called code has changed the object despite its (earlier) promise not to?

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

                                C Offline
                                C Offline
                                chaiguy1337
                                wrote on last edited by
                                #155

                                Jamie Nordmeyer wrote:

                                I saw someone comment on that on another forum.

                                Daniel Grunwald, TheCodeKing and myself came up with that here[^]. You're missing the final part tho, which is a ?? operator to act as the "default": int? x = Company?.Person["Bob"]?.Age ?? null; Of course setting null as the "default default" would also work and be handy.

                                “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO(   Check out my blog!   ) |)””’)                piHole.org -”-”-

                                J 1 Reply Last reply
                                0
                                • S Shog9 0

                                  PIEBALDconsult wrote:

                                  and return [w,h,d] ?

                                  Yeah, keep it distinct from blocks, initializers, etc.

                                  ----

                                  You're right. These facts that you've laid out totally contradict the wild ramblings that I pulled off the back of cornflakes packets.

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

                                  I think what your asking for can be fairly closely achieved already in C# 3.0..its just slightly more verbose: int[] tupleFunc() { double w, h, d; // ... return new[] { w, h, d }; } int[] tuple = tupleFunc();

                                  J S 2 Replies Last reply
                                  0
                                  • J Jamie Nordmeyer

                                    Sigh. As I've said above numerous times, it's not NEEDED, it'd just be nice. :) The ?? operator is not needed. But it's a great shortcut. The foreach construct isn't needed. But it's a great shortcut (you could do the same thing with a while loop, checking whether the MoveNext method of the enumerator returns false). Same with the idea of tuples. I'd rather be able to return 3 or 4 values than have to deal with the messiness of out parameters, or having to define multiple structs to handle each return combination that I might need.

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

                                    J Offline
                                    J Offline
                                    James Lonero
                                    wrote on last edited by
                                    #157

                                    We have the anonymous type. Create an anonymous type structure in your method and return it to a var local variable.

                                    1 Reply Last reply
                                    0
                                    • V Vikram A Punathambekar

                                      Thanks for the explanation, but how would you implement a runtime check? And what would it do - throw an exception that the called code has changed the object despite its (earlier) promise not to?

                                      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 Offline
                                      S Offline
                                      S Senthil Kumar
                                      wrote on last edited by
                                      #158

                                      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 1 Reply Last reply
                                      0
                                      • S Sunny Ahuwanya

                                        Pawel Krakowiak wrote:

                                        I think of them as of an improvement and use them. Smile

                                        Can anyone explain to me how extension methods are an improvement? Besides helping to sell LINQ and encouraging programmers to write code in a non-portable, non object oriented manner, what is the point of extension methods?

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

                                        Who says object-orientation is the sole valid way to write code? Object-orientation can get in the way when something simpler would suffice to get the job done. Extension methods allow you to add a great deal of expressiveness to your code, extend base types you don't have direct control of to provide a cleaner API, etc. etc. Whats with all the hostility towards useful language features and non-object-orientedness? Objects don't solve every problem. Portability? Where are you going to "port" a C# or VB.NET app to...they only run on the .NET platform anyway. Use the features for what they are. As a programmer, your job is to provide solutions to business problems in the most effective, efficient, maintainable way possible...don't get so hung up on all the "rules" and "regulations" of OOP...objects arn't the only option.

                                        J S 2 Replies Last reply
                                        0
                                        • P PIEBALDconsult

                                          Pawel Krakowiak wrote:

                                          add new functionality to the existing classes

                                          They do nothing of the sort!

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

                                          They absolutely do! Look at IEnumerable...EVERY type that implements IEnumerable DOES INDEED get the full functionality of all extension methods written for IEnumerable (so long as the proper using statement(s) are included). You don't have to "implement" the extension methods on each class that implements IEnumerable...the extension methods absolutely do add new functionality without you having to do any extra implementation. And that functionality is accessed through class INSTANCES...not static methods or utility types. You anti-progressive types drive me nuts sometimes...get off your high horse and USE the tools at your disposal. You might find that you actually like them.

                                          P P S 3 Replies 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