Answer the question
In order to leave comments, you need to log in
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++;
}
void Inc(int* value){
value++;
}
Answer the question
In order to leave comments, you need to log in
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.
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.
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question