Why is there not such thing as const when passing argument to another method

T

Tony Johansson

Hello!

If you pass a custom made object such as a product to another method this
product object can be changed by this receiving method even if the sender
does not allow that.

So does anyone know why there is no such thing implemented in C# language
that can prevent a receiving method to change the received product for
example.

//Tony
 
A

Arne Vajhøj

If you pass a custom made object such as a product to another method this
product object can be changed by this receiving method even if the sender
does not allow that.

So does anyone know why there is no such thing implemented in C# language
that can prevent a receiving method to change the received product for
example.

Personally I believe that C++ const would be very nice to have.

But Anders Hejlsberg thougth otherwise.

In .NET it is all or none.

Either something is mutable and everybody can change its state
or it is immutable and nobody can change state.

As stated above then I miss the finer granularity, but in
a big portion of cases, then you probably should look into
immutable classes/objects.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top