K
karthick.ramachandran
Hi,
I was just reading this article
http://www.netobjectives.com/resources/downloads/Best_Practices_CSharp_Delegates.pdf
In which the author had mentioned
<quote>
Delegates would appear to be type safe. The compiler will not allow you
to instantiate a delegate by handing it a method with the wrong
signature, and so the user of the delegate can be confident that the
method is callable with the parameters
it expects, and will return the type it expects.
However, this is not true type safety. The signature-checking is the
only checking done by the compiler. If two delegate types are declared
with the same signature, one can be substituted for the other, and the
compiler will not care.
class Soldier
{
// Delegate type declared within a class, so outside entities will
// refer to it as type Soldier.Fireable
public delegate void Fireable();
// Class Soldier has a member of this type
Soldier.Fireable myWeapon;
// When you arm a Soldier you give it a delegate reference
// of type Soldier.Fireable
public void arm(Soldier.Fireable aWeapon) {
myWeapon = aWeapon;
}
// When you tell the soldier to fight, he uses the Soldier.Fireable
// delegate he holds a reference to
public void fight(){
myWeapon()
}
}
class Payroll
{
// This delegate has the same signature as Soldier.Fireable, but it
is
// a different type, being declared within Payroll. It's type is
// Payroll.Fireable
public delegate void Fireable();
}
The point of concern for us is that it turns out that the arm() method
in Soldier, which is declared as accepting a parameter of type
Soldier.Fireable() will, in fact, take a reference to
Payroll.Fireable(). Similarly, the fight() method will happily use it,
even though it is the wrong type.
</quote>
I tried the same program and tired to pass the delegate of
Payroll.Fireable type to the arm() method of Soldier class. Infact I
was not able to pass the type. Compiler gave me an error saying that it
cannot convert Payroll.Fireable to Solder.Fireable.
Am I missing out on something here? Or is the authors claim wrong?
Ideally I will think that I am not getting the point author is trying
to make.
It will be great if somebody can throw some light on this!!
Thanks!
- R Karthick
I was just reading this article
http://www.netobjectives.com/resources/downloads/Best_Practices_CSharp_Delegates.pdf
In which the author had mentioned
<quote>
Delegates would appear to be type safe. The compiler will not allow you
to instantiate a delegate by handing it a method with the wrong
signature, and so the user of the delegate can be confident that the
method is callable with the parameters
it expects, and will return the type it expects.
However, this is not true type safety. The signature-checking is the
only checking done by the compiler. If two delegate types are declared
with the same signature, one can be substituted for the other, and the
compiler will not care.
class Soldier
{
// Delegate type declared within a class, so outside entities will
// refer to it as type Soldier.Fireable
public delegate void Fireable();
// Class Soldier has a member of this type
Soldier.Fireable myWeapon;
// When you arm a Soldier you give it a delegate reference
// of type Soldier.Fireable
public void arm(Soldier.Fireable aWeapon) {
myWeapon = aWeapon;
}
// When you tell the soldier to fight, he uses the Soldier.Fireable
// delegate he holds a reference to
public void fight(){
myWeapon()
}
}
class Payroll
{
// This delegate has the same signature as Soldier.Fireable, but it
is
// a different type, being declared within Payroll. It's type is
// Payroll.Fireable
public delegate void Fireable();
}
The point of concern for us is that it turns out that the arm() method
in Soldier, which is declared as accepting a parameter of type
Soldier.Fireable() will, in fact, take a reference to
Payroll.Fireable(). Similarly, the fight() method will happily use it,
even though it is the wrong type.
</quote>
I tried the same program and tired to pass the delegate of
Payroll.Fireable type to the arm() method of Soldier class. Infact I
was not able to pass the type. Compiler gave me an error saying that it
cannot convert Payroll.Fireable to Solder.Fireable.
Am I missing out on something here? Or is the authors claim wrong?
Ideally I will think that I am not getting the point author is trying
to make.
It will be great if somebody can throw some light on this!!
Thanks!
- R Karthick