473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

delegates and type safety !

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
4 14075
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
3
by: karthick.ramachandran | last post by:
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...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
9
by: Scott Gifford | last post by:
I have a question about concurrency and delegates. Say I have a class roughly like this: public class Class1 { public delegate void MyEventHandler(); private event MyEventHandler onMyEvent;...
12
by: tshad | last post by:
I have a set up javascript functions that pass function pointers and I am trying to figure out how to do the same thing in C# using delegates. // We define some simple functions here function...
9
by: eric | last post by:
I'm trying to define a dictionary whose value is an Generic Action<> delegate private Dictionary<string, List<Action<T>>> Any ideas on to how specify T or a different collection type more...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.