-1

Is there a way to make my new Class mimic the behavior of an int or any Valuetype?

I want something, so these assignments are valid

MyClass myClass = 1;
int i = myClass;
var x = myClass; // And here is important that x is of type int!

The MyClass looks roughly like this

public class MyClass {
    public int Value{get;set;}
    public int AnotherValue{get;set;}
    public T GetSomething<T>() {..}
}

Every assignment of MyClass should return the Variable Value as Type int.

So far i found implicit operator int and implicit operator MyClass (int value). But this is not 'good enough'.

I want that MyClass realy behaves like an int. So var i = myClass lets i be an int.

Is this even possible?

spender
  • 117,338
  • 33
  • 229
  • 351
  • 5
    `var x = myClass; // And here is important that x is of type int!` I don't think this will ever fly. – spender Nov 15 '17 at 15:38
  • @spender, `implicit operator int` ? – Sinatr Nov 15 '17 at 15:40
  • 1
    a class will never behave like a struct °° cause its a reference type and not a value type. You could use a struct and override the operators. To make your object behave like an integer – Tobias Theel Nov 15 '17 at 15:42
  • `var x = myClass` causing `x` to be of type `int` is a non-starter. – Matthew Watson Nov 15 '17 at 15:42
  • 1
    @Sinatr as discussed by OP, this doesn't (and won't) work for the `var x = myClass`. TBH, it doesn't really make much sense to me to try to do this... Thinking about use cases, it is implicitly convertible to int, so can be used (in most places) instead of an int. OP, why do you want this to be so? – spender Nov 15 '17 at 15:43
  • @splender I ment this `public static implicit operator int(MyClass instance) { return instance.Value; }` so `MyClass myClass = 1;` is valid – CanereCurrere Nov 15 '17 at 15:44
  • You could simply introduce some cast-mechsnism using something like `public static implicit operator int(MyClass b)`, but `x` will still be of type `MyClass` . – MakePeaceGreatAgain Nov 15 '17 at 15:45
  • @spender, the point is I am doing something like this already, a wrapper class around `int` value (kind of enum, but with custom methods) and it works: I can assign int and I can use instance of class as int in expressions. I am not quite sure what you mean. Another question is what doesn't work for OP, `implicit operator` is the way to go. – Sinatr Nov 15 '17 at 15:45
  • 3
    @Sinatr Because `var x = myClass`, x will never be anything other than the type of `myClass`. – DavidG Nov 15 '17 at 15:46
  • @DavidG, thanks, now I understand the point ;) – Sinatr Nov 15 '17 at 15:47
  • @spender In essence i need only one Information. And this is the Value of this Class. Which represents an Id. But with this Id I can get other Informations. So I want the Methods `GetSomething` and store some other information in this class, but if I want to 'work' with this class, it should be as if I'm working with an int. – CanereCurrere Nov 15 '17 at 15:48
  • @CanereCurrere, if you want to call methods you have to cast: `((MyClass)someInt).SomeMethod()` or `MyClass someVar = someInt; someVar.SomeMethod();` – Sinatr Nov 15 '17 at 15:51
  • Note, what *"store some other information in this class"* is not possible as soon as conversion to `int` occurs. The approach is only good if both types can be converted to each other without any loss. Or if you don't mind what other properties will have default value. – Sinatr Nov 15 '17 at 15:58

2 Answers2

1

If you´d created a cast from your class to int as this:

public static implicit operator int(MyClass instance) { return instance.Value; }

you could implicetly cast an instance of MyClass to an int:

int i = myClass;

However you can not expect the var-keyword to guess that you actually mean typeof int instead of MyClass, so this does not work:

var x = myClass;  // x will never be of type int

Apart from this I would highly discourage from an implicit cast as both types don´t have anything in common. Make it explicit instead:

int i = (int) myClass;

See this excellent answer from Marc Gravell for why using an explicit cast over an implicit one. Basically it´s about determing if data will be lost when converting the one in the other. In your case you´re losing any information about AnotherValue, as the result is just a primitive int. When using an explicit cast on the other hand you claim: the types can be converted, however we may lose information of the original object and won´t care for that.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

Is this even possible?

No.

You can convert and cast types to other types implicitly and explicitly, but to have one type supersede another type without any inheritance or interface implementation flies in the face of object-oriented programming principles.

The var keyword is a convenience that tries to guess the type of a value with as much precision as possible. Therefore it can't be forced to represent int when the type is MyClass.

You might consider something like this: var x = (int) myClass;

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
  • `The var keyword is a convenience that tries to guess the type of a value with as much precision as possible.` That's not true. It doesn't guess anything. It simply makes the type of the variable be the type of the expression that initializes it, and every expression has an unambiguous type. There is no guessing involved anywhere. It's merely a convenience preventing you from writing out the type of the variable. – Servy Nov 15 '17 at 16:14
  • Hmm, you're right. The word "guess" was a bad choice. I was trying to communicate the ambiguity that the `var` keyword can have when a variable could easily be of many types on the inheritance hierarchy. – Kyle Delaney Nov 15 '17 at 21:49
  • Again, there is no ambiguity in the type of any variable initialized using `var`. The type is always the type of the expression assigned to it, and every expression always has an unambiguous type. – Servy Nov 15 '17 at 21:50
  • Thanks again. I meant ambiguity from a design perspective. If you weren't using `var` you could pick from several different types for any given variable. – Kyle Delaney Nov 15 '17 at 21:54