Today I had an, argument with my C# teacher because he believes that reference types are passed by value

I posted a link on Facebook to MSDNs page about it, but somehow some guy in my class still argued for it being pass by value. The reason he says so is because the value is the reference, even though it's quite literally a reference.

It's a reference to a variable rather than, a value.


  • 2
    you use the ref keyword if you want to pass the reference, otherwise a copy of the reference is passed by value. Maybe your teacher was right.
  • 1
    It's still a reference though. Had it been passed by value this wouldn't print 3.

    That's why it's called a reference type 😂😂
  • 2
    If a was a reference then doing a = new int[x] inside BBB would affect a inside main also, but that does not happen
  • 1
    idk about c# but in java it's like so

    void random(Point p){
    // this will permanently change x and y of p
    p.x = 1;
    p.y = 2;

    /*p will now be a local variable, the point will still have 1,2 outside of this method*/
    p = new Point(3,4);

    The terminology is kinda hard, but I guess it's like you said, the reference is passed by value (e.g a copy)

    It's a common error, and kinda hard to explain, again cause of the terminology. Hell I only learned about this a month ago, after I've been using java for like 4 years.

    But tbh, if you need to call a constructor on a method argument, then there's something wrong with the design in the first place, a return is more suitable. Which is why you never really need such a thing.

    I hope this helps :)
  • 0
    @NOOB4LIFE No, that's just plain wrong. And I will let the docs explain why: https://docs.microsoft.com/en-us/...

    "With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other"

    This means exactly what happens in the picture I posted above. That a will be a reference to aa, and in other words, it's a god damn reference.
  • 1
    Or, with an actual proof
  • 1
    @vlatkozelka but if the point p is changed outside of the method whilst executing the method. The value of p in your method random would then be affected as well both in java and c#
  • 1
    @matt-jd true, because the reference to p is passed to the method (by value,), so the value of p will be resolved from that reference that hasn't changed. But still if you ever chamge the value p itself in the method, it will change that value of a reference, but p outside will remain the same.
  • 0
    @inaba that link doesnt talk about passing these types as method parameters
  • 0
    @NOOB4LIFE I.. Uhm.. I think you might need a back to basic.. Or to rethink your statement 😂
  • 0
    @vlatkozelka also true, so it's well something. I really don't know what to reply
  • 1
    @matt-jd I'll reply...

    we are on the same page. We both realize that the reference to p passed, is not the actual reference itself *p, but some value holding *p

    OP just doesn't realize maybe that's what his teacher, colleague and people in here in the comments are telling him.

    The below applies to Java:
    All I can say is you can't say "it's passed by reference" nor "it's passed by value" because both are wrong... but you can't blame documents for not saying "it's passed as a value of a reference" or "it's passed as a copy of a reference"

    Just treat it as a reference, while keeping in mind that if you do p = new Point(). that change won't reflect, and you'll be fine.
  • 0
    @vlatkozelka ..seriously

    Okay, so, look at this picture. The fact that this happens means that it is not passed as a value but as a reference. You don't get any more proof than that.

    And yes, I can link to the docs, and keep in mind that this is the docs of the people that made the god damn language, and then say that it's how it is because, it's the god damn documentation. If he meant something else he would have said it after I told him a copy would be made, but he didn't. He still insisted on it being passed as a value.

    Either way, what he meant doesn't matter when what he said wasn't that
  • 1
    Such overconfidence.
  • 1

    I love how irrelevant your tests are to the thing in question. I really hope your approach to test things get better as you gain experience
    I also have a screenshot for you that might help clear things up for you
  • 0
    @vlatkozelka congratulations. You've modified the pointer to be a pointer to something different. That does not make it pass by value at all.

    And my example isn't pointless wtf. How Hugh are you at the moment? They've perfectly demonstrated just what I meant.

    If you in C++ do this:
    void foo(std::vector<int>* p) {
    p = nullptr;

    The pointer p will be destroyed, but whatever p points to will be intact. Just like in your example. And just like in your example, that does not make it pass by value.
Your Job Suck?
Get a Better Job
Add Comment