Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Content Obsolete

This content is no longer current. We recommend you try the following url for more up to date content: http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners

More about Classes and Methods - 15

Download

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.

 

Download the source code for More about Classes and Methods

Tags:

Follow the Discussion

  • shivashiva

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

  • SachinSachin

    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.

  • SachinSachin

    "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

Comments Closed

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.