J
Jon Skeet [C# MVP]
K Viltersten said:I'm working on a project where i use a
lot of sbyte-type variables. I notice
that most (all?) operators tend to cast
the result into int type.
While i can easily recast e.g.
sbyte something = else % 10;
into
sbyte something = (sbyte)(else % 10);
it make the code look less readable
and is a bit tedious. I've been thaugt
that overloading is a no-no, but
perhaps it's a good idea to do it here.
1. Can i overload e.g. modulo operator
using some sort of extension method,
in such manner so it looks like "%"
still but returns sbyte type?
2. Should i do so?
3. How (if not easily googleable and/or
connected to evil gotchas)?
Do all your sbyte variables actually have the same logical type? If so,
why not encapsulate that in your own type, instead of using sbyte? (It
would probably be *backed* by an sbyte).
Then you can define your own operators for your type, which will
probably all return values of the same type.