Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const and non-const function overloading

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

like image 568
zeropoint Avatar asked Jan 17 '23 00:01

zeropoint


2 Answers

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

like image 75
Jon Skeet Avatar answered Jan 26 '23 00:01

Jon Skeet


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; }

   }

}
like image 32
Fyre Avatar answered Jan 25 '23 22:01

Fyre