Content Obsolete

This content is no longer current. Our recommendation for up to date content: http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners

More about Classes and Methods - 15

Sign in to queue

Description

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:

C#, Visual Studio

Embed

Download

The Discussion

  • User profile image
    shiva

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

  • User profile image
    Sachin

    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.

  • User profile image
    Sachin

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

  • User profile image
    Fappio64

    this course instantly go sooo much more confusing on this episode... Not only is this just a hard concept to grasp at the pace you're going at, you didn't relay start where the last episode left off. The code in the start of this episode very different from the end of the last video. I'm just going to watch and hop I understand whats going on instead of follow along... I'll figure it out sooner or later right? I know this course was made 6 years ago but supposedly its still relevant to c# now, and I've found that to be true. Anyways its a great course except for how incredibly complex this 3 part segments of understanding methods is.

Add Your 2 Cents