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

C# Irritation

Scheduled Pinned Locked Moved The Lounge
csharpc++comdesignhelp
46 Posts 23 Posters 1 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 Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #1

    So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

    enum 
    

    Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

    The modifier 'readonly' is not valid for this item

    Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

    C H W L S 11 Replies Last reply
    0
    • S Stuart Dootson

      So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

      enum 
      

      Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

      The modifier 'readonly' is not valid for this item

      Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The whole readonly/const thing is a disaster wrapped in a nightmare. Why have a language that passes by reference and not allow const on parameters ? Why use the term 'const' to mean something different to what it does in C++ ? Is somone designing this language, or do they just get drunk and throw darts at a board ? Seriously, Anders is a smart guy, and a nice guy, and that just leaves me all the more confused as to some of the bad decisions being made in C#, I assume there are some trade offs in play, and/or some things that they've just decided are too much work.

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      1 Reply Last reply
      0
      • S Stuart Dootson

        So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

        enum 
        

        Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

        The modifier 'readonly' is not valid for this item

        Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

        H Offline
        H Offline
        hairy_hats
        wrote on last edited by
        #3

        They still haven't produced a coordinate system where Y increases as you go up. This means that polar coordinates rotate the wrong way around the origin. How difficult can it be? They don't have X increasing to the left so why have Y increasing downwards? It's not difficult, other systems (e.g. RiscOS) have done it the right way up for years.

        S J J 3 Replies Last reply
        0
        • S Stuart Dootson

          So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

          enum 
          

          Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

          The modifier 'readonly' is not valid for this item

          Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

          W Offline
          W Offline
          wout de zeeuw
          wrote on last edited by
          #4

          You need some anger management. :laugh:

          Wout

          H G P 3 Replies Last reply
          0
          • S Stuart Dootson

            So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

            enum 
            

            Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

            The modifier 'readonly' is not valid for this item

            Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

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

            Why do you need it though? If you're not going to modify it anyway, then not making it const will not change anything and if you Are then it's just wrong IIRC readonly fields can only be assigned to in the constructors, right? And const fields are static constants - some kind of replacement for defines I think From a C++ perspective it may be a shade odd.. but afaik MSIL doesn't have const locals either, so even if you were allowed to write it, the information would just be redirected to the bit bucket (could be wrong though)

            C S 2 Replies Last reply
            0
            • L Lost User

              Why do you need it though? If you're not going to modify it anyway, then not making it const will not change anything and if you Are then it's just wrong IIRC readonly fields can only be assigned to in the constructors, right? And const fields are static constants - some kind of replacement for defines I think From a C++ perspective it may be a shade odd.. but afaik MSIL doesn't have const locals either, so even if you were allowed to write it, the information would just be redirected to the bit bucket (could be wrong though)

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              harold aptroot wrote:

              IIRC readonly fields can only be assigned to in the constructors, right?

              If they are members, yeah. Yes, his overall problem is that C# assumes that this just doesn't matter, and doesn't support it well.

              Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

              1 Reply Last reply
              0
              • S Stuart Dootson

                So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

                enum 
                

                Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

                The modifier 'readonly' is not valid for this item

                Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

                S Offline
                S Offline
                Simon P Stevens
                wrote on last edited by
                #7

                A readonly local would be pointless. the value has got to be stored, so still takes up the same amount of memory. Adding const wouldn't actually change anything. The only benefit it would give is a compiler warning if you tried to change the value. But you shouldn't be changing the value anyway if you want it to be readonly. Once compiled, the const/readonly tag wouldn't make any difference, it would compile to the same thing anyway (just a normal local variable).

                Simon

                G S 2 Replies Last reply
                0
                • H hairy_hats

                  They still haven't produced a coordinate system where Y increases as you go up. This means that polar coordinates rotate the wrong way around the origin. How difficult can it be? They don't have X increasing to the left so why have Y increasing downwards? It's not difficult, other systems (e.g. RiscOS) have done it the right way up for years.

                  S Offline
                  S Offline
                  Simon P Stevens
                  wrote on last edited by
                  #8

                  If we're turning this into a 'bitching about c#' thread, I want to throw in my personal annoyances. Colour is spelt with a u. ;P

                  Simon

                  H P A 4 Replies Last reply
                  0
                  • S Simon P Stevens

                    If we're turning this into a 'bitching about c#' thread, I want to throw in my personal annoyances. Colour is spelt with a u. ;P

                    Simon

                    H Offline
                    H Offline
                    hairy_hats
                    wrote on last edited by
                    #9

                    I'm with you there! When will they produce a proper UK English version of Windows? :mad:

                    P 1 Reply Last reply
                    0
                    • W wout de zeeuw

                      You need some anger management. :laugh:

                      Wout

                      H Offline
                      H Offline
                      hairy_hats
                      wrote on last edited by
                      #10

                      Venting on CP *is* anger management!

                      W 1 Reply Last reply
                      0
                      • S Stuart Dootson

                        So.....I want to write some C# code like this (using const as an indicator of intent, as I would in C++):

                        enum 
                        

                        Of course, as I've already discovered[^], const doesn't work this way - it needs a compile-time constant expression. So I replace it with readonly, as suggested by many and varied splendid CP members, only to get this error:

                        The modifier 'readonly' is not valid for this item

                        Wuh? So I investigate readonly. It can only be used on fields. What the flip? So, Microsoft, you 'design' this language with two (not one) type modifiers indicating a design intent; that an item will not be modified after initialisation. One of them (const) requires the programmer to know what the compiler will be able to calculate at compile time (something the compiler already knows, as it'll quite happily point out to you when you get it wrong), while the other (readonly) has what seems to be a purely arbitrary usage limitation. This is crazy - if I call something const in C++, the compiler knows what I mean and *DOES THE RIGHT THING*. OK, it's only a very small part of the language, I know. I can just use a variable instead. It just ticks me off. Anyway rant over.

                        D Offline
                        D Offline
                        Daniel Grunwald
                        wrote on last edited by
                        #11

                        They aren't type modifiers - C# doesn't have any type modifiers (unless you count array brackets [] or the nullable ?). Modifiers in C# apply to a type member, not to the member's return type. Even in parameters, "ref" is meant to modify the parameter itself, not the parameter's type. Additionally having type modifiers in the language would make the already complex overload resolution and type inference even more complex. C#'s type system is WAY less powerful: - C++ templates, partial specialisation etc. - all together much more powerful than C#'s generics - type modifiers - not existant in C# - multiple inheritance - not existant in C# - constructor/deterministic destructor semantics - not existant in C# (but it's possible with managed code, as C++/CLI demonstrates) - operator overloading - C++'s implementation is way more powerful (operators can have reference arguments, you can overload the assignment operator, ...) So what? They're two different languages. Get over it.

                        S L K 3 Replies Last reply
                        0
                        • H hairy_hats

                          I'm with you there! When will they produce a proper UK English version of Windows? :mad:

                          P Offline
                          P Offline
                          phannon86
                          wrote on last edited by
                          #12

                          No UK-EN version of OSX either I believe, also, I seem to remember them slapping us in the face by having the specific option of "US-English" but no other version of English at all.

                          He who makes a beast out of himself gets rid of the pain of being a man

                          1 Reply Last reply
                          0
                          • W wout de zeeuw

                            You need some anger management. :laugh:

                            Wout

                            G Offline
                            G Offline
                            Gary Wheeler
                            wrote on last edited by
                            #13

                            It's about C#. It is managed.

                            Software Zen: delete this;

                            L 1 Reply Last reply
                            0
                            • D Daniel Grunwald

                              They aren't type modifiers - C# doesn't have any type modifiers (unless you count array brackets [] or the nullable ?). Modifiers in C# apply to a type member, not to the member's return type. Even in parameters, "ref" is meant to modify the parameter itself, not the parameter's type. Additionally having type modifiers in the language would make the already complex overload resolution and type inference even more complex. C#'s type system is WAY less powerful: - C++ templates, partial specialisation etc. - all together much more powerful than C#'s generics - type modifiers - not existant in C# - multiple inheritance - not existant in C# - constructor/deterministic destructor semantics - not existant in C# (but it's possible with managed code, as C++/CLI demonstrates) - operator overloading - C++'s implementation is way more powerful (operators can have reference arguments, you can overload the assignment operator, ...) So what? They're two different languages. Get over it.

                              S Offline
                              S Offline
                              Simon P Stevens
                              wrote on last edited by
                              #14

                              Daniel Grunwald wrote:

                              They're two different languages. Get over it.

                              Very good point, and succinctly put. C# has totally different design goals.

                              Simon

                              J 1 Reply Last reply
                              0
                              • G Gary Wheeler

                                It's about C#. It is managed.

                                Software Zen: delete this;

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

                                and it is more Object oriented

                                The Developer - CEH

                                1 Reply Last reply
                                0
                                • S Simon P Stevens

                                  A readonly local would be pointless. the value has got to be stored, so still takes up the same amount of memory. Adding const wouldn't actually change anything. The only benefit it would give is a compiler warning if you tried to change the value. But you shouldn't be changing the value anyway if you want it to be readonly. Once compiled, the const/readonly tag wouldn't make any difference, it would compile to the same thing anyway (just a normal local variable).

                                  Simon

                                  G Offline
                                  G Offline
                                  Gary Wheeler
                                  wrote on last edited by
                                  #16

                                  In C++ the const keyword designates an item that may be initialized but not modified. If the programmer modifies the value, he'll get a compiler error. It's a way of ensuring that intentions for the value are met. It sounds like C# doesn't offer any consistent way to do that, and the two keywords that would seem to provide it are poorly implemented.

                                  Software Zen: delete this;

                                  1 Reply Last reply
                                  0
                                  • H hairy_hats

                                    Venting on CP *is* anger management!

                                    W Offline
                                    W Offline
                                    wout de zeeuw
                                    wrote on last edited by
                                    #17

                                    True, sharing the pain is is always good.

                                    Wout

                                    V 1 Reply Last reply
                                    0
                                    • L Lost User

                                      Why do you need it though? If you're not going to modify it anyway, then not making it const will not change anything and if you Are then it's just wrong IIRC readonly fields can only be assigned to in the constructors, right? And const fields are static constants - some kind of replacement for defines I think From a C++ perspective it may be a shade odd.. but afaik MSIL doesn't have const locals either, so even if you were allowed to write it, the information would just be redirected to the bit bucket (could be wrong though)

                                      S Offline
                                      S Offline
                                      Stuart Dootson
                                      wrote on last edited by
                                      #18

                                      harold aptroot wrote:

                                      Why do you need it though?

                                      To indicate design intent as much as anything. A lot of my C++ follows a kind of functional approach (i.e. immutable state - I guess it's most like the 'do' notation that Haskell uses for monadic types). I find it makes my code more likely to be correct than modifying state willy-nilly.

                                      1 Reply Last reply
                                      0
                                      • S Simon P Stevens

                                        If we're turning this into a 'bitching about c#' thread, I want to throw in my personal annoyances. Colour is spelt with a u. ;P

                                        Simon

                                        P Offline
                                        P Offline
                                        Phil J Pearson
                                        wrote on last edited by
                                        #19

                                        I don't think it's fair to blame C# for the misspelling; it's really the fault of the framework. Any language targetting the framework would have the same problem. Having said that ... if I was writing the language I'd make seamlessly correcting human-language mismatches a part of the spec. :-\

                                        Phil


                                        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                                        D 1 Reply Last reply
                                        0
                                        • D Daniel Grunwald

                                          They aren't type modifiers - C# doesn't have any type modifiers (unless you count array brackets [] or the nullable ?). Modifiers in C# apply to a type member, not to the member's return type. Even in parameters, "ref" is meant to modify the parameter itself, not the parameter's type. Additionally having type modifiers in the language would make the already complex overload resolution and type inference even more complex. C#'s type system is WAY less powerful: - C++ templates, partial specialisation etc. - all together much more powerful than C#'s generics - type modifiers - not existant in C# - multiple inheritance - not existant in C# - constructor/deterministic destructor semantics - not existant in C# (but it's possible with managed code, as C++/CLI demonstrates) - operator overloading - C++'s implementation is way more powerful (operators can have reference arguments, you can overload the assignment operator, ...) So what? They're two different languages. Get over it.

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

                                          Yes, the rationale for C++ is increasingly "if you can't do it in anything else, you can do it in C++." And IMO C++ ought to be relegated to such uses.

                                          Kevin

                                          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