0

For example I have struct:

public struct Foo {
    int value;
}

Is there a way to assign the value (maybe by implementing some kind of interface or smthg) directly to instance of an object skipping reference to the field? And maybe chose this field? Something like this:

Foo number = new Foo();

number = 2; // instead of - number.value = 2;
bleat interteiment
  • 1,429
  • 2
  • 10
  • 15
  • 2
    No, this isn't possible. You can get something *similar* with `public static implicit operator Foo(int i) => new Foo { value = i }`, but while this allows the statement to compile, it does not change `number` but rather replaces it with a new instance altogether -- which may not matter, or it may matter a lot, depending on how the struct is used. Such implicit conversion operators on basic types also tend to cause more confusion than they're worth. – Jeroen Mostert Mar 19 '22 at 12:13
  • Yes, thanks you both. I've been googling for this question for way too long :D – bleat interteiment Mar 19 '22 at 12:19

0 Answers0