-1

I want my program to be configurable what type of int it should use( most likely between int16 and int32) in order to save space.

I want to determine the type at the beginning of my code, so i just need to change one place instead of change all int16 to int32.

Could there be something like:

Type myIntType = int16;// or int32
//just change here
.....
.....
//lost of use of myIntType
List<myIntType> arr = new List<myIntType>();
.....

In a small case the int list will hold numbers between 0 and 10,000 so UInt16 should be good.

But for large case the int list will hold numbers between 0 and 1,000,000 so should use Int32

I even wish there could be int20.

And the size of the list could be very large, so space intense.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Benny Ae
  • 1,897
  • 7
  • 25
  • 37
  • I think you are looking for generics, but its not clear. – maccettura Sep 14 '18 at 16:12
  • 3
    This is a bad idea. – Joel Coehoorn Sep 14 '18 at 16:12
  • No, you can't use a variable to set the generic parameter. Please add more about what you want to _do_ with that list to get a better solution. – D Stanley Sep 14 '18 at 16:14
  • This is a bad idea, but you can do it (per file) with `using myIntType = System.Int16;` at the top of your file... – Ron Beyer Sep 14 '18 at 16:19
  • You can *sort of* get this effect with `using MyList = System.Collections.Generic.List;` This *only* introduces an alias for the type, though, so it won't be easy to write code that works correctly in all circumstances. In general, you should find that this sort of switch simply does not pay off -- if your data doesn't need `int` but can fit a `short`, why not use that always? If your data may not fit a `short`, why not use `int` always? And most importantly, how have you established this is even an actual problem for memory use, worth solving? – Jeroen Mostert Sep 14 '18 at 16:19
  • If you can effectively predict which cases apply when, you shouldn't need one global-fits-all switch to change all lists on the fly -- just use the right type for the right case. You should need only a limited number of overloads that do different things based on whether you're dealing with a `List` or a `List` or a `List`. A 20-bit integer type would be very hard to process efficiently -- you're much better off shopping for extra RAM than having the processor shift around bits. You *could* write your own variable-length integer type, but don't expect good perf. – Jeroen Mostert Sep 14 '18 at 16:28
  • You could probably do this with some environment variable, hard to maintain code, and a heavily modified IDE but instead you should explain what your end goal actually is – Sayse Sep 14 '18 at 16:58

1 Answers1

0

You could use dynamic type in C# (I am not sure which version it was introduced in but the latest one should have it)

    List<dynamic> l = new List<dynamic>();
    dynamic d1 = 1;
    dynamic d2 = (Int16)1;

    l.Add(d1);
    l.Add(d2);
    //Your code goes here
    Console.WriteLine(d1.GetType());
    Console.WriteLine(d2.GetType());

    l.ForEach( x => Console.WriteLine(x.GetType()));
adityap
  • 329
  • 1
  • 10
  • 1
    If the intent is to save memory, `dynamic` isn't going to cut it, because that's an object reference under the covers. It's no better than `int` and twice as bad in a 64-bit setting... and that's not even touching the runtime cost of `dynamic`, which is considerable when compared to a native type. – Jeroen Mostert Sep 14 '18 at 16:21
  • Thanks for pointing it out @JeroenMostert. For the community, here's the explanation to mem usage on dynamic I found - https://stackoverflow.com/questions/41686630/memory-usage-of-dynamic-type-in-c-sharp I am still leaving the answer here just in case somebody is looking for a way it can be done – adityap Sep 14 '18 at 16:25