Any advantage of EventArg as class or struct?

  • Thread starter Thread starter Dan H.
  • Start date Start date
D

Dan H.

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
 
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,
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 said:
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]
- (e-mail address removed)

Dan H. said:
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
 
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]
- (e-mail address removed)

Dan H. said:
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



message news:[email protected]...
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]
- (e-mail address removed)

Dan H. said:
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
 
How can you inherit from EventArgs in a struct, u cant inherit on structs.
Ill be blody amazed if u can.


Dan H. said:
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



message news:[email protected]...
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]
- (e-mail address removed)

Dan H. said:
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
 
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 said:
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]
- (e-mail address removed)

Dan H. said:
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



message news:[email protected]...
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]
- (e-mail address removed)

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
 
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]
- (e-mail address removed)

Dan H. said:
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

message news:[email protected]...
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]
- (e-mail address removed)

Dan H. said:
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]" <[email protected]>
wrote
in
message 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]
- (e-mail address removed)

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
 
Back
Top