Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Structs that can be assigned constant values directly like built-in types

Tags:

.net

struct

Can you make a struct that behaves like one of the built-in classes where you can assign the value directly without calling a property?

ex:

RoundedDouble count;
count = 5;

Rather than using

RoundedDouble count;
count.Value = 5;
like image 892
Jonn Avatar asked Dec 29 '22 07:12

Jonn


1 Answers

You do this via the implicit keyword.

For example, in your case, you'd want something like:

public static implicit operator RoundedDouble(double value)
{
     return new RoundedDouble(value);
}
like image 187
Reed Copsey Avatar answered Feb 23 '23 00:02

Reed Copsey