'Where are the parentheses in an explicit convertion written? [closed]
Why is an explicit conversion written like
int operand = 1;
byte result = (byte)operand;
instead of
int operand = 1;
byte result = byte(operand);
?
Is byte in the former treated as an operator?
Is byte in the latter treated as a method?
Thanks.
Solution 1:[1]
Yes, it is indeed an (unary) operator. You can even create your own ones using following syntax:
public static explicit operator MyType(OtherType t)
{
// return instance of MyType
}
Solution 2:[2]
It's written inside of the parameters. It comes from C/C++. There's no method called byte. It's an explicit cast defined by .NET. byte is a C# keyword/alias for System.Byte.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | adjan |
| Solution 2 | halfer |
