[.net] Are member variables passed by reference?

Started by
4 comments, last by RipTorn 18 years ago
I was just wondering, are member variables of a class passed by reference if that variable is a value variable? In other words, if I have a class as such:

class Some
{
    public int i;
    public Some(){this.i=0;}
}


and I wanted to pass it to a function by reference, would I have to do this:

void foo(int a){a = 2;}
Some s = new Some();
foo(s.i);
or this:

void foo(ref int a){a =2;}
Some s = new Some();
foo(ref s.i);
? EDIT: I don't know if it matters, but this is in C#.
hippopotomonstrosesquippedaliophobia- the fear of big words
Advertisement
No (unless they're reference types anyway). Your first example would be the way to do it. (I don't know if you're allowed to do it, you'll have to try. It's not recommended practice to have public fields in the first place, so I'm sure this isn't either, but that doesn't necessarily mean they won't let you.)
I don't remember exactly, but aren't you supposed to use out parameters for value types and ref parameters for reference types? In this case of the int, you would use out:


class Some{    public int i;    public Some() {this.i = 0;}}void foo(out int a){    a = 2;}Some s = new Some();foo(out s.i);


I hope that helps you out some and if not I'll check my book when I get home.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
All objects are passed by reference where all variables (primitive data types and structures) are passed by value.

To get around passing by value, you're given two very nice tools that you can use: ref and out. Use ref if you want to pass a value-type by reference when it already is initialized. out is very helpful when the value isn't initialized and the function is going to be responsible for setting the value.
Rob Loach [Website] [Projects] [Contact]
Quote:Original post by Rob Loach
All objects are passed by reference where all variables (primitive data types and structures) are passed by value.

To get around passing by value, you're given two very nice tools that you can use: ref and out. Use ref if you want to pass a value-type by reference when it already is initialized. out is very helpful when the value isn't initialized and the function is going to be responsible for setting the value.


Ok, so regardless of if the value type is part of a reference type, it's still passed by value?

Alright, I wasn't sure (and I don't have a compiler around to test) so I decided to ask [grin] Thanks.
hippopotomonstrosesquippedaliophobia- the fear of big words
All value types (ie, structures in C#) inherit from System.ValueType, which inherits from System.Object. All reference types (ie, classes in C#) inherit straight from System.Object.

You can't directly inherit from System.ValueType either.

So basically if it has a structure icon in intellisense, then it will be copy-by-value. If it has a class icon, then it's copy by reference.

The difference between 'ref' and 'out' is that ref requires the input value has been initalised before calling the method, where out requires the value get assigned before leaving the method, otherwise their usage is pretty much exactly the same.

This topic is closed to new replies.

Advertisement