public class SampleClass {
public int value;
public SampleClass(int v)
{ value = v; }
}
// i want to access value like this
SampleClass sc = new SampleClass(5);
int i = sc;
Is there a way to do this in C#? I don't want to have to say sc.Value every time i need to access the value.
Use an implicit conversion:
public class SampleClass {
public int value;
public SampleClass(int v)
{ value = v; }
public static implicit operator int (SampleClass c)
{
return c.value;
}
}
You should look into properties however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With