I’ve normally been creating Prism Events used by the EventAggregator like:
public class SomeEvent : CompositePresentationEvent<SomeEventArgs> { }
public class SomeEventArgs
{
public string Name { get; set; }
}
But while looking at a co-workers code I noticed they did:
public class SomeEvent : CompositePresentationEvent<SomeEvent>
{
public string Name { get; set; }
}
I guess my first question is why does this even compile? It seems to me that it’s implementing a class that isn’t defined yet. And second, does it negatively affect the application at all, is it negligible, or better?
Which rule in the spec do you believe it’s violating?
I think you’d have to specify the exact meaning of each of those terms for the statement to be judged as accurate or not. The compiler knows about
CompositePresentationEventas it’s declared elsewhere (presumably) and it knows aboutSomeEventbecause that’s the class being declared. It’s like having a field of typeFoowithin a classFoo– entirely valid.It’s also very useful to be able to do this – particularly for comparisons. For example:
says that any instance of class
Fooknows how to compare itself with another instance, so it can be used for sorting in a type-safe way. In the case of structs, this also allows you to reduce boxing, as callingx.CompareTo(y)won’t need any boxing whenxis known to be of a type which implementsIComparable<>appropriately.Note that types can get far more interestingly and confusingly recursive. Take this (slightly modified) example from my port of Protocol Buffers:
Here, the aim is to basically end up with two types – a “message” and a “builder” so that you can always construct each from the other. For example: