4

We have const and non-const function overloading in C++ as described here and used in STL iterators.

Do we have such method overloading in Java and C#?

Community
  • 1
  • 1
zeropoint
  • 235
  • 1
  • 4
  • 10

2 Answers2

6

Java and C# don't have the concept of const functions, so the concept of overloading by const/non-const doesn't really apply.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

C# unfortunately does not support const methods or const parameters. There is a new feature in C# 2.0 that somewhat helps in a similar scenario. With C#2.0 get and set accessors of a property can be of different accessibility. So you can make the get accessor public and the set protected as follows

class MyClass

{

int _val;



    public int Val
   {
         protected set { _val = value; }

         get { return _val; }

   }

}
Fyre
  • 1,180
  • 8
  • 12