Saturday, January 15, 2011

a good blog about generic and constrain it with "where"


James M. Hare's blog about constraining generics with "where" clause


The blog is thorough. Store it for future reference.

To summarize some key syntaxs as below:

- constrains to an interface or a base class

public class MyClass<T> where T : IMyInterface<T>

- constrains to reference type

where T : class

- Constrain to value type

where T : struct

- Constrain to required consturctor

Where T: class, new()

"Basically, the where clause allows you to specify additional constraints on what the actual type used to fill the generic type placeholder must support." In another word, "if the type meets the given constraint, you can perform the activities that pertain to that constraint with the generic placeholders."

This empowers you to write less code across types. Less is more.

Saturday, January 8, 2011

.Net 4.0 Framework and Lazy<T> - when lazier gets better

Found a couple good blogs abouot Lazy<T> in the new .Net 4.0 framework.

http://sankarsan.wordpress.com/2009/10/04/laziness-in-c-4-0-lazyt/

http://weblogs.asp.net/gunnarpeipman/archive/2009/05/19/net-framework-4-0-using-system-lazy-lt-t-gt.aspx

The key points are:

- When you create a new object, you no longer do "MyClass = new MyClass();". Instead, you do "Lazy<MyClass> = new Lazy<MyClass>();".

- For classes with no parameterless constructor, you need to do below
new Lazy<Pet>(delegate() { return new Pet("DuDu", "Siberian Husky"); });

- The Lazy<T> will NOT instantiate the instance until its value is accessed.

- Its default mode is NOT thread safe.
However, you can make it threadsafe by doing below constructors:

    1) public Lazy(LazyExecutionMode mode)
    2) public Lazy(Func<T> valueFactory,LazyExecutionMode mode)

What is the big gain here? Well, you no longer need to write your own code to implement the laziness as you did in .Net 3.5, or earlier. Instead, the constructor helps you to implement the lazy loading.

Is it cool to be lazier but get the same thing done.

Tuesday, November 16, 2010

a downloaded sample Silverlight app gave me "ValidateXaml" task failed error

If you download a sample silverlight app and try to build it, and get below error

"Error The "ValidateXaml" task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly 'file:///C:\MyTestingProjects\Silverlight4Upload\SilverlightFileUploadSrc\FileUpload\Bin\Debug\XX.XXXX.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
...
"

No need to try to de-bug it. This is simply because Windows protects you from downloaded files by default

Fixing this is pretty quick only if you know how/where to look. Below are the steps:

1.Right-click the downloaded zip file and choose “Properties”
2.At the bottom of the “General” tab, there is a “Security” section, saying “This file came from another computer and might be blocked to help protect this computer.” Click the “Unblock” button.
4.Unzip and open the solution.
5.Build, and the error message will go away!

Something good to note down for myself.

Friday, November 5, 2010

writing extentions

1) add an extention to a string to check if this string is a valid email address
public static class StringExtensions
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}

it takes one static class and a method to pass in the original object as "this". Done.

To use the above extention
Using StringExtensions;
string email="kelly.qu@123.com"
if (email.IsValidEmailAddress()) //this method would also show in the intellisense
...

2) more advanced extention - check and see if this object is in a collection?
using System;
using System.Collections;
public static class ListExtensions
{
public static bool in(this Object o, IEnumerable c)
{
return c.Any(x=>x.equals(o));
}
}

To test above extention:
using System;
Using ListExtensions;
public class test
{
public void testExtension()
{
string[] vals = {"baltimore", "toronto", "shanghai"};
if ("Portland".in(vals)) // check if Portland is in the list?
}
}