C# Programming - Datatypes - Discussion

Discussion :: Datatypes - General Questions (Q.No.1)

1. 

Which of the following statements are correct about data types?

  1. If the integer literal exceeds the range of byte, a compilation error will occur.
  2. We cannot implicitly convert non-literal numeric types of larger storage size to byte.
  3. Byte cannot be implicitly converted to float.
  4. A char can be implicitly converted to only int data type.
  5. We can cast the integral character codes.

[A]. 1, 3, 5
[B]. 2, 4
[C]. 3, 5
[D]. 1, 2, 5

Answer: Option D

Explanation:

No answer description available for this question.

Sundar said: (Feb 2, 2012)  
There is a predefined implicit conversion from byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

You cannot implicitly convert nonliteral numeric types of larger storage size to byte. Consider, for example, the following two byte variables x and y:

byte x = 10, y = 20;

The following assignment statement will produce a compilation error, because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.

byte z = x + y; // Error: conversion from int to byte

To fix this problem, use a cast:

byte z = (byte)(x + y); // OK: explicit conversion

It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size:

int x = 10, y = 20;
int m = x + y;
long n = x + y;

Also, there is no implicit conversion from floating-point types to byte. For example, the following statement generates a compiler error unless an explicit cast is used:

byte x = 3.0; // Error: no implicit conversion from double
byte y = (byte)3.0; // OK: explicit conversion

When calling overloaded methods, a cast must be used. Consider, for example, the following overloaded methods that use byte and int parameters:

public static void MyMethod(int i) {}
public static void MyMethod(byte b) {}
Using the byte cast guarantees that the correct type is called, for example:

MyMethod(5); // Calling the method with the int parameter
MyMethod((byte)5); // Calling the method with the byte parameter

Source:
http://msdn.microsoft.com/en-us/library/5bdb6693(v=vs.71).aspx

Hope this will help you. Have a nice day!

Ibrahim said: (Oct 16, 2013)  
Hello,

I am not sure this statement below is ture:

"If the integer literal exceeds the range of byte, a compilation error will occur. "

integer range is: -2,147,483,648 to 2,147,483,647
Byte range is: 0 to 255

simple declaration of integer:
int i=255;

How compilation error occurs here?

Andrue Cope said: (Oct 27, 2013)  
The question is poorly phrased. 'A' is implying the literal is a byte eg;

Byte compileError = 1000;

'Constant value '1000' cannot be converted to a 'byte''.

Daisy J said: (Aug 7, 2015)  
I don't know, how it is? Anybody can explain clear way?

Sagar Charde said: (Sep 22, 2015)  
Byte rang is 0 - 255, if this rang is exceeds then compiler give error.

Manju said: (Aug 1, 2019)  
Anybody Can explain this "We can cast the integral character codes".

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.