Sign in to follow this  
genesys

C# (XNA) passing Value type by reference

Recommended Posts

Hi! I've got a quite basic question. If I have a value-type variable, and I pass it to a method using myMethod(ref myValueType); does it then get boxed? so - is it (in terms of performance) better to not pass it by reference but pass it by value? thanks! edit: one more question on boxing: If you create an array or LinkedList of integers - do then all integers get boxed? Is there a way of geting a list or collection of Value-types without boxing them? Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by genesys
Hi!

I've got a quite basic question.

If I have a value-type variable, and I pass it to a method using

myMethod(ref myValueType);

does it then get boxed? so - is it (in terms of performance) better to not pass it by reference but pass it by value?

No, it does not get boxed. It may result in better performance or it may not, hard to tell actually. I have written somewhat about this subject in my journal

Quote:

one more question on boxing:

If you create an array or LinkedList of integers - do then all integers get boxed? Is there a way of geting a list or collection of Value-types without boxing them?

Thanks!

Maybe. If its a generic collection, then in general you will not encounter boxing unless you use a non-overloaded method of the Object class. If you do use such a method, then it will need to be boxed in order to resolve the base method.

Share this post


Link to post
Share on other sites
ok - so far so good! Thank you :)

But then - is there a way to store the same value-type members in two lists without just copying them?

Like to store the value-types in one list and store pointers tothem in another list...

If so - how can I do this?

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