Geeks With Blogs
Prasanna's Blog BizTalk, WCF, C# and Software Factories general
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

Delegate inference
Delegate inference allows you to make a direct assignment of a method name to a delegate variable, without wrapping it first with a delegate object. The oldwayclass shows implementation in C# 1.1 class OldWayClass { delegate void notifyDelegate(); public void InvokeMethod() { notifyDelegate del = new notifyDelegate(SomeMethod); del(); } void SomeMethod() {...} } and the NewWayClass is the sample implementation in C# 2.0 class NewWayClass { delegate void notifyDelegate(); public void InvokeMethod() ......

Posted On Tuesday, February 26, 2008 8:01 AM

Covariance and contravariance in delegate
Covariance and contravariance provide a degree of flexibility when you match method signatures with delegate types. Covariance permits a method to have a more derived return type than what is defined in the delegate. Contravariance permits a method with parameter types that are less derived than in the delegate type. contravariance example: This example demonstrates how delegates can be used with methods that have parameters of a type that are base types of the delegate signature parameter type. ......

Posted On Monday, February 25, 2008 7:45 PM

Generic Proxy for WCF services
When we generate (WCF) service proxy class for a service by using svcutil.exe, it creates a proxy that derives from System.ServiceModel.ClientB... The proxy implements IDisposable and the client code can be wraped inside using statement and guaranteed clean-up in the face of exceptions. However, the System.ServiceModel.ClientB... class can throw exceptions from its Dispose method, because it internally invokes Close() method which might lead t0 a faulted state. In ......

Posted On Saturday, February 23, 2008 12:14 PM

Copyright © Prasanna Krishnan | Powered by: