473,396 Members | 1,789 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,396 software developers and data experts.

Any advantage of EventArg as class or struct?

Hello,
Is there any advantage of using a struct or a class as part of

public delegate bool MyEvent(object source, SomeEventArgs e);

Struct:
public struct SomeEventArgs {
public float SomeFloat1;
public float SomeFloat2;
}

Class
public class SomeEventArgs {
public void SomeEventArgs {
}
public float SomeFloat1;
public float SomeFloat2;
}

Does this decision impact Garbage Collecting?
Execution Speed?

Thanks in advanced!!!

Dan
Nov 15 '05 #1
6 7528
Dan,

Generally, the model is that you use a class derived from EventArgs in
your event handler. This is the model that has been established for the
..NET framework.

However, in terms of performance, if you use a structure, you are going
to have a performance impact because the contents of that structure have to
be copied for each delegate that is called. Granted, for reference types,
the reference has to be copied for the call to each delegate, but this is
typically a much smaller amount of data than a full-blown structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hello,
Is there any advantage of using a struct or a class as part of

public delegate bool MyEvent(object source, SomeEventArgs e);

Struct:
public struct SomeEventArgs {
public float SomeFloat1;
public float SomeFloat2;
}

Class
public class SomeEventArgs {
public void SomeEventArgs {
}
public float SomeFloat1;
public float SomeFloat2;
}

Does this decision impact Garbage Collecting?
Execution Speed?

Thanks in advanced!!!

Dan

Nov 15 '05 #2
Nicholas,
Just making sure I understand...

If its a class, then you are just passing the pointer to that instance.

If its a struct, given the example, then both somefloat1 and somefloat2
passed and copied.

IOW, structs requires more work/time. Passing (somefloat1 and somefloat2)
takes longer than just the class instance pointer. This impact is
multiplied the more subscriptions to that event, since SomeEventArgs gets
passed each time.

Sound right?

Thanks!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP12.phx.gbl...
Dan,

Generally, the model is that you use a class derived from EventArgs in
your event handler. This is the model that has been established for the
.NET framework.

However, in terms of performance, if you use a structure, you are going to have a performance impact because the contents of that structure have to be copied for each delegate that is called. Granted, for reference types,
the reference has to be copied for the call to each delegate, but this is
typically a much smaller amount of data than a full-blown structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hello,
Is there any advantage of using a struct or a class as part of

public delegate bool MyEvent(object source, SomeEventArgs e);

Struct:
public struct SomeEventArgs {
public float SomeFloat1;
public float SomeFloat2;
}

Class
public class SomeEventArgs {
public void SomeEventArgs {
}
public float SomeFloat1;
public float SomeFloat2;
}

Does this decision impact Garbage Collecting?
Execution Speed?

Thanks in advanced!!!

Dan


Nov 15 '05 #3
How can you inherit from EventArgs in a struct, u cant inherit on structs.
Ill be blody amazed if u can.
"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Nicholas,
Just making sure I understand...

If its a class, then you are just passing the pointer to that instance.

If its a struct, given the example, then both somefloat1 and somefloat2
passed and copied.

IOW, structs requires more work/time. Passing (somefloat1 and somefloat2)
takes longer than just the class instance pointer. This impact is
multiplied the more subscriptions to that event, since SomeEventArgs gets
passed each time.

Sound right?

Thanks!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:u8**************@TK2MSFTNGP12.phx.gbl...
Dan,

Generally, the model is that you use a class derived from EventArgs in your event handler. This is the model that has been established for the
.NET framework.

However, in terms of performance, if you use a structure, you are

going
to have a performance impact because the contents of that structure have

to
be copied for each delegate that is called. Granted, for reference types, the reference has to be copied for the call to each delegate, but this is typically a much smaller amount of data than a full-blown structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hello,
Is there any advantage of using a struct or a class as part of

public delegate bool MyEvent(object source, SomeEventArgs e);

Struct:
public struct SomeEventArgs {
public float SomeFloat1;
public float SomeFloat2;
}

Class
public class SomeEventArgs {
public void SomeEventArgs {
}
public float SomeFloat1;
public float SomeFloat2;
}

Does this decision impact Garbage Collecting?
Execution Speed?

Thanks in advanced!!!

Dan



Nov 15 '05 #4
Dan,

For the most part, yes. Just remember, in .NET, these aren't pointers,
but references that you are passing around. The difference is that what a
reference is pointing to can jump around (the managed object itself), but
the reference will always be able to find it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Nicholas,
Just making sure I understand...

If its a class, then you are just passing the pointer to that instance.

If its a struct, given the example, then both somefloat1 and somefloat2
passed and copied.

IOW, structs requires more work/time. Passing (somefloat1 and somefloat2)
takes longer than just the class instance pointer. This impact is
multiplied the more subscriptions to that event, since SomeEventArgs gets
passed each time.

Sound right?

Thanks!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:u8**************@TK2MSFTNGP12.phx.gbl...
Dan,

Generally, the model is that you use a class derived from EventArgs in your event handler. This is the model that has been established for the
.NET framework.

However, in terms of performance, if you use a structure, you are

going
to have a performance impact because the contents of that structure have

to
be copied for each delegate that is called. Granted, for reference types, the reference has to be copied for the call to each delegate, but this is typically a much smaller amount of data than a full-blown structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hello,
Is there any advantage of using a struct or a class as part of

