Deep C# - Passing Parameters
Deep C# - Passing Parameters
Written by Mike James   
Thursday, 19 November 2015
Article Index
Deep C# - Passing Parameters
Pass by reference
Boxing
Making Clones

Passing parameters is easy as it always works in the same way, but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works. So does C# pass by value or reference?

This is Chapter 2 of our ebook on C# - a work in progress.

Deep C#

 coverCsharp

 Chapter List

  1. Value And Reference
    Value and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

  2. Passing Parameters
    Passing parameters is easy as it always works in the same way but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works.

  3. Inheritance
    Inheritance is a simple idea, until you try to make use of it.

  4. Casting – the escape from strong typing
    Casting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it then the how makes a lot more sense.

  5. Controlling Inheritance
    Inheritance is a great idea but it is a powerful technique that can be misused. C# provides the tools to keep inheritance under control or to turn it off completely if you want to.

  6. Delegates
    Delegates are C#'s original way of allowing you to work with functions as if they were first class objects. The aim may be simple but the need to define a type and then an instance of the type can be confusing. Let's see if we can make it all seem logical.

  7. Multicast delegates and events
    Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

  8. Anonymous Methods, Lambdas And Closures
    Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

  9. Take Exception To Everything

  10. Dangerous Pointers
    In C# pointers have been replaced by references but there is still a need for the raw dangerous original concept. You could say "what's so dangerous about pointers?"
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

Passing Parameters

The difference between a value and a reference type can be dangerous when passing parameters to a method. Exactly what is passed and how it behaves confuses even experienced programmers who ask questions along the lines of - is this pass by value or pass by reference or pass by ... 

The rule is very simple, by default value types are passed by value and reference types are also passed by value!

That is by default C# always uses pass by value. 

However when you pass a reference variable by value it look a lot like passing by reference. This sounds complicated but as long as you keep the idea of a value and a reference clear its not so difficult.

Pass Value By Value

Consider first passing a value type by value.

We have the usual PointV struct (see Chapter One) and a method that simply sets one of its fields to a value:

public struct PointV
{
 public int x;
 public int y;
}

void MyMethod(PointV a)
{
 a.x = 10;
}

Notice that the method has nothing to do with the struct - it is part of some other object.

Next we create an instance of the struct using new to make sure its fields have been initialised:

PointV b=new PointV();
b.x = 5;

This results in a value variable b with it’s x field set to 5 as shown

 

fig1

 

If we now call MyMethod with b as parameter:

MyMethod(b);

a new local variable a is created and initially set to the same value as b, then a.x is set to 10.

The result is a local variable with its x field set to 10 as shown

 

fig2

 

Finally MyMethod terminates and all local variables are destroyed leaving only b with its values unchanged by the call as shown below.

 

fig3

 

Thus a call by value does not change the value of b the parameter passed into the method.

Pass Reference By Value

Now consider the reference type version of the same code i.e. what happens when you pass a reference type by value.

In this case we need the class version of the point type, i.e. a reference type version of the object, and a suitably modified MyMethod:

class PointR
{
 public int x;
 public int y;
}

void MyMethod(PointR a)
{
 a.x = 10;
}

As before we create an instance and set its x field to 5:

PointR b=new PointR();
b.x = 5;

fig4

 

This might look the same but as the diagram shows the situation is very different. Now we have a reference variable called b and its content is a reference to a PointR object created on the heap.

Now when we call MyMethod in exactly the same way,

MyMethod(b);

b is passed by value which means that a new local variable a is created and the contents of b are copied to it.

The big difference is now that the content of b is a reference to an object on the heap. The result is that we now have two reference variable referencing the object on the stack and when the assignment a.x=10 occurs in the method the object on the heap is changed as shown.

 

fig5

 

Once again when MyMethod terminates the behaviour is exactly the same. The local variables including a are destroyed but now the change made to the object on the heap persists as shown below.

fig6

 

You should now be able to appreciate that while both value and reference variables are passed by value the effect of making changes in the method are very different.

Also notice that in both cases you cannot make a change to the value of the paramter passed. In the value case this means you cannot change the data and in the reference case you cannot change what is referenced. That is in both cases assignment to a in the method  has no effect on the variable b passed into the method. 

 

Banner

 

To summarise: by default parameters are always passed by value and assignment has no effect on the variables passed in. Passing a reference type allows the method to change the fields of the object referenced. 



Last Updated ( Thursday, 19 November 2015 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.