C # how to handle ints, how are they an array?

Let's say I have 5 ints that are not an array! , and I want to change them all in 1 cycle. Is there a way to make an array that gets the values ​​of these 5 numbers and can change them directly?

Sort of:

int a = 10; int b = 20; int[] rihanna = // .... ? for(int a=0; a<rihanna.length;a++) rihanna[a]++; Console.WriteLine("{0} {1} ",a,b) // what I want to see here is 11 and 21 
-1
source share
4 answers

I think there is a way to solve the problem if you relax the requirement - to have an array of entities that will allow you to change the set of local variables int through operations on the array.

To do this, you can commit variable references to an array of delegates, each of which accepts ref int val .

 void Increase(ref int x) { x++; } void Set(ref int x, int amount) { x = amount; } void Sample() { int a = 10; int b = 20; // Array of "increase variable" delegates var increaseElements = new Action[] { () => Increase(ref a), () => Increase(ref b) }; increaseElements[0](); // call delegate, unfortunately () instead of ++ Console.WriteLine(a); // writes 11 // And now with array of "set the variable" delegates: var setElements = new Action<int>[] { v => Set(ref a,v), v => Set(ref b,v) }; setElements[0](3); Console.WriteLine(a); // writes 3 } 

Notes

  • directly with the help of delegates you should call them with ().
  • it is possible to fix () instead of ++ , by delegating the delegate to an object that will call Increase as an implementation of ++ ....
  • The problem with the version of Set , in which you need to call (3) instead of = 3 , will require more deception - implementing a custom class with indexing to redirect set [index] to call the saved setter function.

Warning: this is really for entertainment purposes - please do not try in real code.

+4
source

In general, the answer is no . This requires that C # allow reference to an integer (or other type of value) that does not match it (and not without reason.)

The best solution I can offer you is to use a function with the ref parameter. Since you are still trying to use a bunch of arbitrary integer variables, you still have to list them all. How to do this with a function call, for example:

 void Increase(ref int x) { x++; } int a = 10; int b = 20; Increase(ref a); Increase(ref b); 
+4
source

I want a preface with the following statement. Although it does what you want .. it is pretty ugly. This is more in line with what you assumed you could β€œjust do” with C # .. when in fact it is a lot more complicated.

 unsafe { int* a = stackalloc int[1]; int* b = stackalloc int[1]; *a = 10; *b = 20; int*[] rihanna = { a, b }; for (int i = 0; i < rihanna.Length; i++) { (*rihanna[i])++; } Console.WriteLine("{0} {1} ", *a, *b); // "11 21" } 
+4
source

You cannot do this in .NET. int are value types, so you cannot directly refer to them.

However, there are several ways around this. Here is just one:

 class IntReference { int Value { get; set; } } IntReference a = new IntReference() { Value = 10 }; IntReference b = new IntReference() { Value = 20 }; IntReference[] rihanna = { a, b }; for (int i = 0; i < rihanna.Length; i++) rihanna[i].Value = rihanna[i].Value + 1; Console.WriteLine("{0} {1} ", a.Value, b.Value); // "11 21" 

Although you really do not need to do this, never. This contradicts the design of value types in .NET.

+2
source

Source: https://habr.com/ru/post/958044/


All Articles