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

Any advantage of EventArg as class or struct?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.