1 // This example illustrates the manipulation of C++ references in C#.
7 public static void Main()
9 Console.WriteLine( "Creating some objects:" );
10 Vector a = new Vector(3,4,5);
11 Vector b = new Vector(10,11,12);
13 Console.WriteLine( " Created " + a.print() );
14 Console.WriteLine( " Created " + b.print() );
16 // ----- Call an overloaded operator -----
18 // This calls the wrapper we placed around
20 // operator+(const Vector &a, const Vector &)
22 // It returns a new allocated object.
24 Console.WriteLine( "Adding a+b" );
25 Vector c = example.addv(a,b);
26 Console.WriteLine( " a+b = " + c.print() );
28 // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline
29 // is used as the proxy classes define finalizers which call the Dispose() method. When
30 // -noproxy is not specified the memory management is controlled by the garbage collector.
31 // You can still call Dispose(). It will free the c++ memory immediately, but not the
32 // C# memory! You then must be careful not to call any member functions as it will
33 // use a NULL c pointer on the underlying c++ object. We set the C# object to null
34 // which will then throw a C# exception should we attempt to use it again.
38 // ----- Create a vector array -----
40 Console.WriteLine( "Creating an array of vectors" );
41 VectorArray va = new VectorArray(10);
42 Console.WriteLine( " va = " + va.ToString() );
44 // ----- Set some values in the array -----
46 // These operators copy the value of Vector a and Vector b to the vector array
50 // This works, but it would cause a memory leak if -noproxy was used!
52 va.set(2,example.addv(a,b));
55 // Get some values from the array
57 Console.WriteLine( "Getting some array values" );
58 for (int i=0; i<5; i++)
59 Console.WriteLine( " va(" + i + ") = " + va.get(i).print() );
61 // Watch under resource meter to check on this
62 Console.WriteLine( "Making sure we don't leak memory." );
63 for (int i=0; i<1000000; i++)
66 // ----- Clean up -----
67 // This could be omitted. The garbage collector would then clean up for us.
68 Console.WriteLine( "Cleaning up" );