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

Interested in this is because in C++ pointers everywhere and nothing, it seems, is not happening, and in C# need to be sure to write that it is unsafe code. I.e. is the situation - I have a class with 150 variables (int), I want to direct these variables to be changed, passing it to another class, like so:
void IncFirst(){
Storage.firstValue++;
}

Ie I took the function pointed directly where the object is located, BUT to me that 150 functions to write to update everyone? This is nonsense.
Logically you can do this:
void Inc(int* value){
value++;
}

Total we only need one method on any number of variables, but to use a pointer you need to enable unsafe code, but as I understand it, he has a number of problems, such as cannot be debugged (as far as I remember). And everywhere write that it is "Very dangerous" and "Unsafe", though, what really is the danger? There is, for example this phrase from the article on the topic: "the Direct use of pointers disables the security system, "which is the security system then? Ie does not write error messages in this code or what?

In General, the whole issue is this: in reality unsafe code unsafe and terrible as he is painted?
June 14th 19 at 21:03
5 answers
June 14th 19 at 21:05
Unsafe rarely use - because fuck you need it in a language that fundamentally improved just that the code is completely managled.

It's certainly not in your example, but for more serious tasks bother with unsafe.

And direct manipulation of pointers is possible - because it is precisely because pointers in C++ can sometimes be almost more total errors for programmers.

Plus better than running the garbage collector when you use pointers.

Consider yourself cool and writing without error (although it is wrong, 100% wrong) and you don't like the limitations of C# - so don't write in C#, what's the problem?

But to be indignant that in one language, one ideology, and in the other the other - how strange.
Why would the languages with the same ideology.
They are very different.

C# and C++ syntax are similar.
Do not need mechanically to bring into one language from another language.


I.e. is the situation - I have a class with 150 variables (int), I want to direct these variables to be changed, passing it to another class


The probability to obtain third-party effects is very large.
Class it's not just one variable as a rule.
If you inkrementirovanie this variable, the status of the entire class as it may considerably change.
If you change directly, not allowing the class to prokontroliroval - it is unclear where it may lead.


BUT to me that 150 features


And they are not meant to be you 150.
Cm. anti-pattern - class God.
This is not a divine class, because there is a class with a bunch of variables and I need to change by adding or subtracting a single value. The essence is just a bunch of classes performing the same functions. - Cristian92 commented on June 14th 19 at 21:08
and what hinders to do it in the mountains? then a single function-a class method is sufficient. - Mohammad.Cole22 commented on June 14th 19 at 21:11
I can't write in C++ in Unity because there is only C#. - Cristian92 commented on June 14th 19 at 21:14
there is not just these variables, many variables via get are and one variable is made, for example, of the 10 other variables declared in the class, and half of these 10 and 3, and so on. Using an array is not as convenient through the list too. - Cristian92 commented on June 14th 19 at 21:17
, it seems to me that you need to revise programming... mafusailmagoga obviously gave you a tip on how to use arrays. Why to create each separate variable with its own function, if you can create only one array and method with the item number and the type of the desired operation (or several methods for certain operations) with ref? And it will be just one class with a dataset.
Here's an example:
class ClassA
{
 public int [] Values = new int[150];

 public void Int(int numberElement)
{
Values[numberElement]++;
}
 public void Dec(int numberElement)
{
Values[numberElement]--;
}
}

Or this:
static class ClassA
{
 public static void Int(ref Storage storage, int numberElement)
{
storage[numberElement]++;
}
 public static void Dec(ref Storage storage, int numberElement)
{
storage[numberElement]--;
}
}


Or this:
static class ClassA
{
 public static void Int(ref int storage)
{
storage++;
}
 public static void Dec(ref int storage)
{
storage--;
}
}

void Main()
{
 public int [] Values = new int[150];

 ClassA.Inc(Values[0]); // Values[0] will increment by one.
}

And it is unnecessary to create as many classes with functions. - Ahmad.Emard21 commented on June 14th 19 at 21:20
because as you in the array variable to get\set fill? Of course, the right to make all simple variables with simple values in the array of classes which would contain the desired data by variable and the other where there is get, to leave as a variable, although it is not very it looks, for half the value, half lies, and the number in the array to take variables that are crazy, then you need to constantly Array.Find use, which is not very good... - Cristian92 commented on June 14th 19 at 21:23
, indexers, or what?
Hmmm... a Pointer in fact is a number (address) to the corresponding data in memory. That is, we have the memory in the computer in the form of huge data array (take it in the abstract), and the pointer is the number of the array in memory location. That is here to perform the operation over the element you need to start to take a pointer (has to be an array element number), and then over him to cast than wish. - Ahmad.Emard21 commented on June 14th 19 at 21:26
yeah, the flea will come out. I do not have the same getters and cetery, I'm such crap at each individual case will need to write (about indexers). - Cristian92 commented on June 14th 19 at 21:29
if the setters are getters are not the same, then all is well, quite adequately fit in the concept of "close a class variable by calling the function" - Mohammad.Cole22 commented on June 14th 19 at 21:32
if you have properties instead of fields (so there are get and set), you can't use them. The access address is not associated with the property, changing the memory directly. - caesar commented on June 14th 19 at 21:35
June 14th 19 at 21:07
Your task is normally solved without unsafe code without crutches and bells and whistles. Just to think in terms of C++ and writing code in C# not quite comparable. If you want to change directly the values is to pass values by reference, have out and ref parameters 150 for the same type values are arrays, there are lists (for different types) . Just need to think within the ideology of C#.

