const functions in C# ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    const functions in C# ?

    Hi,

    can you define const-functions in C# as in C++ ?

    for example (C++-code) :

    int Cube :: GetSide() const
    {
    return m_side;
    }

    Thnx

    Christian


  • Jon Skeet [C# MVP]

    #2
    Re: const functions in C# ?

    Chris <christianc@pan dora.be> wrote:[color=blue]
    > can you define const-functions in C# as in C++ ?
    >
    > for example (C++-code) :
    >
    > int Cube :: GetSide() const
    > {
    > return m_side;
    > }[/color]

    No. I don't know whether or not it's being considered. Const-ness can
    be very useful, but it does require a fair amount of discipline.

    You may well be interested in http://constcli.sscli.net/

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Guest's Avatar

      #3
      Re: const functions in C# ?

      What is the benifit of a const funtion ?


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a23e1 c027b29665989ac 6@msnews.micros oft.com...[color=blue]
      > Chris <christianc@pan dora.be> wrote:[color=green]
      > > can you define const-functions in C# as in C++ ?
      > >
      > > for example (C++-code) :
      > >
      > > int Cube :: GetSide() const
      > > {
      > > return m_side;
      > > }[/color]
      >
      > No. I don't know whether or not it's being considered. Const-ness can
      > be very useful, but it does require a fair amount of discipline.
      >
      > You may well be interested in http://constcli.sscli.net/
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: const functions in C# ?

        <discussion@dis cussion.microso ft.com> wrote:[color=blue]
        > What is the benifit of a const funtion ?[/color]

        On its own, not a lot - but when combined with the idea of a const
        object or variable, it means you can have more safety. For instance,
        suppose you maintain a byte array within a class, and you wish to make
        that array available in a "read-only" kind of way - you could expose it
        with a "const" property, which would mean that clients would have to
        do:

        const byte[] foo = myObject.MyProp erty;

        instead of

        byte[] foo = myObject.MyProp erty;

        They then couldn't do

        foo[0] = 5;

        but they could do

        byte x = foo[0];

        It means you can basically return references to mutable objects, but
        with the "const" modifier meaning that the client could only call
        methods marked as "const" as well. (So for instance, in StringBuilder
        ToString might be marked as "const", but Append wouldn't.)

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Guest's Avatar

          #5
          Re: const functions in C# ?

          isnt that why we have the readonly keyword? Setonce then its locked from
          setting.



          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1a23e7 39e507279d989ac 7@msnews.micros oft.com...[color=blue]
          > <discussion@dis cussion.microso ft.com> wrote:[color=green]
          > > What is the benifit of a const funtion ?[/color]
          >
          > On its own, not a lot - but when combined with the idea of a const
          > object or variable, it means you can have more safety. For instance,
          > suppose you maintain a byte array within a class, and you wish to make
          > that array available in a "read-only" kind of way - you could expose it
          > with a "const" property, which would mean that clients would have to
          > do:
          >
          > const byte[] foo = myObject.MyProp erty;
          >
          > instead of
          >
          > byte[] foo = myObject.MyProp erty;
          >
          > They then couldn't do
          >
          > foo[0] = 5;
          >
          > but they could do
          >
          > byte x = foo[0];
          >
          > It means you can basically return references to mutable objects, but
          > with the "const" modifier meaning that the client could only call
          > methods marked as "const" as well. (So for instance, in StringBuilder
          > ToString might be marked as "const", but Append wouldn't.)
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: const functions in C# ?

            <discussion@dis cussion.microso ft.com> wrote:[color=blue]
            > isnt that why we have the readonly keyword? Setonce then its locked from
            > setting.[/color]

            No, readonly will make the *variable* readonly. It says nothing about
            the object that the reference in that variable refers to (which is why
            you can't mark a method as returning a "readonly" reference).

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • 100

              #7
              Re: const functions in C# ?

              Hi Jon,
              This is not the idea behind the const members of a class in c++. What you
              are talking about is method returning const object.
              When a method is declared as *const* (in a way Chris did it in his original
              post) that means that this method won't modify anything, but its local
              variables nor call any function which is not declared as *const* but it has
              nothing to do with the constness of the returned object. It just guarantee
              that the method itself will not change anything.

              So to answer Chris's question: No, C# doesn't have *const* methods.

              B\rgds
              100

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1a23e7 39e507279d989ac 7@msnews.micros oft.com...[color=blue]
              > <discussion@dis cussion.microso ft.com> wrote:[color=green]
              > > What is the benifit of a const funtion ?[/color]
              >
              > On its own, not a lot - but when combined with the idea of a const
              > object or variable, it means you can have more safety. For instance,
              > suppose you maintain a byte array within a class, and you wish to make
              > that array available in a "read-only" kind of way - you could expose it
              > with a "const" property, which would mean that clients would have to
              > do:
              >
              > const byte[] foo = myObject.MyProp erty;
              >
              > instead of
              >
              > byte[] foo = myObject.MyProp erty;
              >
              > They then couldn't do
              >
              > foo[0] = 5;
              >
              > but they could do
              >
              > byte x = foo[0];
              >
              > It means you can basically return references to mutable objects, but
              > with the "const" modifier meaning that the client could only call
              > methods marked as "const" as well. (So for instance, in StringBuilder
              > ToString might be marked as "const", but Append wouldn't.)
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: const functions in C# ?

                100 <100@100.com> wrote:[color=blue]
                > This is not the idea behind the const members of a class in c++. What you
                > are talking about is method returning const object.
                > When a method is declared as *const* (in a way Chris did it in his original
                > post) that means that this method won't modify anything, but its local
                > variables nor call any function which is not declared as *const* but it has
                > nothing to do with the constness of the returned object. It just guarantee
                > that the method itself will not change anything.[/color]

                But there are two things here - the method could be declared not to
                change anything in the current object, *and* it could also be declared
                to return a reference to an object which then can't be changed by the
                client. In other words, if something returns const Foo, then only the
                methods declared as const within Foo are available.

                I don't think you get anything like the full benefit without both
                halves.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Chris

                  #9
                  Re: const functions in C# ?

                  >"When a method is declared as *const* (in a way Chris did it in his
                  original[color=blue]
                  >post) that means that this method won't modify anything but its local
                  >variables, nor call any function which is not declared as *const* "[/color]

                  That was my idea indeed about the const-function.

                  So, unfortunately it doesn't exits in C# ? too bad.

                  thanks to you all for your time though !!

                  Christian

                  "100" <100@100.com> wrote in message
                  news:Os12pserDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > Hi Jon,
                  > This is not the idea behind the const members of a class in c++. What you
                  > are talking about is method returning const object.
                  > When a method is declared as *const* (in a way Chris did it in his[/color]
                  original[color=blue]
                  > post) that means that this method won't modify anything, but its local
                  > variables nor call any function which is not declared as *const* but it[/color]
                  has[color=blue]
                  > nothing to do with the constness of the returned object. It just guarantee
                  > that the method itself will not change anything.
                  >
                  > So to answer Chris's question: No, C# doesn't have *const* methods.
                  >
                  > B\rgds
                  > 100
                  >
                  > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  > news:MPG.1a23e7 39e507279d989ac 7@msnews.micros oft.com...[color=green]
                  > > <discussion@dis cussion.microso ft.com> wrote:[color=darkred]
                  > > > What is the benifit of a const funtion ?[/color]
                  > >
                  > > On its own, not a lot - but when combined with the idea of a const
                  > > object or variable, it means you can have more safety. For instance,
                  > > suppose you maintain a byte array within a class, and you wish to make
                  > > that array available in a "read-only" kind of way - you could expose it
                  > > with a "const" property, which would mean that clients would have to
                  > > do:
                  > >
                  > > const byte[] foo = myObject.MyProp erty;
                  > >
                  > > instead of
                  > >
                  > > byte[] foo = myObject.MyProp erty;
                  > >
                  > > They then couldn't do
                  > >
                  > > foo[0] = 5;
                  > >
                  > > but they could do
                  > >
                  > > byte x = foo[0];
                  > >
                  > > It means you can basically return references to mutable objects, but
                  > > with the "const" modifier meaning that the client could only call
                  > > methods marked as "const" as well. (So for instance, in StringBuilder
                  > > ToString might be marked as "const", but Append wouldn't.)
                  > >
                  > > --
                  > > Jon Skeet - <skeet@pobox.co m>
                  > > http://www.pobox.com/~skeet
                  > > If replying to the group, please do not mail me too[/color]
                  >
                  >[/color]


                  Comment

                  • 100

                    #10
                    Re: const functions in C# ?

                    > I don't think you get anything like the full benefit without both[color=blue]
                    > halves.[/color]

                    Disagree. The fact that I'm calling *const* doesn't necessarily mean that I
                    don't want to change the returned object as well as the fact that the class
                    provides a method that is not *const* doesn't necessarily mean that the
                    returned value has to be changeable.
                    For example Clone method can be decalred as *const*. As user of this method
                    it gives me peace of
                    mind that this method won't change anything. As programmer it helps me to
                    avoid errors. Anyway it returns copy that the user can change.
                    Or if operators -- and ++ were prototyped as *const* methods. Errors like
                    "not creating new object but altering the one passed via method parameter"
                    will not occur. It doesn't mean though that the returned object cannot be
                    changed.

                    B\rgds
                    100


                    Comment

                    • Frank Oquendo

                      #11
                      Re: const functions in C# ?

                      100 wrote:
                      [color=blue]
                      > Disagree. The fact that I'm calling *const* doesn't necessarily mean
                      > that I don't want to change the returned object as well as the fact
                      > that the class provides a method that is not *const* doesn't
                      > necessarily mean that the returned value has to be changeable.[/color]

                      But isn't the whole point of const methods and methods returning const
                      references to allow that decision to be made by the designer, not the
                      user?

                      --
                      There are 10 kinds of people. Those who understand binary and those who
                      don't.


                      (Pull the pin to reply)


                      Comment

                      • 100

                        #12
                        Re: const functions in C# ?

                        Hi Frank,
                        [color=blue][color=green]
                        > > Disagree. The fact that I'm calling *const* doesn't necessarily mean
                        > > that I don't want to change the returned object as well as the fact
                        > > that the class provides a method that is not *const* doesn't
                        > > necessarily mean that the returned value has to be changeable.[/color]
                        >
                        > But isn't the whole point of const methods and methods returning const
                        > references to allow that decision to be made by the designer, not the
                        > user?[/color]
                        Returning *const* object the designer force not changing the object. But my
                        point is that const method has nothing to do with returning const object.
                        The retuned object from *const* method might be *const*, but doesn't have to
                        be.

                        From the users point of view method declared as *const* has more informative
                        meaning.

                        For method returning *const* - Yes, designer makes the desicion. And that is
                        what Jon was talking about.
                        But the question was about the *const* methods. And with *const* methods
                        designer doesn't make any desicions about the returned object.

                        B\rgds
                        100


                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: const functions in C# ?

                          100 <100@100.com> wrote:[color=blue][color=green]
                          > > I don't think you get anything like the full benefit without both
                          > > halves.[/color]
                          >
                          > Disagree. The fact that I'm calling *const* doesn't necessarily mean that I
                          > don't want to change the returned object as well as the fact that the class
                          > provides a method that is not *const* doesn't necessarily mean that the
                          > returned value has to be changeable.[/color]

                          You don't have to have both halves in the same *method*, and I never
                          suggested that you did.
                          [color=blue]
                          > For example Clone method can be decalred as *const*. As user of this method
                          > it gives me peace of mind that this method won't change anything.[/color]

                          That can be done with documentation though - the main benefit of
                          constness (IMO) is getting the compiler to notice that something you've
                          said won't change genuinely won't, and making sure you don't try to
                          change something you shouldn't do.
                          [color=blue]
                          > As programmer it helps me to avoid errors.[/color]

                          How, if the compiler isn't going to enforce anything? Without both
                          halves, the compiler hasn't *got* anything to enforce.
                          [color=blue]
                          > Anyway it returns copy that the user can change.
                          > Or if operators -- and ++ were prototyped as *const* methods. Errors like
                          > "not creating new object but altering the one passed via method parameter"
                          > will not occur. It doesn't mean though that the returned object cannot be
                          > changed.[/color]

                          No, and that's why I've said, they're separate things - but I *do*
                          believe you don't get much benefit unless both parts are available.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: const functions in C# ?

                            100 <100@100.com> wrote:[color=blue]
                            > Returning *const* object the designer force not changing the object. But my
                            > point is that const method has nothing to do with returning const object.[/color]

                            Yes it does - because when you've had a const object reference returned
                            to you, you should only be able to call const methods.
                            [color=blue]
                            > The retuned object from *const* method might be *const*, but doesn't have to
                            > be.[/color]

                            And no-one's claimed that it does.
                            [color=blue]
                            > From the users point of view method declared as *const* has more informative
                            > meaning.
                            >
                            > For method returning *const* - Yes, designer makes the desicion. And that is
                            > what Jon was talking about.
                            > But the question was about the *const* methods. And with *const* methods
                            > designer doesn't make any desicions about the returned object.[/color]

                            Yes - but any explanation of const methods which doesn't also include
                            how const variables/objects/references (I'm not so hot on the exact
                            terminology here) come about in the first place, which will often
                            include a method returning a const object reference, is incomplete IMO.
                            You seem to think I'm conflating the two ideas, and I'm not - but to
                            say they have "nothing to do" with each other is wrong, IMO.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • 100

                              #15
                              Re: const functions in C# ?

                              > > From the users point of view method declared as *const* has more
                              informative[color=blue][color=green]
                              > > meaning.
                              > >
                              > > For method returning *const* - Yes, designer makes the desicion. And[/color][/color]
                              that is[color=blue][color=green]
                              > > what Jon was talking about.
                              > > But the question was about the *const* methods. And with *const* methods
                              > > designer doesn't make any desicions about the returned object.[/color]
                              >
                              > Yes - but any explanation of const methods which doesn't also include
                              > how const variables/objects/references (I'm not so hot on the exact
                              > terminology here) come about in the first place, which will often
                              > include a method returning a const object reference, is incomplete IMO.
                              > You seem to think I'm conflating the two ideas, and I'm not - but to
                              > say they have "nothing to do" with each other is wrong, IMO.[/color]

                              If the question was about the const variables or reference to const objects
                              (or in terms of c++ const variables, const members or pointers and
                              references to const objects) - yes, you should say something about const
                              methods as long as those are the only methods of the object that can be
                              called. But talking about *const* methods we may not mention const
                              variables, etc because those methods may use any type of variables and
                              members. They are not limited to access const fields or (as we both agree)
                              of returning const objects only.

                              B\rgds
                              100
                              [color=blue]
                              > --
                              > Jon Skeet - <skeet@pobox.co m>
                              > http://www.pobox.com/~skeet
                              > If replying to the group, please do not mail me too[/color]


                              Comment

                              Working...