CODE
<Window x:Class=”WpfApplication1.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
x:Name=”Window”
Title=”Path Opacity Mask Animation Demo”
Width=”640″ Height=”480″ xmlns:System=”clr-namespace:System;assembly=mscorlib”>
<Window.Resources>
<Storyboard x:Key=”Timeline1″>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”path1″ Storyboard.TargetProperty=”(Shape.StrokeDashOffset)”>
<SplineDoubleKeyFrame KeyTime=”00:00:01″ Value=”25″/>
<SplineDoubleKeyFrame KeyTime=”00:00:02″ Value=”0″/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”path2″ Storyboard.TargetProperty=”(Shape.StrokeDashOffset)”>
<SplineDoubleKeyFrame KeyTime=”00:00:03″ Value=”25″/>
<SplineDoubleKeyFrame KeyTime=”00:00:04″ Value=”0″/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent=”FrameworkElement.Loaded”>
<BeginStoryboard Storyboard=”{StaticResource Timeline1}”/>
</EventTrigger>
</Window.Triggers>
<Grid x:Name=”LayoutRoot”>
<Image Source=”Hydrangeas.jpg” Stretch=”Fill”>
<Image.OpacityMask>
<VisualBrush>
<VisualBrush.Visual>
<Path x:Name=”path1″ Stretch=”Fill” Stroke=”#FF000000″ StrokeDashOffset=”1000″ StrokeEndLineCap=”Round” StrokeStartLineCap=”Round” StrokeThickness=”20″ HorizontalAlignment=”Left” Margin=”228,145.336,0,188.972″ Width=”71″ Data=”M294,171 C289,162 283,147 268,151 253,155 244,164.99992 244,177.00008 244,189.00024 284,196.00033 287,207.00047 290,218.00062 286,249.00103 259,250.00104 232,251.00105 235,225.00071 233,219.00063″ StrokeDashArray=”25 1000″/>
</VisualBrush.Visual>
</VisualBrush>
</Image.OpacityMask>
</Image>
<Path x:Name=”path2″ Stretch=”Fill” Stroke=”#FF000000″ StrokeDashOffset=”1000″ StrokeEndLineCap=”Round” StrokeStartLineCap=”Round” StrokeThickness=”20″ HorizontalAlignment=”Left” Margin=”228,145.336,0,188.972″ Width=”71″ Data=”M294,171 C289,162 283,147 268,151 253,155 244,164.99992 244,177.00008 244,189.00024 284,196.00033 287,207.00047 290,218.00062 286,249.00103 259,250.00104 232,251.00105 235,225.00071 233,219.00063″ StrokeDashArray=”25 1000″/>
</Grid>
</Window>
C# Passing Parameters
Passing Value-Type Parameters
A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Therefore, passing a value-type variable to a method means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the variable. If you want the called method to change the value of the parameter, you have to pass it by reference, using the ref or out keyword.
The following example demonstrates passing value-type parameters by value. The variable n is passed by value to the method SquareIt. Any changes that take place inside the method have no affect on the original value of the variable.
class PassingValByVal
{
static void SquareIt(int x)
// The parameter x is passed by value.
// Changes to x will not affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(n); // Passing the variable by value.
System.Console.WriteLine("The value after calling the method: {0}", n);
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 5
*/
The variable n, being a value type, contains its data, the value 5. When SquareIt is invoked, the contents of n are copied into the parameter x, which is squared inside the method. In Main, however, the value of n is the same, before and after calling the SquareIt method. In fact, the change that takes place inside the method only affects the local variable x.
The following example is the same as the previous example, except for passing the parameter using the ref keyword. The value of the parameter is changed after calling the method.
class PassingValByRef
{
static void SquareIt(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
}
static void Main()
{
int n = 5;
System.Console.WriteLine("The value before calling the method: {0}", n);
SquareIt(ref n); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n);
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
The value before calling the method: 5
The value inside the method: 25
The value after calling the method: 25
*/
Passing Reference-Type Parameters
A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.
The following example demonstrates passing a reference-type parameter, arr, by value, to a method, Change. Because the parameter is a reference to arr, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, arr.
class PassingRefByVal
{
static void Change(int[] pArray)
{
pArray[0] = 888; // This change affects the original element.
pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
}
static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);
Change(arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
}
}
/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888
*/
In the preceding example, the array, arr, which is a reference type, is passed to the method without the ref parameter. In such a case, a copy of the reference, which points to arr, is passed to the method. The output shows that it is possible for the method to change the contents of an array element, in this case from 1 to 888. However, allocating a new portion of memory by using the new operator inside the Change method makes the variable pArray reference a new array. Thus, any changes after that will not affect the original array, arr, which is created inside Main. In fact, two arrays are created in this example, one inside Main and one inside the Change method.
This example is the same as the previous example, except for using the ref keyword in the method header and call. Any changes that take place in the method will affect the original variables in the calling program.
class PassingRefByRef
{
static void Change(ref int[] pArray)
{
// Both of the following changes will affect the original variables:
pArray[0] = 888;
pArray = new int[5] {-3, -1, -2, -3, -4};
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
}
static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);
Change(ref arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
}
}
/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3
*/
All of the changes that take place inside the method affect the original array in Main. In fact, the original array is reallocated using the new operator. Thus, after calling the Change method, any reference to arr points to the five-element array, which is created in the Change method.
Swapping strings is a good example of passing reference-type parameters by reference. In the example, two strings, str1 and str2, are initialized in Main and passed to the SwapStrings method as parameters modified by the ref keyword. The two strings are swapped inside the method and inside Main as well.
class SwappingStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
}
static void Main()
{
string str1 = "John";
string str2 = "Smith";
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);
SwapStrings(ref str1, ref str2); // Passing strings by reference
System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
}
}
/* Output:
Inside Main, before swapping: John Smith
Inside the method: Smith John
Inside Main, after swapping: Smith John
*/
C# Boxing and Unboxing
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.
In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.
Boxing
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
Consider the following declaration of a value-type variable:
int i=123;
The following statement implicitly applies the boxing operation on the variable i:
object o=i; //Implicit boxing
The result of this statement is creating an object reference o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

Unboxing
Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:
Checking the object instance to make sure that it is a boxed value of the given value type.
Copying the value from the instance into the value-type variable.
The following statements demonstrate both boxing and unboxing operations:
int i = 123;
object o = i; //boxing
int j = (int) o; //unboxing
The following figure demonstrates the result of the previous statements.
