Is there something like code synonyms in C #?

For example, I have a code:

if (myArray[i, j, k] == someValue) myArray[i, j, k] = someOtherValue; else throw new Exception(string.Format("value {0} is wrong!", myArray[i, j, k])); 

and it seems like a little dangerous. I would like to have some "code synonym", for example:

 var xx => myArray[i, j, k]; if (xx == someValue) xx = someOtherValue; else throw new Exception(string.Format("value {0} is wrong!", xx)); 

where xx not the value of myArray[i, j, k] , but a synonym for this code, and => is a kind of declaration syntax for this construction.

I would like to have this function to ensure that using multiply myArray[i, j, k] will be exactly the same code, without the risk of getting myArray[j, i, k] in some place.

Maybe C # can do something similar, without ugly syntax? I mean safe code, not an unsafe memory pointer. Or is there something wrong with my point of view?


I apologize for the possible incorrect definition of my idea. I would like to use this trick ("code synonyms" or something like that) as a regular language function (? Or a compiler, etc.) Not only in this case. Specialized classes or functions are good in a particular case, but in general they will lead to a significant increase in the amount of code, and I would like to avoid this.

+4
source share
5 answers

Maybe for this you need some kind of function? For instance:

In your code:

 UpdateElement(ref myArray[i, j, k], someValue, someOtherValue); 

And function:

 void UpdateElement(ref int element, int compareWith, int value) { if (element == compareWith) element = value; else throw new Exception(string.Format("Value {0} is wrong!", element)); } 

UPD: Renaming a method from "TryUpdateElement" to "UpdateElement" because Try .. methods usually do not throw an exception.

+4
source

You can implement your own type of matrix that will store the actual index

 public class Matrix<T> { private T[, ,] _array; public Matrix(int sizeX, int sizeY, int sizeZ) { _array = new T[sizeX, sizeY, sizeZ]; } public T this[int i, int j, int k] { get { return _array[i, j, k]; } set { _array[i, j, k] = value; } } public int CurrentI { get; set; } public int CurrentJ { get; set; } public int CurrentK { get; set; } public void SetCurrentCell(int i, int j, int k) { CurrentI = i; CurrentJ = j; CurrentK = k; } public T Current { get { return _array[CurrentI, CurrentJ, CurrentK]; } set { _array[CurrentI, CurrentJ, CurrentK] = value; } } public static implicit operator T(Matrix<T> matrix) { return matrix.Current; } // The assignment operator (=) cannot be overloaded. But we can overload | // instead, allowing us to write: m |= value in order to perform an assignment. public static Matrix<T> operator |(Matrix<T> m, T value) { m.Current = value; return m; } } 

You can use it as follows:

 var m = new Matrix<int>(3, 3, 3); m[0, 2, 1] = 77; m.SetCurrentCell(0, 2, 1); m.Current = 88; int value = m; // Using the implicit operator and the current indexes m |= 20; // Using |= as assignment operator 

The general approach has the disadvantage that it becomes more difficult to implement numerical operations (mathematical operations such as +, -, *, / are not defined for general types). But you can enable a generic type in a blurred class that implements numerical calculations

 public class DoubleMatrix : Matrix<double> { public static DoubleMatrix operator +(DoubleMatrix a, DoubleMatrix b) { //TODO: Implement + operator return m; } //TODO: Implement other operators } 

C # 7.0 introduces ref locators to do just that.

 ref int xx = ref myArray[i, j, k]; if (xx == someValue) xx = someOtherValue; else throw new Exception(string.Format("value {0} is wrong!", xx)); 

Please note that you get not only a synonym, but also a link, which means that array indices are evaluated only once.

+1
source

You can use anonymous delegates for this.

 var get_xx = delegate(){ return myArray[i, j, k];}; var set_xx = delegate(int v){ myArray[i, j, k] = v;}; if (get_xx() == someValue) set_xx(someOtherValue); else throw new Exception(string.Format("value {0} is wrong!", get_xx())); 
0
source

This last sentence provokes obvious answers. It seems you would like to use something like a code generator.

It seems like something like the C preprocessor matches the score:

 #define xx myArray[i, j, k] 

Well, it really is not ... the usual "point of view", but the good news is that you can just use the CPP preprocessor in your C # project.

Ideas:

  • Take the pre-assembly step.
  • generate your .cs from the .cs.cpp file
0
source

You can write yourself a function that allows you to do something like this:

 UpdateMatrixValueIf(myArray, i, j, k, (v) => v == someValue, someOtherValue); 

And implement it as follows:

 public void UpdateMatrixValueIf<T>(T[,,] array, int i, int j, int k, Func<T, bool> check, T newValue) { if (check(array[i, j, k])) array[i, j, k] = newValue; else throw new Exception(string.Format("Value {0} is wrong!", array[i, j, k])); } 

Of course, doing something like this really makes sense if you do it more than once. Otherwise, the answer is simply no, that you cannot do it better or safer. You can use some crazy reflection methods, but I don’t think it would be appropriate. Just make sure that this part of the code is checked by the module and you should not run into problems with indexes.

0
source

All Articles