delegates and type safety !

  • Thread starter Thread starter NotYetaNurd
  • Start date Start date
N

NotYetaNurd

Hi all,

I read that delegates are type safe and Functional pointers are not!!...
in this context what does type safety mean?
can anyone of you throw some light on it

regards,
...
 
NetYetaNurd,

When it is said that delegates are type safe, consider the following:

public delegate void MyDelegate(int myParam);

This definition means that when I pass around a delegate, it MUST point
to a method that has the signature of one integer parameter, and no return
value. When using function pointers, it is just a pointer, and I can cast
it to anything, which could lead to problems if I cast it to the wrong
thing.

Hope this helps.
 
Thanks Nicholas,
You are almost like my Online Tutor , Clearing Even my most silly doupts
with utmost clarity...

Thanks once again,
NetYetaNurd.


Nicholas Paldino said:
NetYetaNurd,

When it is said that delegates are type safe, consider the following:

public delegate void MyDelegate(int myParam);

This definition means that when I pass around a delegate, it MUST point
to a method that has the signature of one integer parameter, and no return
value. When using function pointers, it is just a pointer, and I can cast
it to anything, which could lead to problems if I cast it to the wrong
thing.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

NotYetaNurd said:
Hi all,

I read that delegates are type safe and Functional pointers are not!!...
in this context what does type safety mean?
can anyone of you throw some light on it

regards,
..
 
Well yeah, if you *cast* it, but then that's true of many kinds of cast
operation in C/C++. It's not the fact that it's a function pointer that
makes it unsafe, but that a *cast* on it may be unsafe.

But function pointers are typed, just like any other pointers are typed. So
I don't know why this claim for delegates being typesafe is anything new.

S.

Nicholas Paldino said:
NetYetaNurd,

When it is said that delegates are type safe, consider the following:

public delegate void MyDelegate(int myParam);

This definition means that when I pass around a delegate, it MUST point
to a method that has the signature of one integer parameter, and no return
value. When using function pointers, it is just a pointer, and I can cast
it to anything, which could lead to problems if I cast it to the wrong
thing.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

NotYetaNurd said:
Hi all,

I read that delegates are type safe and Functional pointers are not!!...
in this context what does type safety mean?
can anyone of you throw some light on it

regards,
..
 
Simon,

Pointers in C++ are just integers that are a reference to a point in
memory which can be coerced into any other pointer type. This is not the
case in .NET. In .NET, references actually have a type associated with
them, and assignments to that are checked for validity (in other words, you
can not just cast around it).

Because of this, I could not take a delegate wrapping one method, and
coerce (cast) it into another delegate type with a different signature. I
could do this in C++ with function pointers (or cast any pointer to a
function pointer actually), often with unpredictable and undesirable
results.

I would argue that pointers in C++ are not type safe because you can
easily manipulate (cast) the pointer to any other pointer type. I would
even go further to say that there really is only one kind of pointer, that
the type doesn't really matter, because I can do this cast, effectively
rendering it useless.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Simon Trew said:
Well yeah, if you *cast* it, but then that's true of many kinds of cast
operation in C/C++. It's not the fact that it's a function pointer that
makes it unsafe, but that a *cast* on it may be unsafe.

But function pointers are typed, just like any other pointers are typed. So
I don't know why this claim for delegates being typesafe is anything new.

S.

in message news:[email protected]...
NetYetaNurd,

When it is said that delegates are type safe, consider the following:

public delegate void MyDelegate(int myParam);

This definition means that when I pass around a delegate, it MUST point
to a method that has the signature of one integer parameter, and no return
value. When using function pointers, it is just a pointer, and I can cast
it to anything, which could lead to problems if I cast it to the wrong
thing.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

NotYetaNurd said:
Hi all,

I read that delegates are type safe and Functional pointers are not!!...
in this context what does type safety mean?
can anyone of you throw some light on it

regards,
..
 
Back
Top