Equipping yourself before digging into Unit Testing!

Generic Type, Delegate as an Anonymous Method, and Lambda Expression are what you need to master before you can work with Moq Framework (one of the Unit Test frameworks).

I made a summary here about those terms. Please view the web site referred in the source section to read full details.

 

[Anonymous Delegate]
Basically, anytime you see a method that accepts a delegate as a parameter you could use anonymous methods.

Example:

Sort method in class List.

definition

class List
{
	...
	public void Sort (Comparison<T> comparison);
	...
}
public delegate int Comparison<in T> (T x,	T y);

usage

List myList;

myList.Sort(delegate(MyItem item1, MyItem item2)

{

return Comparer<MyItem>.Default.Compare(item1, item2);

});

ForEach(Action<T> action) method in class List.

usage

myList.ForEach(delegate(MyItem item) {//do-something} )

see more .Find, .ConvertAll

source: http://weblogs.asp.net/dwahlin/archive/2007/04/23/The-Power-of-Anonymous-Methods-in-C_2300_.aspx

 

[Generic type]
Make the code much more reusable: They are a type used for methods whose code  remains the same whilst the data type of the parameters can change with each use.

To truly create a generic type parameter, you need to ensure that a constraint is met – i.e. a class or interface that must be included as a part of the type used for the parameter.

Example:

Class Compare<AnyType, AnyType>
{
	AnyType GetLarger(AnyType obj1, AnyType obj2)
					where AnyType : IComparable
	{
		//Logic...
		if obj1.Compare(obj2) > 0 return obj1 else return obj2;
	}

}
 source: http://www.developer.com/net/net/article.php/2229511/What-Are-C-Generics.htm

 

[Anonymous delegate replaced by lambda expression]

<pre>Func<int, int> f1 = delegate(int i) { return i + 1; };
Func<int, int> f2 = i => i + 1;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: