R
raylopez99
I'm posting this fragment from another thread to frame the issue
clearer.
How to pass an object to a function/method call in C# that will
guarantee not to change the object?* In C++, as seen below, you can
use the 'const' keyword in the function / method declaration. But how
to do this in C#?
*for example: "void Foo() const;"
Bonus question: how to do in C# this, from C++:
"const MyObjectReturnedThatCannotBeModified Foo(); "
RL
So are you saying 'pass-by-value' is like 'constant' in C++ (which to
my newbie knowledge C# lacks) in that you can safely pass an object
to
a function so it cannot be manipulated to change the original? If
so,
please let me know how to do that, since honestly once I learned C# I
have yet to find how you can make a function/method take a 'read
only'
copy of a class (not a primitive type, but of a class). In fact,
Googling this confirmed this fact (see below). So I don't see how
pass-by-value is any 'safer' (or read-only) than 'pass-by-value' for
a
class passed.
RL
Googling on 'const', note the **** text ****--RL
The const keyword only applies to variables, which MSDN calls
“fields”. A field with the const keyword indicates that the field
cannot be changed. The value of the field must be initialized in the
declaration. Interestingly, attributes cannot be declared as const,
and furthermore, attributes referring to const fields can only have
getters, not setters.
****In C++, the const keyword was also used to indicate that the
method did not modify any variables:****
void Foo() const;
****or that the method returned a reference that could not be
modified:****
const int& Foo();
or that the parameter being passed as a reference could not be
modified by the method:
void Foo(const int& i);
which could lead to some ridiculous but valid statements:
const int& Foo(const int& i) const;
Happily, C# treats the keyword const with only one meaning.
Declaring fields as const protects both you and other programmers
from
accidentally changing the value of the field. Also note that with
const fields, the compiler performs some optimization by not
declaring
any stack space for the field.
Furthermore, const can only be applied to intrinsic types. You can’t
say:
const HasConstField hcf2=new HasConstField();
clearer.
How to pass an object to a function/method call in C# that will
guarantee not to change the object?* In C++, as seen below, you can
use the 'const' keyword in the function / method declaration. But how
to do this in C#?
*for example: "void Foo() const;"
Bonus question: how to do in C# this, from C++:
"const MyObjectReturnedThatCannotBeModified Foo(); "
RL
If your client lives, say, in Dallas, then,
when you want to reach it, you look where? in Dallas, no! if you are in
Seattle, you look at your cardfile holding your clients addresses. Now, if
one of your buddy (procedure), living in New York, ask to contact the said
client, you have two options: make a copy of your cardfile and 'pass' it to
him, that is 'by value', or you cant 'rent' him, for a while, your cardfile,
that will be 'passed by reference'. In both case, your New York buddy can
reach your client, in Dallas, and he can change your client to a not-client
anymore, quite permanently, sure, but, and there is a but, in the last case,
it can do much more, he can screw your cardfile too. And NEVER you ever
"passed" (ouch) the client it-self to your buddy.
So are you saying 'pass-by-value' is like 'constant' in C++ (which to
my newbie knowledge C# lacks) in that you can safely pass an object
to
a function so it cannot be manipulated to change the original? If
so,
please let me know how to do that, since honestly once I learned C# I
have yet to find how you can make a function/method take a 'read
only'
copy of a class (not a primitive type, but of a class). In fact,
Googling this confirmed this fact (see below). So I don't see how
pass-by-value is any 'safer' (or read-only) than 'pass-by-value' for
a
class passed.
RL
Googling on 'const', note the **** text ****--RL
The const keyword only applies to variables, which MSDN calls
“fields”. A field with the const keyword indicates that the field
cannot be changed. The value of the field must be initialized in the
declaration. Interestingly, attributes cannot be declared as const,
and furthermore, attributes referring to const fields can only have
getters, not setters.
****In C++, the const keyword was also used to indicate that the
method did not modify any variables:****
void Foo() const;
****or that the method returned a reference that could not be
modified:****
const int& Foo();
or that the parameter being passed as a reference could not be
modified by the method:
void Foo(const int& i);
which could lead to some ridiculous but valid statements:
const int& Foo(const int& i) const;
Happily, C# treats the keyword const with only one meaning.
Declaring fields as const protects both you and other programmers
from
accidentally changing the value of the field. Also note that with
const fields, the compiler performs some optimization by not
declaring
any stack space for the field.
Furthermore, const can only be applied to intrinsic types. You can’t
say:
const HasConstField hcf2=new HasConstField();