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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. const parameters in c#

const parameters in c#

Scheduled Pinned Locked Moved C#
csharpc++helpquestioncareer
7 Posts 4 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.
  • J Offline
    J Offline
    Joel Holdsworth
    wrote on last edited by
    #1

    class MyClass
    {
    public void Foo(const int param)
    {
    // ... ^^^^^ compile error here
    }
    }

    This type of statment works perfectly well in C++, but a const param seems to confuse the C# compiler a bit. It says "Type Expected". How is it possible pass a constant / read only parameter in C#? Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

    H 1 Reply Last reply
    0
    • J Joel Holdsworth

      class MyClass
      {
      public void Foo(const int param)
      {
      // ... ^^^^^ compile error here
      }
      }

      This type of statment works perfectly well in C++, but a const param seems to confuse the C# compiler a bit. It says "Type Expected". How is it possible pass a constant / read only parameter in C#? Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      const is not allowed in the parameter list. You should read the C# Language Specification[^]. IIRC, a constant parameter declaration isn't even allowed by the CLI (Common Language Infrastructure).

      Microsoft MVP, Visual C# My Articles

      J 1 Reply Last reply
      0
      • H Heath Stewart

        const is not allowed in the parameter list. You should read the C# Language Specification[^]. IIRC, a constant parameter declaration isn't even allowed by the CLI (Common Language Infrastructure).

        Microsoft MVP, Visual C# My Articles

        J Offline
        J Offline
        Joel Holdsworth
        wrote on last edited by
        #3

        Putting /*[in]*/ before each parameter seems to stop them being changable at compile time e.g.

        public void foo(/*[in]*/ int foo)
        {
        // ....
        }

        - but I get the impression that this is a bit of a cludged way of getting the results. I think I'll just rethink how I do this. Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

        H B 2 Replies Last reply
        0
        • J Joel Holdsworth

          Putting /*[in]*/ before each parameter seems to stop them being changable at compile time e.g.

          public void foo(/*[in]*/ int foo)
          {
          // ....
          }

          - but I get the impression that this is a bit of a cludged way of getting the results. I think I'll just rethink how I do this. Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Interesting if true; I've never seen that documented. It would definitely be a compiler feature, though, and have no bearing in the actual code. Keep in mind that an Int32 is a value type and is therefore passed by value. This won't prevent your code from changing it, but if your method implementation does it won't affect the variable that was passed to you (if a variable was indeed passed to your method). Since your method isn't virtual (and hence can't be overridden), just make sure you don't change it. I know that's not a lot of protection, but declaring an argument as constant is really just to avoid future headaches if you forget that you shouldn't change it, IMO.

          Microsoft MVP, Visual C# My Articles

          J 1 Reply Last reply
          0
          • H Heath Stewart

            Interesting if true; I've never seen that documented. It would definitely be a compiler feature, though, and have no bearing in the actual code. Keep in mind that an Int32 is a value type and is therefore passed by value. This won't prevent your code from changing it, but if your method implementation does it won't affect the variable that was passed to you (if a variable was indeed passed to your method). Since your method isn't virtual (and hence can't be overridden), just make sure you don't change it. I know that's not a lot of protection, but declaring an argument as constant is really just to avoid future headaches if you forget that you shouldn't change it, IMO.

            Microsoft MVP, Visual C# My Articles

            J Offline
            J Offline
            Joel Holdsworth
            wrote on last edited by
            #5

            Well actually it's not an Int32 - its actually an object which I cooked up myself. Still it seems a bit wierd to not allow const or readonly parameters because, just as you say, they always help me avoid playing around with data which should be left untouched! Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

            L 1 Reply Last reply
            0
            • J Joel Holdsworth

              Well actually it's not an Int32 - its actually an object which I cooked up myself. Still it seems a bit wierd to not allow const or readonly parameters because, just as you say, they always help me avoid playing around with data which should be left untouched! Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Joel Holdsworth wrote: Still it seems a bit wierd to not allow const or readonly parameters because, just as you say, they always help me avoid playing around with data which should be left untouched! I agree, alot of errors can be detected at compile time. But that said, comin with generics we should be able to do this:

              struct ConstValue<Type T>
              {
              readonly T value;

              ConstValue<T>(T obj)
              {
              value = obj;
              }
              static implicit operator ConstValue<T> (T obj)
              {
              return new ConstValue<T>(cobj);
              }
              }

              Which will serve the same purpose. top secret xacc-ide 0.0.1

              1 Reply Last reply
              0
              • J Joel Holdsworth

                Putting /*[in]*/ before each parameter seems to stop them being changable at compile time e.g.

                public void foo(/*[in]*/ int foo)
                {
                // ....
                }

                - but I get the impression that this is a bit of a cludged way of getting the results. I think I'll just rethink how I do this. Joel Holdsworth Wanna give me a job over the summer? View my online CV and Job Application[^]

                B Offline
                B Offline
                Baris Kurtlutepe
                wrote on last edited by
                #7

                Joel Holdsworth wrote: Putting /*[in]*/ before each parameter seems to stop them being changable at compile time No it doesn't, or better said: Thank god it doesn't. Thinking of crawling through all comments in the code just to see what other 'features' the compiler might put into the code gives me the creeps.

                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