Agree that we cannot define a constant function in C#, I am concerned if there is any way we can achieve this in C#.
As far I know constants should be resolved at compile time itself. But in C# all object creations happens at run time. And there is no global function concept as well in C#.
Lets take C++ for a while.... here we can always define a global function as constant or even a member function as constant. Now here also we have the flexibility to create the object at runtime. Now consider we are creating object at run time only then also we can make it constant such as in the following example....
class aC
{
public:
int i;
aC():i(9){}
void print() const
{
cout<<" In Const "<<i<<endl;
}
void print()
{
cout<<" In Non Const "<<i<<endl;
}
};
main()
{
aC aObj;
aC const aObj2;
aC const *aNewObj = new aC();
aObj.print();
aObj2.print();
aNewObj->print(); // will call constant version of print()
return 0;
}
why in c# we just cannon write.....
aC const aObj = new aC(); // if aC is a class in C#
Vivek Srivastava
[email protected]