0

I went through this:

var bmp = new WriteableBitmap(ImageWidth, ImageHeight, 96, 96, PixelFormat, null); // declaring the variable

_measurement.MasterImage.CopyTo(bmp); // passing the variable by value

TransformedBitmap transformedBmp = new TransformedBitmap( bmp, new RotateTransform(90)); // using the variable with it new value

How could variable bmp be changed and we did Not pass by reference?

  • 3
    The value of the variable `bmp` won't have changed - but that's just a reference to an object. `CopyTo` can still modify the reference itself. Time to read http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Jun 21 '16 at 15:57
  • @jon I am trying to get a solid base about this so thanks. So `bmp` already is a reference because we used `new`? so passing a copy of it is the same as passing a new reference to the same object. And that is why the value of bmp have been changed? –  Jun 21 '16 at 16:01
  • 3
    No, `bmp` is a reference because *every* the value of *any* variable whose type is a reference type is a reference. See also http://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference/32010236#32010236 and http://pobox.com/~skeet/csharp/references.html – Jon Skeet Jun 21 '16 at 16:03

2 Answers2

1

bmp is an object, a reference type - which means that you are passing references to that object, not values.

 public void Resize(WriteableBitmap bmp)
 {
     bmp.Width = 100;       // Changes the object which bmp points to
 }

The contents of that object can be changed, even if the reference to it is passed by value.

 // Create a new object
 bmp = new WriteableBitmap(ImageWidth, ImageHeight);

 Resize(bmp);      // pass a reference to the object

 // bmp.Width is now 100

See MSDN: Value Types and Reference Types

0

The "value" has not changed. It was a given object and it is still that object.

It might be easier to think of if you imagine a List<string>. You can pass it to other methods and it will always be the same object. Other methods might add items to the list or delete them. Or if it's a class with properties, you could pass it to a method that changes those properties.

What they can't change (unless you pass it by reference) is the object itself. They can't set it to a different object or set it to null.

They can do absolutely anything else that you can do to or with that object.

Here's a blog post I wrote on this with a few examples. It will click and you'll see it.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • `The "value" has not changed.` The value *has* changed. The *variable* hasn't changed. – Servy Jun 21 '16 at 16:12
  • 2
    Understood. I'm answering the question in the terms in which it was asked. Sometimes introducing the terminology isn't the best way to clear up confusion. This is better explained by example than by terminology. Then introduce the terminology. Don't downvote just because you don't agree with my approach. – Scott Hannen Jun 21 '16 at 16:16
  • @ScottHannen The question specifically uses the both of the terms "value" and "variable". It even specifically asks why the *variable* was changed, so you're explicitly *not* using the relevant term from the question. You also shouldn't assume that people downvote your post just because they tried to help you improve it. In fact, I didn't. I will now though, given how rude you've been, and your insistence on knowingly using an incorrect explanation rather than fixing a simple mistake.. – Servy Jun 21 '16 at 16:17
  • 1
    Wrong, the correct terminology is important and must be taught correctly from the start. Otherwise, the reader may leave thinking they understood the concept, when in fact they didn't (and they eventually come back on SO asking why things are not working as expected). Simplifying terms is one thing. But using incorrect terms is different. – sstan Jun 21 '16 at 16:22
  • 1
    My answer focuses on the behavior, which is really what OP is trying to understand. Sometimes introducing terms, even correct ones, might not help. Or it might. It depends on the reader. Personally, I find that some explanations assume that providing the right words will provide the right understanding. For some people that will help. For others it might not. – Scott Hannen Jun 21 '16 at 16:22
  • It's not the incorrect term. You may not like it, but it is correct. The value that was passed into the method is the memory location of where the object lives. This has not changed. – Steve's a D Jun 21 '16 at 16:25
  • 1
    Correct terminology is important. That's why I use it in my linked blog post. But I don't always recommend leading with it. Sometimes you need to describe a concept as it relates to the problem, and then introduce the terms for what you described. Or if you feel that teaching is a one-size-fits-all endeavor then by all means downvote and discourage OP from considering an explanation that might help. – Scott Hannen Jun 21 '16 at 16:30
  • @ScottHannen So why is introducing new, incorrect, terminology helpful in accomplishing that? If the OP was using incorrect terms and you used his terminology then that argument would at least apply to your answer, but the OP used the *correct* term and you *went out of your way to use the wrong term, contrary to the terminology of the quesiton*. How does going out of your way to *introduce* the wrong terminology help? – Servy Jun 21 '16 at 17:01
  • I agree that answering an OP should be at the OP's level of question and understanding. Helping him to get it and jump to the next level. Learning the correct terminology from the first time? I don't even know yet what is the meaning of terminology. Anyway, my question is "_Why the value **have been changed**_" so.. –  Jun 21 '16 at 17:14
  • 1
    I didn't introduce any new terminology. Did you not notice the quotes I put around "value", indicating that the use of the word is not literal? Did you not notice the comment in the OP - 'passing the variable by value?' How would you explain to the OP that it was passed "by value" but it's not a value? I'm not minimizing the importance of understanding these things correctly. But when I answer a question like this I'm functioning as a teacher, not a textbook. I choose my words carefully to minimize the number of things the OP must learn *simultaneously* and instead make them *sequential*. – Scott Hannen Jun 21 '16 at 17:16