Archive for 'Code Notes' Category

C# Find All Derived Types From Assembly

By admin - Last updated: Sunday, November 15, 2009

I was always wondering if there is any method to find all derived types for the given type within an assembly. It would be handy to just pass either a class or an interface type and it will return me all its subs. I’ve come out with a method does that. It finds all the […]

C# different ways of declaring a delegate

By admin - Last updated: Friday, October 30, 2009

Lambda is quick and sweet. class DelegateTests     {         public void Run()         {             // there are many ways to call method Test             // 1, declare Func<int, string> delegate             […]

AssemblyResolve FileNotFoundException, Load Assembly from resource

By admin - Last updated: Friday, September 18, 2009

I was struggling with this issue while I was trying to load the referenced assembly stream from the resource. It was caused by JIT (Just In Time compiler) tries to resolve the assembly before the CLR actually executes the AssemblyResolve assignment. You may have the code looks like this static void Main(string[] args) {   […]

C# Wait for all threads completed with timeout

By admin - Last updated: Friday, July 31, 2009

We only want to do something after all threads finish their work. The advantage is that all the threads can work simultaneously while each of them has no dependency to the other. Typically, I found it improves efficiency when making call to the remove webservice. For example, there is a web service operation called getProductById(int […]

XML deserialization, XML undefined property becomes an empty list

By admin - Last updated: Wednesday, July 29, 2009

When deserialize XML into object, the object’s property becomes an empty list even if the property is absence from the XML, the expected behavior is the property is null. For example, given a class public class MyFoo     {         public string Id { get; set; }         […]

C# StreamReader, StreamWriter default internal FileStream

By admin - Last updated: Monday, April 6, 2009

I was wondering about the default FileStream object configuration within StreamWriter constructor, specifically the FileShare parameter of the FileStream. I’d like to find out if other processes are able to read the content at the same time while StreamWriter writes the content into the file. e.g using (StreamWriter writer= new StreamWriter(“test.txt”)) {      while(true); […]

C# Polymorphism and Inheritance override the abstract or virtual

By admin - Last updated: Sunday, February 1, 2009

Occasionally, programmers get to explain Polymorphism and Inheritance in .NET, I’ve compiled some typical use cases in case they need to be explained without IDE. We start with IDrawable interface which indicates all its implementations have a Draw method. Geometry abstract class which contains PrintType() that will be called by the specific implementation. Shape class […]

C# To Upper Case First Letter of a String

By admin - Last updated: Thursday, October 2, 2008

Utilize a simple extension method to capitalize the first letter of a string. public class Program     {         static void Main(string[] args)         {             string s = “toUpperFirstLetter”;             // ToUpperFirstLetter is the extension method defined […]

C# Pointer in unsafe context

By admin - Last updated: Saturday, September 20, 2008

Pointer usage in C# in unsafe context static unsafe void Main() {     int i;     int[] array = { 11, 22, 33 };     // pointer pi has the address of variable i     int* pi = &i;     i = 0;     // dereference the pi, i.e. […]

Get Current Directory while running as WindowsService

By admin - Last updated: Monday, September 8, 2008

During development, while you run the application as a console application or windows form application, often the code needs to load a specific file within the same folder of the exe assembly resides. The file can be located by using the file name directory since the default location will be the exe’s directory. For instance, […]

C# Extension method, GetValueOrNull for value type object

By admin - Last updated: Wednesday, March 12, 2008

Return null if the value type object has default value. public static T? GetValueOrNull<t>( this T value ) where T : struct {    if( value.Equals( default( T ) ) )       return new Nullable<t>();    else       return value; } // Usage int i = default(int); //0 int? i = […]

ASP.NET:Get values of dynamically added controls.

By admin - Last updated: Wednesday, March 5, 2008

At the stage of OnInit, all control objects are performing initialization, if added controls were not initialized at this stage, and even though the postback data contain the values of those controls, you won’t be able to obtain the user inputed values of those controls at later stage. e.g. PageLoad, OnClickHandler method. In order to […]