Geeks With Blogs
Prasanna's Blog BizTalk, WCF, C# and Software Factories March 2008 Entries
Strategy Pattern
The strategy pattern is typically used when your programmer's algorithm should be interchangeable with different variations of the algorithm. For example, if you have code that sorts array, under certain circumstances, you might want to create QuickSort and under other circumstances, you might want to create Merge Sort. The strategy pattern is usually implemented by declaring an abstract base class with an algorithm method, which is then implemented by inheriting concrete classes. At some point in ......

Posted On Wednesday, March 19, 2008 8:27 PM

Contravariance and generics
since i have already posted basic ContraVariance in delegate, i am not going to go thru the same again, those of you who had missed my earlier post can refer here here is an example of using generics with contravariance namespace ContravarianceWithGenerics { public interface IEquity { string ToString(); string Save(); } //base class abstract class Equity : IEquity { int _price; public Equity(int Price) { _price = Price; } public override string ToString() { return GetType().Name + " : " + _price; ......

Posted On Wednesday, March 19, 2008 6:45 PM

Generics & Inheritance
A generic class that uses parameterized types, like MyBase<T>, is called an open-constructed generic. A generic class that uses no parameterized types, like MyBase<int>, is called a closed-constructed generic. You may derive from a closed-constructed generic; that is, you may inherit a class named MyDerived from another class named MyBase, as in: public class MyDerived<T> : MyBase1<int> You may derive from an open-constructed generic, provided the type is parameterized. For ......

Posted On Thursday, March 13, 2008 12:49 PM

Constraints in generic and Their Benefits
A generic class allows you to write your class without committing to any type, yet allows the user of your class, later on, to indicate the specific type to be used. While this gives greater flexibility by placing some constraints on the types that may be used for the parameterized type, you gain some control in writing your class. Let's look at an example: Example 1. The need for constraints: code that will not compile public static T Max<T>(T op1, T op2) { if (op1.CompareTo(op2) < 0) return ......

Posted On Thursday, March 13, 2008 12:25 PM

Copyright © Prasanna Krishnan | Powered by: GeeksWithBlogs.net