Just wondering how It could be done and I’m at a point where I am implementing the stream.Read method. Am I missing something or there is just a ref keyword missing or what?
private void Form1_Load(object sender, EventArgs e)
{
byte[] ex = new byte[1] { 0 };
MessageBox.Show(ex[0].ToString());
ok(ex);
MessageBox.Show(ex[0].ToString());
}
private int ok(byte[] asd)
{
asd = new byte[1] { 255 };
return 1;
}
//first result: 0
//second result: 0
This is basically a matter of understanding two important concepts:
Your
okmethod changes the value of the parameter – that won’t be visible to the caller, because it’s a value parameter (the argument is passed by value). However, if you were to write:instead of the first line, then that change would be visible. That’s not changing the parameter itself; it’s changing the value of an element within the object that the parameter refers to.
Basically, the value of an expression in .NET is never an object – it’s always either a value type value or a reference – a way of explaining how to get to an object.
Suppose I hand you a piece of paper with my home address on. If you rub out that address and write on a different address, that doesn’t change where I live, does it? That’s what you’re doing when you change the parameter value. However, if you go to the address on the piece of paper and paint the front door red, then I’ll see a red front door when I come home. You haven’t changed the value on the piece of paper – you’ve made a change within the object that the value on the piece of paper refers to. That’s what
Stream.Readdoes.