D
David
I feel like an idiot asking this but here goes: I understand the 'concept'
of scope and passing data by value and/or by reference but I am confused on
some specifics.
class example{
int i; //my global var
private void method1(int myInt){
int x; //local var
etc...
}
private void method2(int myInt){
int z; //local var
etc...
}
}
so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within a
method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a 'copy' of the passed
variable's data and that passing byref passes the actual memory location of
the passed variable. So, for instance, if my global var i was passed byref
to either of those methods in the example and modified, that actual global
var i would be modified directly... whereas if it was passed byval and
modified within the method that the global i var would remain untouched.
now here is where I get confused: say the var x in method1 was a reference
type like 'object'. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var i
is?
side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)
of scope and passing data by value and/or by reference but I am confused on
some specifics.
class example{
int i; //my global var
private void method1(int myInt){
int x; //local var
etc...
}
private void method2(int myInt){
int z; //local var
etc...
}
}
so I get that the global var i is accessible by any methods in above class
example, based on where its declared, and that the var x, for example, is
local to method1 and only accessible from method1. And beyond that, within a
method, you can have further localized variables within conditional IF
blocks or loops. I get that passing data byval passes a 'copy' of the passed
variable's data and that passing byref passes the actual memory location of
the passed variable. So, for instance, if my global var i was passed byref
to either of those methods in the example and modified, that actual global
var i would be modified directly... whereas if it was passed byval and
modified within the method that the global i var would remain untouched.
now here is where I get confused: say the var x in method1 was a reference
type like 'object'. It is declared in method1, so scope is only method1.
What if method2 was called from method1, and x is passed in. Would the x
used in method2 be the same x from method1? If I wanted it to be would x
have to be declared outside both those methods where my example global var i
is?
side bar question: does c# pass parameters by value or by reference by
default? (i think its by value except I recall something special about
strings?)