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. What Language Features Do You Miss In C#?

What Language Features Do You Miss In C#?

Scheduled Pinned Locked Moved The Lounge
csharpjavascriptcomquestiondiscussion
102 Posts 36 Posters 0 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.
  • M Marach

    you can type several 'case' statements one after the other. contrived example:

    int number = GetNumberBetween1And5();
    string text = null;
    switch( number ) {
    case 1:
    text = "1";
    break;
    case 2:
    case 3:
    case 4:
    text = "2 or 3 or 4";
    break;
    case 5:
    text = "5";
    break;
    }

    is that type of fall-through what you said you're missing?

    M Offline
    M Offline
    Marc A Brown
    wrote on last edited by
    #46

    I already knew about (and use) that type of fallthrough (but thanks for pointing it out anyway). I'm talking about a case (no pun intended) where you have an action to perform in two cases that requires some kind of setup in one of the cases but not the other.

    switch(whichAction)
    {
    case Actions.ActionWithSetup:
    DoSetup();
    case Actions.Action:
    DoAction();
    break;
    }

    In this example, in the one case, DoSetup is performed, followed by DoAction; in the other case only DoAction is performed. You can do this in C (and Java as I recall) but not in C#. I'm fine with the language not allowing the fallthrough to happen unintentionally but think there should be a keyword to allow it. For example:

    switch(whichAction)
    {
    case Actions.ActionWithSetup:
    DoSetup();
    nobreak;
    case Actions.Action:
    DoAction();
    break;
    }

    I realize that in my example I could simply call DoAction in both cases (and that's what I would do, given the C# limitation); and I also understand that if I've got a block of code in the second case, I can break it out into a separate method and call that method in both cases (which again is what I would do); however, if it's a really small block of code, I don't necessarily want to create a new method for it or duplicate the code.

    M P 2 Replies Last reply
    0
    • M Marc A Brown

      I already knew about (and use) that type of fallthrough (but thanks for pointing it out anyway). I'm talking about a case (no pun intended) where you have an action to perform in two cases that requires some kind of setup in one of the cases but not the other.

      switch(whichAction)
      {
      case Actions.ActionWithSetup:
      DoSetup();
      case Actions.Action:
      DoAction();
      break;
      }

      In this example, in the one case, DoSetup is performed, followed by DoAction; in the other case only DoAction is performed. You can do this in C (and Java as I recall) but not in C#. I'm fine with the language not allowing the fallthrough to happen unintentionally but think there should be a keyword to allow it. For example:

      switch(whichAction)
      {
      case Actions.ActionWithSetup:
      DoSetup();
      nobreak;
      case Actions.Action:
      DoAction();
      break;
      }

      I realize that in my example I could simply call DoAction in both cases (and that's what I would do, given the C# limitation); and I also understand that if I've got a block of code in the second case, I can break it out into a separate method and call that method in both cases (which again is what I would do); however, if it's a really small block of code, I don't necessarily want to create a new method for it or duplicate the code.

      M Offline
      M Offline
      Marach
      wrote on last edited by
      #47

      Thanks for elaborating, I understand now. And I agree that C# language should have some construct like the suggested one to allow the fall-through.

      1 Reply Last reply
      0
      • A AspDotNetDev

        Inline assembler.

        Thou mewling ill-breeding pignut!

        T Offline
        T Offline
        Tim Schwallie
        wrote on last edited by
        #48

        I believe that's been there since version 1.0

        A 1 Reply Last reply
        0
        • S Steve Wellens

          sprintf sscanf

          Steve Wellens

          G Offline
          G Offline
          glennPattonWork3
          wrote on last edited by
          #49

          Amen, would make life easier at the moment! mind you can get them from C++ Glenn

          1 Reply Last reply
          0
          • C Chris C B

            On error resume next

            G Offline
            G Offline
            glennPattonWork3
            wrote on last edited by
            #50

            VB error handling, really?

            1 Reply Last reply
            0
            • K Kent Sharkey

              Sure, it's on another discussion site[^], but that doesn't mean we can't also discuss it here. Personally, while it certainly doesn't fit in the "missing" category, I see them moving it closer and closer to a hybrid C#/JavaScript language with each new version.

              -------------- TTFN - Kent

              T Offline
              T Offline
              Tim Schwallie
              wrote on last edited by
              #51

              Enum that supports other types, not just integral types. A required tag on the end of block regions. Why? Cause it can become a bitch knowing which '}' belongs with what after a while and folks are too lazy to comment. Or VS could get better and put a comment there for you. But then this isn't about VS. Improved Friend relationships between DLL's. Current way of setting up Friend relationships is awkward.

              1 Reply Last reply
              0
              • S Sentenryu

                why the heck you just don't use a generic method?

                public void DoGenericStuff(Foo foo)
                {
                ....
                }

                I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241

                P Offline
                P Offline
                Paulo Zemek
                wrote on last edited by
                #52

                Hahahahaha. You didn't get it at all. Imagine that for some reason you don't know the T type at compile-time. You are in an event or something and you are receiving a valid value, but it is cast as object. How do you do a call to the generic class without knowing its generic type? In such case, it is up to you to do the cast, but you don't have the destination type at compile time. The solution in such situation is to use reflection or dynamic. I solve the problem having an untyped interface. That works very well for my classes, but not to already existing classes. Even if it is not a generic class situation, you can see that happening with database connections. You have SqlConnection, OracleConnection, SqlCommand, OracleCommand, SqlParameter, OracleParameter and so on. But you can use all of them al IDbConnection, IDbCommand and so on. So, you create a parameter using the command... you dont know if it is an OracleParameter or SqlParameter... but it is not important, as when you add a parameter to a command the driver do the cast for you. (Ok, it is a little stupid that you create the parameter and it is not added automatically... but I want to ilustrate a situation where you have a valid value [the parameter] but you don't have the valid type).

                S 1 Reply Last reply
                0
                • P PIEBALDconsult

                  A goto case is not a goto. But, yes, I don't like break in a switch; in my opinion break should only be for loops only.

                  F Offline
                  F Offline
                  Florian Rappl
                  wrote on last edited by
                  #53

                  I do not really agree, since case statements form a valid label and goto LABEL is a real goto. Why was goto brought to C (and therefore to C++, C#, ...) anyway? It is REALLY simple to implement goto in C (for transferring into assembler). Its jut a plain JMP. Labels can be transferred nearly 1-1, so having a goto the way it has been introduced to C makes sense. switch-case statements have just a list of JE statements, i.e. using goto is the way to use those labels outside of the usual flow. Here is how switch-case results in MSIL:

                  IL_0001: ldc.i4.5
                  IL_0002: stloc.0 // a
                  IL_0003: ldloc.0 // a
                  IL_0004: stloc.1 // CS$4$0000
                  IL_0005: ldloc.1 // CS$4$0000
                  IL_0006: switch (IL_0015, IL_0022)
                  IL_0013: br.s IL_002F
                  IL_0015: ldstr "Zero"
                  IL_001A: call LINQPad.Extensions.Dump
                  IL_001F: pop
                  IL_0020: br.s IL_003C
                  IL_0022: ldstr "Zero"
                  IL_0027: call LINQPad.Extensions.Dump
                  IL_002C: pop
                  IL_002D: br.s IL_003C
                  IL_002F: ldstr "Nothing"
                  IL_0034: call LINQPad.Extensions.Dump
                  IL_0039: pop
                  IL_003A: br.s IL_003C

                  The line IL_0006 will result in the list of JE statements. The following program was used to produce these lines of IL code:

                  void Main()
                  {
                  var a = 5;

                  switch(a)
                  {
                  	case 0:
                  		"Zero".Dump();
                  		break;
                  	case 1:
                  		"One".Dump();
                  		break;
                  	default:
                  		"Non-Zero".Dump();
                  		break;
                  }
                  

                  }

                  If you would now compare this to usual labels you would see that both are identical. It's really just a syntax thing that case statements start with case, hence since one has always to specify the full label, the case needs to be included for any goto call on those labels.

                  P 1 Reply Last reply
                  0
                  • K Kent Sharkey

                    Sure, it's on another discussion site[^], but that doesn't mean we can't also discuss it here. Personally, while it certainly doesn't fit in the "missing" category, I see them moving it closer and closer to a hybrid C#/JavaScript language with each new version.

                    -------------- TTFN - Kent

                    J Offline
                    J Offline
                    jaquadro
                    wrote on last edited by
                    #54

                    - Extension properties and operators. - An analogue to the ?? operator that returns null if any object in a chain of lookups is null, e.g. var furniture = house.livingroom.sofa without having to null-check every step. - Return type covariance. - typedef support, mainly to alias a pervasively used ugly generic type without needing to put a using statement in every file. More useful for value types that can't be subclassed to achieve similar ends.

                    1 Reply Last reply
                    0
                    • C Chris C B

                      On error resume next

                      K Offline
                      K Offline
                      Kent Sharkey
                      wrote on last edited by
                      #55

                      Yes! And gosub.

                      -------------- TTFN - Kent

                      G 1 Reply Last reply
                      0
                      • P Paulo Zemek

                        Hahahahaha. You didn't get it at all. Imagine that for some reason you don't know the T type at compile-time. You are in an event or something and you are receiving a valid value, but it is cast as object. How do you do a call to the generic class without knowing its generic type? In such case, it is up to you to do the cast, but you don't have the destination type at compile time. The solution in such situation is to use reflection or dynamic. I solve the problem having an untyped interface. That works very well for my classes, but not to already existing classes. Even if it is not a generic class situation, you can see that happening with database connections. You have SqlConnection, OracleConnection, SqlCommand, OracleCommand, SqlParameter, OracleParameter and so on. But you can use all of them al IDbConnection, IDbCommand and so on. So, you create a parameter using the command... you dont know if it is an OracleParameter or SqlParameter... but it is not important, as when you add a parameter to a command the driver do the cast for you. (Ok, it is a little stupid that you create the parameter and it is not added automatically... but I want to ilustrate a situation where you have a valid value [the parameter] but you don't have the valid type).

                        S Offline
                        S Offline
                        Sentenryu
                        wrote on last edited by
                        #56

                        if you know the interface you can always cast to IFoo. thanks to Covariance[^]

                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241

                        P 1 Reply Last reply
                        0
                        • S Steve Wellens

                          sprintf sscanf

                          Steve Wellens

                          S Offline
                          S Offline
                          Sentenryu
                          wrote on last edited by
                          #57

                          I don't know about sscanf, but String.Format() doesn't do the trick for when you need sprintf?

                          I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241

                          1 Reply Last reply
                          0
                          • T thrakazog

                            My answer is always multiple inheritance. Times that I would use it are rare. But when those times come up man do I ever want that.

                            Play my game Gravity: IOS[^], Android[^], Windows Phone 7[^]

                            A Offline
                            A Offline
                            Alan Balkany
                            wrote on last edited by
                            #58

                            I agree. Multiple inheritance can be an elegant, simple, effective solution to many design problems. It's left out because there are contrived cases where it produces complexity, but these are just bad programming.

                            1 Reply Last reply
                            0
                            • S Sentenryu

                              if you know the interface you can always cast to IFoo. thanks to Covariance[^]

                              I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241

                              P Offline
                              P Offline
                              Paulo Zemek
                              wrote on last edited by
                              #59

                              That works for IEnumerable but not for IList (to show a limitation). In fact, if you need to give parameters to the Foo object (in the command case, you add a parameter to the command) it can at maximum be an "in T", so you can't use IFoo<object> Also, even "out T" will not work which structs. Try to get an IEnumerator<int> as an IEnumerator<object>. It does not work. But for such situation there is the non-generic that works in a more generic way... you can use an IEnumerator without a generic parameter. In such case there is such interface, but that's not always the case. And as a note... using IFoo<object> is contravariance, not covariance.

                              1 Reply Last reply
                              0
                              • K Kent Sharkey

                                Sure, it's on another discussion site[^], but that doesn't mean we can't also discuss it here. Personally, while it certainly doesn't fit in the "missing" category, I see them moving it closer and closer to a hybrid C#/JavaScript language with each new version.

                                -------------- TTFN - Kent

                                A Offline
                                A Offline
                                Alan Balkany
                                wrote on last edited by
                                #60

                                The ability to look at a variable's memory location while in another part of the program. In C++, I sometimes use what I call the "Stakeout Debugging Pattern": I create a Watch expression on the address of a variable, so I can see how it changes while not in scope. (The debugger won't show variables not in scope). E.g. *(int *)0x12345678 But there's no way (that I know of) to do this in C#. This would help with debugging.

                                Sander RosselS 1 Reply Last reply
                                0
                                • K Kent Sharkey

                                  Sure, it's on another discussion site[^], but that doesn't mean we can't also discuss it here. Personally, while it certainly doesn't fit in the "missing" category, I see them moving it closer and closer to a hybrid C#/JavaScript language with each new version.

                                  -------------- TTFN - Kent

                                  S Offline
                                  S Offline
                                  svella
                                  wrote on last edited by
                                  #61

                                  Gonna be controversial, but Checked Exceptions.

                                  1 Reply Last reply
                                  0
                                  • M Marc Clifton

                                    Multiple inheritance. Interfaces are useful as abstractions, but there are times I want to inherit concrete functionality from multiple classes. Marc

                                    Latest Article: C# and Ruby Classes: A Deep Dive
                                    My Blog

                                    P Offline
                                    P Offline
                                    Paulo Zemek
                                    wrote on last edited by
                                    #62

                                    I completely agree. Even if we can simulate multiple inheritance with interfaces and extension methods, well, extension methods are ugly, require a special using to be available and can't be virtual. So, multiple inheritance will be great. And if there are persons that use it incorrectly, well... there are persons that do all kinds of stupid things.

                                    1 Reply Last reply
                                    0
                                    • K Kent Sharkey

                                      Sure, it's on another discussion site[^], but that doesn't mean we can't also discuss it here. Personally, while it certainly doesn't fit in the "missing" category, I see them moving it closer and closer to a hybrid C#/JavaScript language with each new version.

                                      -------------- TTFN - Kent

                                      J Offline
                                      J Offline
                                      Jasmine2501
                                      wrote on last edited by
                                      #63

                                      THROWS, as an optional declaration. I would like the following... public void myfunction(int x) throws ArgumentException, OverflowException { } And, Java needs the other form of throws too, which I'd like to see in C# public void myfunction(int x) throws NONE { } ... for methods which can't throw exceptions.

                                      P 1 Reply Last reply
                                      0
                                      • T Tim Schwallie

                                        I believe that's been there since version 1.0

                                        A Offline
                                        A Offline
                                        AspDotNetDev
                                        wrote on last edited by
                                        #64

                                        Riiiiight...

                                        Thou mewling ill-breeding pignut!

                                        T 1 Reply Last reply
                                        0
                                        • S Single Step Debugger

                                          This one is type dependent and not a language future but some class method. Same as Split() (very useful method by the way) or IsNullOrEmpty() for example.

                                          There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                                          A Offline
                                          A Offline
                                          AspDotNetDev
                                          wrote on last edited by
                                          #65

                                          What does it matter that it's a framework implementation and not implemented by the C# specification? Just as long as it accomplishes the same thing.

                                          Thou mewling ill-breeding pignut!

                                          S 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