473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Declaring a passed by reference function argument as const

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) { }
--
Ralph Flaugher
Nov 16 '05 #1
7 1978
Ralph,

No, const is not supported in C#.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ralph Flaugher" <Ra***********@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
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) { }
--
Ralph Flaugher

Nov 16 '05 #2
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? <

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
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.
"Jeff Louie" <je********@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 16 '05 #4
(Others have replied to your actual request, but...)

Ralph Flaugher <Ra***********@discussions.microsoft.com> wrote:
In C# objects are passed in functions by reference.


No they're not. References are passed by value. There's a difference.
See http://www.pobox.com/~skeet/csharp/parameters.html

You can't pass an actual *object* at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
> 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.
Nov 16 '05 #6
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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
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
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
6
by: Marc | last post by:
T x; T foo(T, T); bind1st(ptr_fun(foo), x) creates a function object that takes an argument of type T const&. This does not work if T is already a reference type like int const&. So my first...
7
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
14
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public:...
1
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified...
11
by: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
14
by: c.lang.myself | last post by:
Is there any memory allocated in stack when declaring a function e.g void fun(int a,int b,int c); Is it true that compiler reserves 3 stack spaces for parameters a ,b and c....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.