By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

delegates and type safety !

P: n/a
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,
...
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- ni**************@exisconsulting.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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,
..

Nov 13 '05 #2

P: n/a
Thanks Nicholas,
You are almost like my Online Tutor , Clearing Even my most silly doupts
with utmost clarity...

Thanks once again,
NetYetaNurd.
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:OF**************@TK2MSFTNGP10.phx.gbl...
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]
- ni**************@exisconsulting.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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,
..


Nov 13 '05 #3

P: n/a
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:OF**************@TK2MSFTNGP10.phx.gbl...
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]
- ni**************@exisconsulting.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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,
..


Nov 13 '05 #4

P: n/a
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]
- ni**************@exisconsulting.com

"Simon Trew" <ten.egnaro@werts> wrote in message
news:eT**************@TK2MSFTNGP11.phx.gbl...
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:OF**************@TK2MSFTNGP10.phx.gbl...
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]
- ni**************@exisconsulting.com

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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,
..



Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.