More about Classes and Methods - 15
- Posted: Nov 21, 2011 at 9:17 AM
- 56,666 Views
- 3 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
This lesson digs into more details about Classes—what exactly happens when you create a new instance of a class? What exactly is a reference to an instance of a class? How does passing the reference to a method affect a class? We also review overloaded methods, talk about static versus instance methods, constructors, and more.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Hi,
Initially i typed the methods as :
public static void passByValue(Car car)
{
car.Make = "BMW";
}
public static void passByReference(ref Car car)
{
car.Make = "BMW";
}
and I expected the first method not to make any changes to myNewCar.Make(still remain as toyota) and the second one to make myNewCar.Make as BMW, however both changed to BMW. How did that happen( I thought it was very similar to your integer example).
This is because all objects are passed by reference. Putting the ref keyword or not makes no difference in case of objects. The only reason one might consider putting in a 'ref' next to a object would be to let people know that this method might change your object. Hope that makes sense.
"This is because all objects are passed by reference. Putting the ref keyword or not makes no difference in case of objects. The only reason one might consider putting in a 'ref' next to a object would be to let people know that this method might change your object. Hope that makes sense."
Sorry I stand corrected. In case of Objects. When you do NOT use the 'ref' keyword, you pass a copy of the reference(a copy of the location of the car object in memory). When you do use the 'ref' keyword you passing a reference to the object passed in. The reason why both your functions change your car make, is because, you are making changes to the same object in memory. If you wanted a similar effect you get with value types like integer, you would have to first 'new' up the object in your passByValue method.
When 'value types' like integer, bool, float etc, are passed by VALUE they create a copy of the variable(i.e a NEW memory location). Thus it does not affect the original value of the 'value type' passed in. ok.. that was quite a mouthful :) Hope it helps.
Remove this comment
Remove this thread
close