Although I do not quite understand the problem is to change the variables in the class and what to do to write 150 methods. Class this is the object that is passed by reference. Passing the object, You will change these variables from the object as you want. If you want to transfer somewhere directly in the class field of type int and you want it changed there, then pass it as an out/ref parameter.
OK, how should look the code? I want to change the class of any value. What I don't know. For example:

void IncFirst()
{
Storage.first++;//clearly show that increasing the variable any
}

void IncLast()
{
Storage.last++;//clearly show that increasing the variable any
}

And now we get this:
void Inc(Storage storage)
{
storage.//how to tell you which variable I want to change?
}


Or are you saying that if I do this:
class Storage()
{
int first = 0;
}
class Routine()
{
Storage storage;
void Main()
{
Inc(ref Storage.first);
Console.PrintString(Storage.first.ToString());//there will be 1?
}

void Inc(ref int value)
{
value++;
}
}
- Cristian92 commented on June 14th 19 at 21:10
If You want to access the value at offset, then this is definitely a data structure array. Get array access by index, pass the index. If You care about the names of these variables, create a Dictionary and in addition to the index, you'll be able to key to turn or get a value by key.

Inc(ref Storage.first);
Console.PrintString(Storage.first.ToString());//there will be 1?

If correctly to write the code - Yes, there will be 1.

You need to first create the object
var storege = new Storage;
then the field object to pass to Inc()
Inc(ref sorage.fist); - Mohammad.Cole22 commented on June 14th 19 at 21:13
Rewrote Your example as expected in C# )

public class Storage
{
 public int First = 0;
}

 public class Routine
{
 public void Inc(ref int value)
{
value++;
}
 }

And call:
var storage = new Storage();
var routine = new Routine();
routine.Inc(ref storage.First);
Console.WriteLine(storage.First);
- Cristian92 commented on June 14th 19 at 21:16
why create an object? I have in the code box, he declared. Dictionary cannot be created, because it is not serialized, and I need it. The array of classes suitable. I will try to use ref and I hope it will work, but not sure. - Cristian92 commented on June 14th 19 at 21:19
in the example class, so it is necessary to create, or make it static in order not to create each time. The dictionary is serialized fine. If you pass as a parameter the object itself, then use the ref will not make sense, ref is needed only for significant types. - Ahmad.Emard21 commented on June 14th 19 at 21:22
at first the link "c# serialized if the dictionary" says that no, try and accomplish your goal. - Cristian92 commented on June 14th 19 at 21:25
You have not said what you want to serialize. In General, you can serialize anything and into anything, just define your serialization rules. And in this case it is possible to do the same. Here is an example

Here's another option
// Serialization
 var document = new XElement(
"items",
 dictionary.Select(x => new XElement("item", new XAttribute("id", x.Key), new XAttribute("value", x.Value)))
);
 var xml = document.ToString();
Console.WriteLine(xml);

 // Deserialization
 var document2 = XElement.Parse(xml);
 var obj = document2.Descendants("item")
 .ToDictionary(x => (int)x.Attribute("id"), x => (string)x.Attribute("value"));
- Ahmad.Emard21 commented on June 14th 19 at 21:28
the first link "c# serialized if the dictionary" says that no, try and accomplish your goal.

at first the link "c# serialize dictionary to xml" on Staroverov it is written so. - Cristian92 commented on June 14th 19 at 21:31
June 14th 19 at 21:09
Who gonorrhea was not sick - he is not hussar -)

Unsafe code is not dangerous and quite safe even if the brain writing enough to foresee all consequences.

As practice shows, even the old women are faloye for the dearth. Often of global proportions.
As examples - those still emerging vulnerability on stack overflow and other classic pieces of the days of Morrison
Unsafe code is not dangerous and quite safe even if the brain writing enough to foresee all consequences.


Of course safe.
In the end, the OS and driver is written and many of the accompanying software.

Only in the sense managled that the programmer has focused on solving the ultimate problemthat the customer need, not on the solution of related tasks which are necessary only to the programmer extra effort and time dealing with the fact that you can shoot yourself in the foot - Cristian92 commented on June 14th 19 at 21:12
exactly -) - Mohammad.Cole22 commented on June 14th 19 at 21:15
June 14th 19 at 21:11
All can be done without pointers. For example, using indexers or arrays. You don't know how in the array to define getters and setters (get; set;)? Read more about the properties and learn that it is none other than the methods of access to the hidden variables.
And what, in the array for each value to specify a different getter and cetery? - Cristian92 commented on June 14th 19 at 21:14
Yes, but you will have to write the desired functionality, and do not use autorealizare properties. - Mohammad.Cole22 commented on June 14th 19 at 21:17
June 14th 19 at 21:13
Note on new features of C# language, 7.0 there were safe pointers:
https://docs.microsoft.com/en-us/dotnet/csharp/wha...

Find more questions by tags C#