0

I was looking for a similar way to create an alias for something else like its possible in C using preprocessor (this question is a bit similar, couldn't find anything useful there).

This is the problem: I've got a method that receives an array, but each position of the array has a specific meaning, like they where different parameters with specific names. What I want to do is to make my code easier to read (and write) by using those specific names, but, on the other hand, I don't want to create another method call (like in example 1) nor assign the array positions to new variables (example 2), because the performance is critical.

Example 1:

    void OriginalMethodSignature(Type[] values)
    {
        SimplifiedMethod(values[0], values[1], ... values[n]);
    }

    void SimplifiedMethod(Type specificName1, Type specificName2, ... Type specificNameN)
    {
        // simple implementation using specific names instead of values[n]
    }

Example 2:

    void OriginalMethodSignature(Type[] values)
    {
        Type specificName1 = values[0];
        Type specificName2 = values[1];
        ...
        Type specificNameN = values[n];
        // simple implementation using specific names instead of values[n]
    }

I cannot change the method signature because its used in a dellegate, the Type is fixed.

The next example is a bit better, but still not optimum:

    void OriginalMethodSignature(Type[] values)
    {
        // implementation using values[specificName1] ... values [specificNameN]
    }
    const int specificName1 = 0;
    const int specificName2 = 1;
    ...
    const int specificNameN = n-1;

Is there any way to create an snippet for this purpose? If yes, how would it be?

Community
  • 1
  • 1
HericDenis
  • 1,364
  • 12
  • 28
  • 1
    Could you create a private enumeration for each index, then say `values[(int)MyEnum.FirstValue]`? – Nick Gotch Aug 30 '13 at 17:52
  • Yeah, this is better then nothing, I figured out this one too, like the edit I just posted. – HericDenis Aug 30 '13 at 17:53
  • not to start an argument, but `Enums` are like `goto`. Good for very specific use cases, but normally a code-smell. – STW Aug 30 '13 at 18:00
  • I would suggest you start with an easily readable method, run a profiler, and focus on improving the slowest part of the overall code (not just the code around reading your `Type[]`). – STW Aug 30 '13 at 18:07

2 Answers2

3

There isn't any built in way to do what you wan't, because you shouldn't really be doing it at all. You should be using an object with properties instead of an array.

Anyway, you can make an object that encapsulates the array, so that the properties use the array as storage:

public class NamedObject {

  private Type[] _values;

  public NamedObject(Type[] values) {
    _values = values;
  }

  public SpecificName1 { get { return _values[0]; } set { _values[0] = value; } }
  public SpecificName2 { get { return _values[1]; } set { _values[1] = value; } }
  public SpecificName3 { get { return _values[2]; } set { _values[2] = value; } }
  public SpecificName4 { get { return _values[3]; } set { _values[3] = value; } }
  public SpecificName5 { get { return _values[4]; } set { _values[4] = value; } }
  public SpecificName6 { get { return _values[5]; } set { _values[5] = value; } }

}

Now you can use the object to access the array:

void OriginalMethodSignature(Type[] values) {
  NamedObject obj = new NamedObject(values);
  // get a value
  Type x = obj.SpecificName4;
  // set a value
  obj.SpecificName2 = x;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Well, don't solve my problem, but as it says "There isn't any built in way to do what you want" - this is the right answer – HericDenis Aug 31 '13 at 00:34
2

Create a dedicated class or struct, and parse the array into it.

public class MyClassOfStuff
{
    Type SpecificName1 {get;set;}

    Type SpecificName2 {get;set;}

    public static MyClassOfStuff Parse(Type[] value)
    {
        Type specificName1 = values[0];
        Type specificName2 = values[1];
        ...
        Type specificNameN = values[n];
    }
}

 void OriginalMethodSignature(Type[] values)
{
    var mystuff = MyClassOfStuff.Parse(values);
}
STW
  • 44,917
  • 17
  • 105
  • 161
  • Certainly works, but will affect the performance, that's what I am avoiding... I'm looking for some processor-time renaming (I don't know if I am being sufficiently clear, not my mother-tongue) – HericDenis Aug 30 '13 at 17:57
  • when you're coding in .NET your brain needs to stop thinking about C -- it is fast enough :) – STW Aug 30 '13 at 17:59
  • I partially agree @STW, I usually assume that, when I just need clear-good-to-read code, but in this specific situation the performance is really critical, this is a optimization numerical method, I think I'm going for enums or constants definition, will be the faster one and still a bit nice to read – HericDenis Aug 30 '13 at 18:05