Do I need to reset a stream(C#) back to the start if I pass stream into method as Pass Value-Type Parameter method -
i have seen couple of example on internet stated have reset stream by
stream.seek(0, seekorigin.begin);
however, if pass stream method
public static bool validatestreamline(stream stream)
do have still reset stream?
understand on usual case if pass in int, string, float or other general variable type as
public static bool validateint(int i)
the value of int not change.
would difference of nature of pass-by value method on how method react stream?
you seem misunderstanding how arguments passed in c#. default, arguments passed value in c#. in order pass them reference need use special keyword: ref
or out
latter being used when argument used second output of given method. (see int.trypase
example).
the important thing understand here arguments passed value behave quite diferently if type of argument reference type or value type. seem confused.
to understand how works, make sure clear on following:
- variables hold values.
the value of variable type value type, is value type's instance itself:
int = 1 // holds value 1
the value of variable type reference type not instance of said type. value memory address instance lives.
string s = "hello!" // s not hold "hello!" holds number points place in memory string "hello!" lives.
so, clear on that, happens when pass arguments value (c#'s default)? happens copy of variable made , copy passed method.
if type reference type, copied , passed method value stored in variable. , that? memory address object referenced variable lives. see happens? both original variable , copied variable both point same object:
public class foo { var frobbed = false; public bool frobbed { { return frobbed; } } public void frob() { frobbed = true; } } void frob(foo foo) { foo.frob(); } var myfoo = new foo(); frob(myfoo); console.writeline(myfoo.frobbed); //outputs true! why? because myfoo , foo both point same object! value of both variables (memory address) same!
if type value type, value of value type copied , handed method there no way method can modify value type stored in original variable.
public void increment(int i) { = + 1; } var myint = 1; increment(myint); console.writeline(myint); //outputs 1, not 2. why? holds own copy of 1, knows nothing copy of 1 stored in myint.
things change when pass arguments reference. now, argument passed method not copy, original variable itself. logical question follows; change in how reference types behave? answer yes, quite lot:
public void byvaluecall(string s) { s = "goodbye"; } public void byreferencecall(ref string s) { s = "goodbye"; } var mystring = "hello!"; console.writeline(byvaluecall(mystring )); //outputs "hello!" console.writeline(byvaluecall(mystring )); //outputs "goodbye!"
this behavior identical value types too. happening here?
when pass argument value, method gets copy of variable; therefore assigning new value argument assigning new value copy; original variable @ callsite doesn't care change value of it's copy, value type or reference type. keep holding value had.
when passing argument reference, not passing copy, passing variable itself. in case assigning new value variable persist @ callsite. canonical example following swap method:
public void swap(ref int a, ref int b) { var temp = a; = b; b = temp; } var i1 = 1; var i2 = 2; swap(ref i1, ref i2); var b = i1 == 2; //true b = i2 == 1; //true
so after of this, should understand why following behaves way does:
public resetstream(stream stream) { stream.seek(0, seekorigin.begin); } var mystream = new ... mystream.read(bytes, 0, 1024); resetstream(mystream); var isatorigin = mystream.position == 0; //returns true!
Comments
Post a Comment