The runtime doesn't have the concept of const per se but it does have
the more general concept of a modifier in a type signature. For example,
"void Foo (const bar b)" in managed C++ actually generates a type
signature that contains the VC++ const modifier applied to parameter
bar.

Modifiers come in two flavors - required and optional. Code that doesn't
understand a required modifer is supposed to produce an error. Code that
doesn't understand an optional modifier may freely ignore it without
problem.

-- Brent Rector, .NET Wise Owl

Demeanor for .NET - an obfuscation utility

http://www.wiseowl.com/Products/Products.aspx



-----Original Message-----
From: Marsh, Drew [mailto:[EMAIL PROTECTED]] 
Sent: Wednesday, May 22, 2002 3:16 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Does C# have equivalent of C++ const
parameters/meth ods?


Sam Gentile [mailto:[EMAIL PROTECTED]] wrote:

> I haven't found this in C#. Does C# have the equivalent of
> C++'s const parameters or const methods? This is common in
> C++: void Foo(const string& str)
>
> Even with MC++, it appears that although const parameters can be used 
> in the methods of a managed class, methods themselves cannot be 
> defined as const.

No, I don't believe there's any CIL level concept of const params. I
guess this is in part due to the fact that unless params are explicitly
marked as by reference with the "ref" or "out" keywords, they're by
value and you can never hurt the caller by modifying one of them anyway.
There is no CIL level concept of a const method either AFAIK.

I suppose a specific language implementation could support such concepts
though and handle it at compile time, making sure your don't modify
paramaters for the former and making sure you don't write to any fields
directly or indirectly for the latter.

Later,
Drew
.NET MVP

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to