124 points · by yread · 14y ago · 39 comments
public static IEnumerable<CalculatedTax> CalculateTaxes<T>(this T t)
where T : IOrderItems, IAddress {}
This adds an extension method to any type that implements those interfaces. Using this technique I have been able to make my interfaces much smaller and have generic functions which apply to many different types.Here's what it would might look like in Haskell:
calculateTaxes :: (OrderItems a, Address a) => a -> [CalculatedTax]Here's a full example in some production code. I ended up doing it this way to simplify some crazy legacy code. There were a bunch of different classes that provide order items and extended prices in different ways. With this setup as long as those types implement the necessary interfaces, they get those extension methods for free.
orderItems.CalculateTaxes();
If orderItems implements IOrderItems.Just have to include a using statement for the namespace it's in, it's an extension method. Look them up if you're unsure.
I didn't know you could do that with them, it's pretty cool. Although it's kinda what abstract classes and inheritance is for, but you can only inherit from one in C#.
A better title to this article would have been "Less Understood Features of C#," as things like nullable types, boxing and unboxing using as/is, readonly variables, Nullable<T>, type inferrence and most of the rest of the articles content should not be hidden to anyone who intends to have more than passing knowledge of the language. Most of the things mentioned by the post benefit from having the additional explanation so they can be understood well though.
public void IEnumerable<T>
{
IEnumerator<T> GetEnumerator();
}
The T type is only used in outputs in all the members. Therefore it can be declared with the `out` directive (i.e. public void IEnumerable<out T>). Covariance & contravariance doesn't work with classes, only with interfaces & delegates. Consider this delegate: public delegate void Action<in T>(T obj);
This can be declared with covariance (the in directive) because it only uses T in it's inputs. So an Action<string> can be passed into a method that takes Action<object>.Also, note that this doesn't work with T Func<T>(T obj) because it takes T as input & returns it (output). However, it does work with TReturn Func<in T, out TReturn>(T obj) because neither T nor TReturn is taken as both input & output.
Does this make more sense? It's a hard concept to fully understand
I don't want to sound snobbish, but at least 90% of that list I consider as a basic C# knowledge. If you don't know how to make an enumerable with yield, or insert some debug code with DEBUG or guard resource with using(), what exactly you know?
http://csharpindepth.com/Articles/Chapter11/StreamingAndIter...
I had used it to make a state machine but didn't know its relation to IEnumerable. The article was enlightening to a C# latecomer like me.
Instead of writing this pattern all the time:
StreamWriter writer = null;
try {
writer = new StreamWriter(filename);
// etc.
} finally {
if (writer != null) {
writer.Close();
}
}
You can just write this: using (StreamWriter writer = new StreamWriter(filename)) {
// etc.
}
Which does exactly the same thing. try { dobadthing(); }
finally { cleanup(); }
calls dobadthing(), which throws an exception; then cleanup() is called in case some recovery can be done, then the exception is thrown as if there were no try block.Otherwise yeah, no real surprises. Except maybe the Generic aliases.
http://stackoverflow.com/questions/tagged/hidden-features?so...