Is it possible to wrap an integer and call it an integer?

I want to make int in IDictionary<int, Driver> little less obscure regarding its meaning, so I thought that maybe packing would be a good idea. The result will be similar to IDictionary<finishPosition, Driver> .

Preferably, finishPosition should be assigned as follows finishPosition = 1 .

I do not know if this is possible and how.

+6
source share
5 answers

You can use struct with implicit conversion.

 public struct FinishPosition { public readonly int Position; public FinishPosition(int position) { this.Position = position; } public static implicit operator FinishPosition(int position) { return new FinishPosition(position); } } // ... Dictionary<FinishPosition, Driver> dict = new Dictionary<FinishPosition, Driver>(); Driver bob = new Driver(); // The following two lines are effectively equivalent dict[new FinishPosition(7)] = bob; dict[7] = bob; // So are these two bob = dict[7]; bob = dict[new FinishPosition(7)] 
+5
source share

Absolutely just use this with the directive:

using FinishPosition = System.Int32;

at the top of your source file and write the code in the same way as in your example.

If you use the use directive like this, FinishPosition is int, and other files will see it as int - there is no new FinishPosition type defined (thanks for the configurator in the comments).

+5
source share

You can use the enumeration:

 enum Position { Finish, Some, Other } IDictionary<Position, Driver> lookup = ...; Driver d = lookup[Position.Finish]; 
+1
source share

In my opinion, it is useful to use the AbstractDataType template wherever the general value is used with specific intentions (this case was made by many others with examples such as Percentage, ReadonlyString, ConnectionString, etc.)

Please note that I personally believe that with and from implicit conversions like all this is empty (having an implicit transform, there is no longer a compiler to guarantee that common values ​​are used with a specific intent in the right place).

Here is a pattern that should inspire you to be your own: you can still choose the level of convenience / verbosity that you prefer. He shows two approaches:

  • _byInt index PseudoInt dictionary
  • _byEnum indexes a dictionary by type Enum: int

Notes

  • If you are #define IMPLICIT_CONVERSIONS, you will get implicit conversions to the base index type (so that int or enumerate)
  • this is extrapolated to all types of values, including user-defined ones (try it with the XYZCoord structure :))
  • see if you really need conversions to and from Enum types

Without further ado:

 #define IMPLICIT_CONVERSIONS using System.Collections.Generic; namespace NS { public enum PositionEnum : int { Begin = 0, Normal = 1, End = 99 } public struct Pseudo<T> where T : struct { public T Value; public Pseudo(T value) { Value = value; } #if IMPLICIT_CONVERSIONS public static implicit operator T(Pseudo<T> pi) { return pi.Value; } public static implicit operator Pseudo<T>(T ni) { return new Pseudo<T>(ni); } #endif } static class Program { private static Pseudo<T> AsPseudo<T>(this T value) where T : struct { return new Pseudo<T>(value); } private static readonly IDictionary<Pseudo<int>, string> _byInt = new Dictionary<Pseudo<int>, string>() { { 0, "aap" }, { 1, "noot" }, { 99, "mies" }, }; private static readonly IDictionary<Pseudo<PositionEnum>, string> _byEnum = new Dictionary<Pseudo<PositionEnum>, string>() { { PositionEnum.Begin, "aap" }, { PositionEnum.Normal, "noot" }, { PositionEnum.End, "mies" }, }; public static void Main(string[] args) { string s; s = _byInt[0]; s = _byEnum[PositionEnum.Normal]; } } } 
+1
source share

I just adapted the RichEnum base class in the question that I linked before [1]

I would say that I prefer this method because it really seems the most flexible in the long run. The only drawback I can imagine is that the RichEnum type can no longer be a value type.

Go to see what I mean (includes a fully running sample again):

[1] Can we define implicit conversion of enumerations in C #?

0
source share

All Articles