PageRenderTime 122ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/csharp/reference/runme.cs

#
C# | 73 lines | 32 code | 19 blank | 22 comment | 2 complexity | b6f462e1017f84cf4b071804be912238 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This example illustrates the manipulation of C++ references in C#.
  2. using System;
  3. public class runme {
  4. public static void Main()
  5. {
  6. Console.WriteLine( "Creating some objects:" );
  7. Vector a = new Vector(3,4,5);
  8. Vector b = new Vector(10,11,12);
  9. Console.WriteLine( " Created " + a.print() );
  10. Console.WriteLine( " Created " + b.print() );
  11. // ----- Call an overloaded operator -----
  12. // This calls the wrapper we placed around
  13. //
  14. // operator+(const Vector &a, const Vector &)
  15. //
  16. // It returns a new allocated object.
  17. Console.WriteLine( "Adding a+b" );
  18. Vector c = example.addv(a,b);
  19. Console.WriteLine( " a+b = " + c.print() );
  20. // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline
  21. // is used as the proxy classes define finalizers which call the Dispose() method. When
  22. // -noproxy is not specified the memory management is controlled by the garbage collector.
  23. // You can still call Dispose(). It will free the c++ memory immediately, but not the
  24. // C# memory! You then must be careful not to call any member functions as it will
  25. // use a NULL c pointer on the underlying c++ object. We set the C# object to null
  26. // which will then throw a C# exception should we attempt to use it again.
  27. c.Dispose();
  28. c = null;
  29. // ----- Create a vector array -----
  30. Console.WriteLine( "Creating an array of vectors" );
  31. VectorArray va = new VectorArray(10);
  32. Console.WriteLine( " va = " + va.ToString() );
  33. // ----- Set some values in the array -----
  34. // These operators copy the value of Vector a and Vector b to the vector array
  35. va.set(0,a);
  36. va.set(1,b);
  37. // This works, but it would cause a memory leak if -noproxy was used!
  38. va.set(2,example.addv(a,b));
  39. // Get some values from the array
  40. Console.WriteLine( "Getting some array values" );
  41. for (int i=0; i<5; i++)
  42. Console.WriteLine( " va(" + i + ") = " + va.get(i).print() );
  43. // Watch under resource meter to check on this
  44. Console.WriteLine( "Making sure we don't leak memory." );
  45. for (int i=0; i<1000000; i++)
  46. c = va.get(i%10);
  47. // ----- Clean up -----
  48. // This could be omitted. The garbage collector would then clean up for us.
  49. Console.WriteLine( "Cleaning up" );
  50. va.Dispose();
  51. a.Dispose();
  52. b.Dispose();
  53. }
  54. }