B
beginwithl
hi
1) I realize that objects are created at run time, but compiler should
still be able to determine to what type of object a reference points
to, since code responsible for creating an object of certain type is
already present at compile time!
For example:
Object o=new A();
C c = ( C ) o;
The above code raises an exception. Why can’t compiler look at “Object
o = new A ();” declaration and from there figure out that ‘o’ is
pointing to an instance of type A and thus report an error?
2) Why when unboxing, must the object being unboxed be of exact same
type as a value that was boxed? Why can’t we unbox it to a compatible
type?
Thus, the following would raise an exception:
byte b = 100;
object o = b;
int i = ( int ) o; // exception
3) As far as I understand, compiler is only capable of checking
whether the variable ( call it a ) on the left side of assignment
operator is compatible with the type of variable ( call it b ) on the
right side of operator, but it can’t check whether b points to
instance of type compatible with a?!
class A{…}
class B{…}
void Main()
{
B b = new B();
a = b; // compiler can figure out that b is not compatible with
a, but not much else
}
Are there any other “peculiarities” with regards to ability of a
compiler to recognize the correct type?
4)
int [] i = { 1, 3, 6 ,9 ,16 ,20 };
foreach ( byte A in i )
{
System.Console.WriteLine( A );
}
Why does foreach statement implicitly convert from int to byte?
Meaning, normally an explicit cast would be required to convert from
int to a byte and for a good reason, since there is a possibility of a
data loss. Thus one would expect that foreach would also follow those
rules?!
thank you
1) I realize that objects are created at run time, but compiler should
still be able to determine to what type of object a reference points
to, since code responsible for creating an object of certain type is
already present at compile time!
For example:
Object o=new A();
C c = ( C ) o;
The above code raises an exception. Why can’t compiler look at “Object
o = new A ();” declaration and from there figure out that ‘o’ is
pointing to an instance of type A and thus report an error?
2) Why when unboxing, must the object being unboxed be of exact same
type as a value that was boxed? Why can’t we unbox it to a compatible
type?
Thus, the following would raise an exception:
byte b = 100;
object o = b;
int i = ( int ) o; // exception
3) As far as I understand, compiler is only capable of checking
whether the variable ( call it a ) on the left side of assignment
operator is compatible with the type of variable ( call it b ) on the
right side of operator, but it can’t check whether b points to
instance of type compatible with a?!
class A{…}
class B{…}
void Main()
{
B b = new B();
a = b; // compiler can figure out that b is not compatible with
a, but not much else
}
Are there any other “peculiarities” with regards to ability of a
compiler to recognize the correct type?
4)
int [] i = { 1, 3, 6 ,9 ,16 ,20 };
foreach ( byte A in i )
{
System.Console.WriteLine( A );
}
Why does foreach statement implicitly convert from int to byte?
Meaning, normally an explicit cast would be required to convert from
int to a byte and for a good reason, since there is a possibility of a
data loss. Thus one would expect that foreach would also follow those
rules?!
thank you