psychoric still trying to figure out what this blog is about

if you are on 64bit and your VS IDE hang when you try to "View in Browser", change the default browser. Works for me!
28Mar/080

C# Generic Constraints

Generic is a new wonder in .NET with the flexiblity it introduces to coders. But when you wish to explore further, you might face the limitation of only able to work with Object capabilities. This is because generic doesn't know your type details, and since all types are Object, so you only have Object to play with. To overcome this, we have Where keyword in C# (VB.net has As, i think). Apart from be able to constraint the types that generic accept, it also allows you to use the constraints capabilities.

Generics support few types of constraints:

  • Interface: Allow only types that implement specific interfaces to use your generic.
  • Class/Base class: Allow only types that match or inherit from a specific base class to use your generic.
  • Constructor: Require types that use your generic to implement a parameterless constructor.

Say you have a custom Interface as below

public interface IMyCustomOne
{
	string WhatsMyName();
}

and a generic method

public bool CheckNameEqualJohn<T>()
{
	return (T.WhatsMyName == "John");
}

Compiler will throw you at the instant you try to build.

To solve this, we add Where at the end

public bool CheckNameEqualJohn<T>() where T : IMyCustomOne
{
	return (T.WhatsMyName == "John");
}

Woots, compiler is now happy! The obstacle has been removed and off you go to maximize your creativity. If you need 2 constraints, how to go about it?

public class MyClass<T,U>
	where T : IMyCustomT
	where U : IMyCustomU
{
	//....
}

Some additional jargons about this constraint that will make you look cooler in front of your peers

Unbound Type Parameter
Basically this refers to the most basic way of us using generic

public class MyClass<T>
{
	//....
}

Naked Type Constraint
When a generic type parameter is used as constraint

public class MyClass<T>
{
	public void MethodOne<U>(U param) where U : T
	{
		//....
	}
}

Happy Generic'ing!

Filed under: Coder's Code No Comments
11Mar/080

Better comparison with enumeration

Suppose you have an enum list and a variable which value comes from external source. What do you usually do to compare against the enum list?

If (MyValue == "3") { //3 means ok, do some stuff }

What if one day you decide that you don't like 3 (for whatever reason) and change it to 30? You will have to find and replace the repeated value all over your project. Yes, you have the FindAndReplace function with most IDE, but holy cow, you only realize that the last couple of 3 meant something else!

So a safer way is to cast the value into appropriate enum for comparison or any other operation.

'VB.NET
dim myEnumValue as TheEnum = CType([Enum].Parse(GetType(TheEnum), _
				tempValue.ToString()), TheEnum)
If myEnumValue = TheEnum.NumberThree Then
	'..
End If

//C#
TheEnum myEnumValue = (TheEnum)Enum.Parse(typeof(TheEnum), tempValue);
if (myEnumValue == TheEnum.NumberThree) { //.. }

"Hey, that looks useful. I want it in my SuperCoolReusable library" you might say. With the power of Generic, we can put it into a common call.

'VB.NET
Public Shared Function ParseEnum(Of T)(ByVal Value As Object) As T
	Return CType([Enum].Parse(GetType(T), Value.ToString()), T)
End Function

//C#
public static ReturnType ConvertStringToEnum<ReturnType>(string Value)
{
	return (ReturnType)Enum.Parse(typeof(ReturnType), Value);
}

Another simple yet useful tip! ;)

Filed under: Coder's Code No Comments
3Mar/080

Have you made your environment pledge?

Facts, in case you wonder :

1. It takes less than half a second to turn off the water tap while brushing your teeth, and that in turn saves 14,600 litres of water a year!

2. It requires less than 0.1% of your brain power to remember to turn lights out when not required, and that action saves 68kilogram of CO2 a year!

3. It requires less than couple of mouse clicks to print 2 pages a sheet, and that setting saves half a tree every year!

Those are my pledges to prevent the earth from deteriorating. You don't have to be involved in lots of activities to save the earth when you can even start small at your own pace. Make your pledge today here! Together, we can :)

Filed under: Psycho's Note No Comments