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 7 2002
Ralph,
No, const is not supported in C#.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Ralph Flaugher" <Ra***********@ discussions.mic rosoft.com> wrote in message
news:2C******** *************** ***********@mic rosoft.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
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 readonlycollect ion. Unfortunately there is nothing
stopping the client from casting the readonly reference to type
readwritecollec tion.
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!
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********@yah oo.com> wrote in message
news:eb******** ******@TK2MSFTN GP14.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.
(Others have replied to your actual request, but...)
Ralph Flaugher <Ra***********@ discussions.mic rosoft.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
> 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.
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!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 include files:
typedef Context *Context_ptr;
typedef ObjOut<Context> Context_out;
|
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 function call
funcRet()is made which returns the expected argument type for the
function call by reference funcByRef(class A&);
The only way to get around this probelm is to first call the
funcRet(), assign its value to a variable and pass that...
|
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 problem is with the &. My second problem is with the const. Why
should bind1st change the constness of the second argument of the
function ?
|
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 safe and bug-free.
Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a...
|
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:
A<T>& make_A() {return A<T>();};
};
| |
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 function,
`consume'. It has one arg, INBUF, that points to a variable that
points to the first character in the input buffer. The function never
modifies characters pointed to by `*INBUF'. It, however, updates
the `*INBUF' value to point to...
|
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++ book (except for the "friend").
can someone explain why GCC is giving me problems here?
for a += or similar operator, what does a proper declaration look like
and what are its arguments for?
|
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 temporary but it was stated that
it would not. This has come up in an irc channel but I can not find the
original thread, nor can I get any code to work.
Foo& Bar( int Val )
{
return Foo( Val );
}
|
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....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |