Sign in to follow this  
SticksandStones

[.net] Are member variables passed by reference?

Recommended Posts

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#.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this