C# Class as one Value in Class // Mimic int behaviour
Asked Answered
O

2

-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?

Oberhausen answered 15/11, 2017 at 15:37 Comment(13)
var x = myClass; // And here is important that x is of type int! I don't think this will ever fly.Hostage
@spender, implicit operator int ?Segal
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 integerJillion
var x = myClass causing x to be of type int is a non-starter.Fixate
@Segal 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?Hostage
@splender I ment this public static implicit operator int(MyClass instance) { return instance.Value; } so MyClass myClass = 1; is validOberhausen
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 .Spermatozoon
@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.Segal
@Segal Because var x = myClass, x will never be anything other than the type of myClass.Lime
@DavidG, thanks, now I understand the point ;)Segal
@Hostage 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<T> 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.Oberhausen
@CanereCurrere, if you want to call methods you have to cast: ((MyClass)someInt).SomeMethod() or MyClass someVar = someInt; someVar.SomeMethod();Segal
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.Segal
S
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.

Spermatozoon answered 15/11, 2017 at 15:49 Comment(0)
F
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;

Frippery answered 15/11, 2017 at 15:50 Comment(4)
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.Matt
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.Frippery
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.Matt
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.Frippery

© 2022 - 2024 — McMap. All rights reserved.