public delegate bool MyEvent(object source, SomeEventArgs e);

Struct:
public struct SomeEventArgs {
public float SomeFloat1;
public float SomeFloat2;
}

Class
public class SomeEventArgs {
public void SomeEventArgs {
}
public float SomeFloat1;
public float SomeFloat2;
}

Does this decision impact Garbage Collecting?
Execution Speed?

Thanks in advanced!!!

Dan



Nov 15 '05 #5
Kinda a related question but....

Where is the proper place to declare the delegate?

Within the class? Or outside the class but in the same namespace?

Thanks for your time!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ov**************@TK2MSFTNGP11.phx.gbl...
Dan,

For the most part, yes. Just remember, in .NET, these aren't pointers, but references that you are passing around. The difference is that what a
reference is pointing to can jump around (the managed object itself), but
the reference will always be able to find it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Nicholas,
Just making sure I understand...

If its a class, then you are just passing the pointer to that instance.

If its a struct, given the example, then both somefloat1 and somefloat2
passed and copied.

IOW, structs requires more work/time. Passing (somefloat1 and somefloat2)
takes longer than just the class instance pointer. This impact is
multiplied the more subscriptions to that event, since SomeEventArgs gets passed each time.

Sound right?

Thanks!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP12.phx.gbl...
Dan,

Generally, the model is that you use a class derived from EventArgs in your event handler. This is the model that has been established for
the .NET framework.

However, in terms of performance, if you use a structure, you are

going
to have a performance impact because the contents of that structure
have to
be copied for each delegate that is called. Granted, for reference

types, the reference has to be copied for the call to each delegate, but this is typically a much smaller amount of data than a full-blown structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
> Hello,
> Is there any advantage of using a struct or a class as part of
>
> public delegate bool MyEvent(object source, SomeEventArgs e);
>
> Struct:
> public struct SomeEventArgs {
> public float SomeFloat1;
> public float SomeFloat2;
> }
>
> Class
> public class SomeEventArgs {
> public void SomeEventArgs {
> }
> public float SomeFloat1;
> public float SomeFloat2;
> }
>
> Does this decision impact Garbage Collecting?
> Execution Speed?
>
> Thanks in advanced!!!
>
> Dan
>
>



Nov 15 '05 #6
Dan,

It depends on what the event is. If the type that derives from
EventArgs is going to be used for multiple events across multiple objects,
then I would definte the delegate in the same namespace as the objects. For
example, EventHandler is defined in the System namespace.

However, if the EventArgs-derived class is specific to the class that is
firing the event, then I would declare the EventArgs and the delegate as
nested class definitions (for the EventArgs class, and declare the delegate
inside the class as well).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Kinda a related question but....

Where is the proper place to declare the delegate?

Within the class? Or outside the class but in the same namespace?

Thanks for your time!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ov**************@TK2MSFTNGP11.phx.gbl...
Dan,

For the most part, yes. Just remember, in .NET, these aren't

pointers,
but references that you are passing around. The difference is that what a
reference is pointing to can jump around (the managed object itself), but the reference will always be able to find it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Nicholas,
Just making sure I understand...

If its a class, then you are just passing the pointer to that instance.
If its a struct, given the example, then both somefloat1 and somefloat2 passed and copied.

IOW, structs requires more work/time. Passing (somefloat1 and somefloat2) takes longer than just the class instance pointer. This impact is
multiplied the more subscriptions to that event, since SomeEventArgs gets passed each time.

Sound right?

Thanks!
Dan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP12.phx.gbl...
> Dan,
>
> Generally, the model is that you use a class derived from EventArgs
in
> your event handler. This is the model that has been established for the > .NET framework.
>
> However, in terms of performance, if you use a structure, you
are going
> to have a performance impact because the contents of that structure

have to
> be copied for each delegate that is called. Granted, for reference

types,
> the reference has to be copied for the call to each delegate, but

this is
> typically a much smaller amount of data than a full-blown structure.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "Dan H." <Da********@NO.comcast.SsPpAaMm.net> wrote in message
> news:O4**************@tk2msftngp13.phx.gbl...
> > Hello,
> > Is there any advantage of using a struct or a class as part of
> >
> > public delegate bool MyEvent(object source, SomeEventArgs e);
> >
> > Struct:
> > public struct SomeEventArgs {
> > public float SomeFloat1;
> > public float SomeFloat2;
> > }
> >
> > Class
> > public class SomeEventArgs {
> > public void SomeEventArgs {
> > }
> > public float SomeFloat1;
> > public float SomeFloat2;
> > }
> >
> > Does this decision impact Garbage Collecting?
> > Execution Speed?
> >
> > Thanks in advanced!!!
> >
> > Dan
> >
> >
>
>



Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: redhotsly | last post by:
Hi, Is is possible to create an event handler method that can handle any type of event no matter what the delegate signature is. Here is the code I have so far: public class class1 {...
3
by: redhotsly | last post by:
Hi, Is is possible to create an event handler method that can handle any type of event no matter what the delegate signature is. Here is the code I have so far: public class class1 {...
3
by: yinglcs | last post by:
I am reading the Boost scoped_ptr library, and I wonder what is the advantage of using that. Here is an example from Boost.org web site: #include <boost/scoped_ptr.hpp> #include <iostream> ...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.