January 2005 - Posts
|
IL_0000: ldc.r4 (C3 F5 48 40) IL_0005: box [mscorlib]System.Single IL_000a: stloc.0 |
object PI= 3.14F;
|
|
IL_000b: ldloc.0 IL_000c: unbox [mscorlib]System.Single IL_0011: ldind.r4 IL_0012: conv.i4 IL_0013: stloc.1 |
int i = (int)((float)PI);
|
Come to think of it .. as you can see unboxing with converstion does the following
1. Unbox from object PI
2. Loads a value of type float32 as a type F (float) onto the evaluation stack indirectly.
3.Converts the value on top of the evaluation stack to int32.
Try
int i = (int)PI;
Check out what happens at runtime. ;)
I just had to put this down.. pretty neat as to where XML can lead and where you can take XML now.
|
public class A
{
public int x=0;
}
public class Program
{
public static void Main()
{
A a = null;
Method(a); //Reference of a is copied onto the stack of the method
System.Console.WriteLine(a.x); //Method does not affect the //value of ref of a as its passed by val.
}
public static void Method(A obj) //Call by value and so null is copied onto obj
{
obj = new A();
obj.x = 10;
}
}
|
Well lets reinterpret this and we end up correcting ourself and we say the the value in the object's reference is copied when the method parameters are passed by value.
This means that a on the stack for the method there is a place holder for A's reference and into this, Null is copied (remember its not by ref). Now when the method returns nothing happens of the original place holder for A inside the Main method. This implies that the object A still has null as the method will not modify the original value inside a as its pass by value.
Hmm theory and pratice sometimes require quite deeper transcription or else the meaning can be quite lost.
So i guess saying that objects are passed by referencec is not enough and rather we should say the reference of the object is copied if its passed by value.
--Oops forgot to mention that there would be a null value exception thrown.
This was the objective as the object a is still null and does not get the instance assigned by the method.
Thinking about type-safe down-casting just wrote this code snippet for some self assurance. It doesnt implement down casting.I'd rather say its just the opposite that is attempted.
|
using System;
class A
{
public virtual void Display()
{
Console.WriteLine("A");
}
}
class B:A
{
public override void Display()
{
Console.WriteLine("B");
}
}
class MainClass
{
public static void Main()
{
B obj = new B();
A objA = obj;
((A)obj).Display(); //Is it A or B
objA.Display();
} } |
First thought was what would the output to this be .
hmm.. then there came the point where i wanted to get to the parents display member using the object of the derived class.I realize that its not going to happen but still I'd like some reference as to why this kind of an implementation is done incase of late binding inspite of specifying the type as that of the parent.
Well the good thing is this implementation works same in java and delphi so i guess there should be a rather sane explanation which i would like to know. Is this a standard or a definition for late binding ? But then the concept of casting for type is applicable only for down casting isnt it or i guess runtime type has the final say.
I hope someone can throw some light on this.