0

Pointers in c#

Why would c# have pointers if they are NOT safe to use?? Its like having a car but not drive it because you got no driver licence or smth...

20th Aug 2025, 10:27 AM
Linux
Linux - avatar
4 Risposte
+ 5
A sharp knife is unsafe for a toddler, but absolutely necessary for a professional butcher. Pointers cut away the fat of high-level languages and get right to the meat and bone.
20th Aug 2025, 12:36 PM
Brian
Brian - avatar
+ 3
you can use unsafe code. you just have to make sure you have no memory leaks or security risks. pointers are are inherently unsafe. unlike C or C++, C# decided it's a good idea to have a mechanism to keep reminding people of that. you have to declare the part where you're using it as unsafe. Like warning signs in a minefield. if you think using unsafe can give you the extra performance and you have the skill to do it properly, go ahead and use unsafe. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code
20th Aug 2025, 11:18 AM
Bob_Li
Bob_Li - avatar
+ 2
They r only unsafe if u dont know what u are doing
20th Aug 2025, 12:19 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Aleksei Radchenkov true. but people often mess up. having the unsafe keyword is a good idea. it's like a safety lever in a gun. it makes using pointers an intentional decision, and serves as a warning flag for others that might be modifying your code. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unsafe
20th Aug 2025, 12:32 PM
Bob_Li
Bob_Li - avatar