C# unsafe mode | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

C# unsafe mode

started with C but had some problems with the pointers/memory management so I decided to not, at least for now and learn an easier language first. I chose C#, then learned that C# has an UNSAFE MODE that uses pointers and I think memory management, that scared me to use it because of my experience with C. What I’ve researched is, a C# programmer would probably never/VERY rarely need to use UNSAFE MODE. I thought maybe it would be a better idea to start with Java since it does not have unsafe mode at all?

16th Feb 2022, 3:48 PM
Jermainne Espinoza
21 Answers
+ 1
As Shen Bapiro said, other languages are based on C but have much easier ways to do things. You are unlikely (in modern computing) to absolutely have to save that couple of bytes per iteration like they did in the 70s & 80s. Learning C instead of python is like: building a house in the wilderness with no tools nor materials, python is like having a hardware store a few minutes away. Build your own "tools" first (your own understanding), use pre-built shortcuts afterwards! 🔨🐒
18th Feb 2022, 4:15 AM
HungryTradie
HungryTradie - avatar
+ 6
You will not have to deal with unsafe in C# especially if you are just learning. just let C# CLR (Common Language Runtime) do the memory management. even in C safety is not an excuse not to learn it. it's actually good to deal with memory management to understand how it works, instead of letting some virtual machine layer do it for you. as a learner you don't have to worry about safety for now.
16th Feb 2022, 6:22 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 5
You don't have to worry about unsafe. Unless you're making a 30 million dollar video game, or know what you are doing and want to use unmanaged code for some reason, it not something that is likely to come up.
16th Feb 2022, 6:12 PM
Kail Galestorm
Kail Galestorm - avatar
+ 3
Thank you so much, is it ok for me to go with Java for now?
16th Feb 2022, 7:29 PM
Jermainne Espinoza
+ 3
Even working with unity, you can freely learn and take C# coding very far without ever running into a situation where you have too use unsafe mode. Yes there are many work around if ever you run into a situation where you think you need to manually allocate memory or something. There are c++ libraries that can interface with C#. You can add one to your c# project. There are a few keywords such a "ref" and "out" that address some of the reference issues that might come up very rarely. It might seem obvious but using unsafe code is not recommended. It doesn't mean your code will be unsafe. It just means .net can't verify that it is safe. By the time you run into limitation that can be solved with unmanaged code, you should probably be thinking about learning c++ and that will not be any time soon after learning c#.
17th Feb 2022, 1:26 PM
Kail Galestorm
Kail Galestorm - avatar
+ 2
I would also point out that Java has it's own memory allocation issues and similar unsafe libraries. Like in C#, these are very advanced topics in Java, and shouldn't come up very often but the same advice applies.
17th Feb 2022, 1:34 PM
Kail Galestorm
Kail Galestorm - avatar
+ 2
bro whats wrong with memory management ? its a fun topic to learn and learning what the heap and pointers can do gets u one of the most powerful features in C. dont get intimidated by a chapter simply because its hard to do. just start learning it and keep failing until u fully understand and master how it works
17th Feb 2022, 2:36 PM
Shen Bapiro
Shen Bapiro - avatar
+ 2
dynamic memory allocation and manual memory management is going to be important once you get to the realm of data structutes. remember those Lists, Hashmap, Stack, Trees and other data structures u find on high level languages like C#? all of them are implemented using dynamic memory allocation and pointers, understanding how to create them yourself is going to be necessary if you want to make more complex data structure other than a simple static array
17th Feb 2022, 2:49 PM
Shen Bapiro
Shen Bapiro - avatar
+ 2
if you just started to learn programming, C would be the best language to learn in my opinion. its simple and barebones enough to the point where it forces you to think like a computer. which helps a lot in understanding what exactly happens when your program runs on the processor. if you master learning C, learning other languages is like a piece of cake since most of them are derivatives based off C, like C#, C++, Python, Java . practically C with extra features. C teaches u the core fundamentals of programming and i think it will reward u in the long term if u want to learn lots of other languages in the future
17th Feb 2022, 2:58 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Let me be honest, 99% of C # developers did not use unsafe. Of course, I had to use a C ++ library myself, and it was a very valuable experience. The ref keyword is commonly used for addressing and as an alternative to a pointer. This is also very rare.
16th Feb 2022, 7:24 PM
hossein B
hossein B - avatar
+ 1
Thank you so much for the response
17th Feb 2022, 1:40 PM
Jermainne Espinoza
+ 1
Python can be seen as a higher level language then C# or Java. Python allocates memory through it's memory manager which is highly efficient and flexible. It doesn't handle garbage collection or heaps the same as c#. Without going too deep, no, python doesn't exactly have anything similar. You are kind of stuck with the python structure of making objects.
17th Feb 2022, 1:50 PM
Kail Galestorm
Kail Galestorm - avatar
+ 1
Thank you. Actually, i guess maybe i understand it somewhat, what i dont get is how i would use memory allocation in real life situation
17th Feb 2022, 2:39 PM
Jermainne Espinoza
+ 1
Thank you for all the information, would you reccomend i learn C or C++ first?
17th Feb 2022, 2:51 PM
Jermainne Espinoza
+ 1
well i think it's upto you whether u want to start off with a high level language or not. im just stating what learning C first would help you before getting into other languages, if you want speed and simplicity C is always the best way to program something.
17th Feb 2022, 3:07 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Thank you
17th Feb 2022, 3:07 PM
Jermainne Espinoza
0
Thank you. Is it safe to say that i dont have to worry about UNSAFE MODE for now at least even if i am working in UNITY? Any feedback would be greatly appreciated, thank you
17th Feb 2022, 4:41 AM
Jermainne Espinoza
0
Also, so i understand correctly, would it be possible in a situation where UNSAFE MODE would be nessesary, to have another way to go through it? A workaround? In other words SAFE MODE?
17th Feb 2022, 4:44 AM
Jermainne Espinoza
0
What about Python? Does Python have anyrging similar to UNSAFE MODE?
17th Feb 2022, 1:41 PM
Jermainne Espinoza
0
Thank you
17th Feb 2022, 1:57 PM
Jermainne Espinoza