Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Can anyone help me with C#?

Name: Anonymous 2011-03-16 5:41

Hi /prog/,

I'm trying to teach myself C# and the book I have totally sucks. Can anyone teach me about (ref) and (out)? About passing value type arguments and passing by reference?

Thanks

-Me

(Yes I know I'm unintelligent and I suck)

Name: Anonymous 2011-03-16 6:43

mailto:noko
Requesting sagetank.

Name: Anonymous 2011-03-16 7:00

>>1
About passing value type arguments and passing by reference?
It's not exactly about "value types" in particular.

Say you have a variable x. If it's value type it holds the value itself, otherwise it holds a reference to the value.

Normally the contents of the variable are passed to a function, be that a value or a reference. In other words, you have your variable x outside of a function, and an argument y inside the function, and these two are different things, despite having the same value when function is called. If you then change the value of y, the value of x doesn't change (but if both are references to some object and you modify that object through y, of course you will see the changes through x too)

When you pass it as "ref x", the reference to the variable itself is passed, transparently. So your argument y is the same thing as your variable x, and whenever you assign something to y inside the function, x changes too.

"out" is exactly the same thing implementation-wise, but additionally means that the function is supposed to initialize the variable. So you don't get a compiler error when you pass an uninitialized variable to the function, and do get a compiler error when you try to use the passed value inside a function.

You might also want to read and about pointers: http://en.wikipedia.org/wiki/Pointer_(computing), understanding them would allow you to see how references actually work, rather than treating it as some arbitrary magic.

Name: Anonymous 2011-03-16 9:57

I'm trying to teach myself C#
Can anyone teach me about (ref) and (out)?

Don't change these.
Name: Email:
Entire Thread Thread List