T
T
TheTalion2017-10-01 19:21:52
C++ / C#
TheTalion, 2017-10-01 19:21:52

Why is unsafe code unsafe and rarely used in C#?

This is of interest because in C ++ there are pointers everywhere and nothing terrible seems to happen, but in C # it is necessary to write that this is unsafe code. Those. there is a situation - I have a class with 150 variables (int), I want to change these variables directly, passing it to another class, like this:

void IncFirst(){
Storage.firstValue++;
}

Those. I took it and in the function indicated directly where the object is located, BUT should I write 150 such functions to update each one? This is nonsense.
Logically, you can do this:
void Inc(int* value){
value++;
}

In total, one method is enough for us for any number of variables, but in order to use the pointer, you need to include unsafe code, but, as I understand it, it carries a number of problems, such as it cannot be debugged (as far as I remember). And everywhere they write that it is "Very dangerous" and "Unsafe", although, what is the real danger here? There is, for example, such a phrase from an article on the topic: "Direct use of pointers disables the security system " - what kind of security system is that? Those. does not write error messages in such code or what?
In general, the question in general is this: is unsafe code really as unsafe and scary as it is painted?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
mafusailmagoga, 2017-10-01
@mafusailmagoga

Rarely use unsafe - because the hell it is needed in a language that is fundamentally improved just by the fact that the code is fully managled.
Certainly not in your example, but for the sake of more serious tasks, you should bother with unsafe.
And direct work with pointers is excluded - because it is precisely with pointers in C ++ that almost the most mistakes happen to programmers.
Plus, the garbage collector works worse when you use pointers.
You think you're cool and write without errors (although you're not, 100% not) and you don't like the limitations of C# - well, don't write in C#, what's the problem?
But to be indignant that in one language there is one ideology, and in another another - somehow strange.
They are very different.
C# and C++ are similar only in syntax.
No need to purely mechanically bring into one language an approach from another language.
Those. there is a situation - I have a class with 150 variables (int), I want to change these variables directly, passing it to another class
The probability of getting side effects is very high.
A class is not just one variable as a rule.
If you increment this variable, the state of the entire class can somehow change a lot.
If you change directly, without letting the class control it, it is not clear what this can lead to.
BUT to me that, 150 functions
And you shouldn't have 150 of them.
See the anti-pattern - the God class.

E
eRKa, 2017-10-01
@kttotto

Your task is normally solved without unsafe code, without crutches and bells and whistles. Just thinking in terms of C++ and writing code in C# is not entirely comparable. If you want to change the values ​​directly, there is a transfer of values ​​by reference, there are out and ref parameters, there are arrays for 150 values ​​of the same type, there are lists (and for heterogeneous ones). You just need to think within the C# ideology.
Although I don’t quite understand the problem of changing variables in a class and why write 150 methods for this. A class is an object that is passed by reference. By passing the object, you will change these variables of the object as you wish. If you want to pass a class field of type int directly somewhere and want it to change there, then pass it as an out / ref parameter.

D
d-stream, 2017-10-01
@d-stream

Anyone who hasn't had gonorrhea is not a hussar -)
Unsafe code is absolutely not terrible and even completely safe if the brains of the writer are enough to foresee all the consequences.
As practice shows, even old women have fake holes. Often on a global scale.
As examples - the same vulnerabilities still appearing on stack overflow and other classic things from the time of Morrison

G
Griboks, 2017-10-01
@Griboks

Everything can be done without pointers. For example, using indexers or arrays. You don't know how to set getters and setters in an array (get; set;)? Read more about properties and find out that they are nothing more than methods to access hidden variables.

I
Ivankon, 2017-10-05
@Ivankon

Pay attention to the new features of the C# 7.0 language, there are safe pointers:
https://docs.microsoft.com/en-us/dotnet/csharp/wha...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question