2

So, I love the C# language, and the automatic getters, setters, and so on, but I want to utilize it to be able to do a good portion of the plumbing myself. A great deal of the syntax is specifically why I want to use C# as opposed to sucking it up and picking C++ back up (which isn't entirely out of the question here).

My objective is to use SDL, GDI+, DirectX, or something of that nature to be able to create all of my own Windows from scratch like this, but using C#. I am very okay with using P\Invoke, doing my own memory management, and so on.

So, is there a way to mark an entire application as unsafe so I can do my own memory management?

Rachael Dawn
  • 819
  • 6
  • 13
  • Related: http://stackoverflow.com/a/3074443/1870760. Note, unsafe doesn't stop garbage collection. – Hatted Rooster Mar 12 '17 at 13:24
  • Sadface. I am not so much concerned about garbage collection, but I certainly would like to be able to call native C++ code and do memory management when I want. However, last I tried, the Destructor made my class automatically start losing values. – Rachael Dawn Mar 12 '17 at 13:26
  • 1
    Maybe this is an X/Y problem and the GC isn't messing your stuff up. What did you have trouble with anyway? – Hatted Rooster Mar 12 '17 at 13:27
  • http://stackoverflow.com/questions/41274807/losing-argument-value This was the issue I was having with the garbage collector. Basically, the moment I removed the destructor from the class, it started working without a problem. – Rachael Dawn Mar 12 '17 at 13:32
  • On a related note to the X/Y problem, my issue is that I like some of the syntax things like foreach in C#, but I want the control of C++ over my application. So I've been in a mental struggle for months because I love the ease of use of C#, but love the control C++ has, hence my endeavor to try to use C# as a "low-level" language by trying to get the garbage collector to go away. – Rachael Dawn Mar 12 '17 at 13:49

1 Answers1

2

You can't do your own memory management. You can use an open source framework like Mono and write your own garbage collector. But it's unlikely you'll reinvent a better wheel.

And unsafe doesn't mean you can override default memory management. It means that pointer operations are allowed. You are still in managed memory and you have no control over creation or destruction of your objects.

Sefe
  • 13,731
  • 5
  • 42
  • 55
  • I wasn't so much concerned about garbage collection being an issue, but the ability to access pointers was very appealing. Also, have an upvote, because it appears you have corrected my gross misunderstanding of what unsafe does. I was under the impression that `unsafe` told the GC to keep its hands off of the class/code. – Rachael Dawn Mar 12 '17 at 13:34
  • In all my time developing in C# I never needed pointer operations. – Sefe Mar 12 '17 at 13:36
  • Even when using PInvoke to access C++-written DLLs? – Rachael Dawn Mar 12 '17 at 13:37
  • 3
    @JonathanSchmold there's IntPtr for that. – Hatted Rooster Mar 12 '17 at 14:06