9
inaba
6y

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.

Kindly
Fuck
Off

Comments
  • 1
    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 😂😂
  • 1
    If a was a reference then doing a = new int[x] inside BBB would affect a inside main also, but that does not happen
  • 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
  • 0
    @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#
  • 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
  • 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
  • 0
    Such overconfidence.
  • 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.
Add Comment