Пишу вот простой код:
byte Masha = 10;
byte Petya = 10 * 4;
byte Vanya = Masha + Petya; // ошибка без явного преобразования
А вот так ошибки нету:
byte Masha = 10;
byte Petya = 10 * 4;
byte Vanya =(byte) (Masha + Petya);
Зачем преобразовывать значение int в byte, если я тип указываю byte? что за хрень?
Другие языки программирования и технологии
C# почему когда создаю тип данных byte - он его автоматически преобразовывает в тип int?
Значит есть на то причины. Видимо, целое по умолчанию 32 бита. Следуй правилам компилятора и будет тебе счастье. 10 и 4 это же значения без типа, они явно воспринимаются не как байт. А скорее не они сами, а результат операции (для большей точности).
----------------------------------------------------------------------------------------------
Standard ECMA-334
C# Language Specification
4th edition (June 2006)
14.7.4 Addition operator
For an operation of the form x + y, binary operator overload resolution (§14.2.4) is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.
The predefined addition operators are listed below. For numeric and enumeration types, the predefined addition operators compute the sum of the two operands. When one or both operands are of type string, the predefined addition operators concatenate the string representation of the operands.
• Integer addition:
int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);
void operator +(long x, ulong y);
void operator +(ulong x, long y);
----------------------------------------------------------------------------------------------
Как видишь, оператора, принимающего и возвращающего byte нет. Аргументы типа byte преобразуются к int и вызывается оператор, возвращающий int.
Standard ECMA-334
C# Language Specification
4th edition (June 2006)
14.7.4 Addition operator
For an operation of the form x + y, binary operator overload resolution (§14.2.4) is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.
The predefined addition operators are listed below. For numeric and enumeration types, the predefined addition operators compute the sum of the two operands. When one or both operands are of type string, the predefined addition operators concatenate the string representation of the operands.
• Integer addition:
int operator +(int x, int y);
uint operator +(uint x, uint y);
long operator +(long x, long y);
ulong operator +(ulong x, ulong y);
void operator +(long x, ulong y);
void operator +(ulong x, long y);
----------------------------------------------------------------------------------------------
Как видишь, оператора, принимающего и возвращающего byte нет. Аргументы типа byte преобразуются к int и вызывается оператор, возвращающий int.
Похожие вопросы
- [C++] Программа крашится если тип данных LPSTR
- C++ Про выбор типа данных. Int, char и т. д.
- Что имеется ввиду под 4 байтами у типа данных int?(С++)
- Как преобразовать тип string к типу int? C++
- C++. Типы данных. Создание типа данных.
- Учу C++! Уже знаю: типы данных, циклы. условия, привидения и т. д учу 5дней. вот программа! Как норм за 5 дней?
- C# Почему const uint SIZE не воспринимается массивом, как константа, а int работает? Погрешности с double
- Как в Visual C++ 2008 express перевести тип int в string или char?
- Как на C++ создать массив типа int из стольких элиментов, что бы значение бралось из перемнно count ?
- Вес может ли быть дробным числом? Какой тип данных использовать для этого int или float или double ?
а с плавающей запятой вроде double?
"Я ничего не знаю, но чего-нибудь напишу, чтобы мне ещё баллов добавили"