Declaring a passed by reference function argument as const

G

Guest

In C# objects are passed in functions by reference. Is there a way like in
C++ to declare that the function does not alter the passed object? Something
like:

Foo(const Byte [] someThing) { }
 
J

Jeff Louie

Ralph... In C# you can pass a reference to a copy of an object or wrap
the object in a read only class and pass a reference to the read only
object. Alternatively, you can redesign and pass a reference to an
immutable object. Finally you can create an inheritance hierarchy of
non-null collection --> read-only collection --> readwrite collection.
Create an instance of the readwrite collection, but pass a reference
variable of type readonlycollection. Unfortunately there is nothing
stopping the client from casting the readonly reference to type
readwritecollection.

Regards,
Jeff
Is there a way like in
C++ to declare that the function does not alter the passed object? <
 
J

James Curran

You're missing his point.

He's not asking how can you prevent a function from modifying it's
parameter. He wants to know how the author of a function can announce to
potenial users of the function that it's not going to modify it's paramter
(ie, that it's safe to pass an immotible object without going throught the
wrapping you suggest)

Unfortuately I can't think of any.
 
G

Guest

In C# objects are passed in functions by reference.

As Jon pointed out, the "real" story is fairly complicated. C# has 4 types
of parameters (value, ref, out, and variable length).

When you pass a reference type (a class or an array) to a method, the thing
you are passing is a reference, not the object.

The most common case is to pass the reference by value. The value of the
reference is copied to the method. The method can use it's reference to
change the data inside the object.

It is also possible to pass the reference as a reference parameter (yes, I
know that is hard to say ;-). In this case, the method can still use it's
parameter to change the data inside the object, but it now has the added
ability to assign to its reference and change the client's reference. This is
analogous to a C++ pointer to a pointer (i.e. **) parameter.

In any case, all of the above is not relevent to your question :) but it
still might be interesting.
 
J

Jeff Louie

I beg to differ. Nicholas already stated that there is no equivalent to
this use of C++ const in C#. I suggested several alternatives to the use
of const. including the passing of a reference to an immutable object.

Regards,
Jeff
You're missing his point.
 
G

Guest

If it's not too much trouble, you could always accept a XML Serialization
version of the object as the parameter. Alternatively, you could just add a
comment in the documentation summary that the object will not be modified.
Reuben
 

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