Creating XLS Files in C#

If you need to create XLS files in .NET, I suggest you to use the following open-source libraries:

Considerations developing SharePoint 2010 client applications

Here are a few considerations developing client applications accessing the SharePoint 2010 object model:

  • the target processor must be x64 (the default is x86 instead);
  • you need to use .NET framework 3.5 and not 4.0.

As noted in the post SP2010: SharePoint not supported with .NET framework version, these bugs were present since the beta of SharePoint, but they haven't been fixed yet.

Dynamic Language Extensions in C# 4

C# 4 introduces the dynamics.

In contrast to the var keyword, witch the type is determined at compile-time, the dynamic keyword is type safe too, but its type is determined at runtime.

There are then:

  • the DynamicObject class: you have to derive from it, and implement the TryGetMember, TrySetMember and TryInvokeMember methods;
  • the ExpandObject: this class is sealed and you have to use it as it is.

C# 4 collection interfaces

In C# we have the following interfaces for collections and lists:

  • IEnumerable<T>: supports the foreach statement via the GetEnumerator() method;
  • ICollection<T>: supports the Count property and the Add(), Remove(), Clear() and CopyTo() methods;
  • IList<T>: derives from ICollection<T> and adds the Insert() and RemoveAt() methods; also it offers the following methods:
    • public void ForEach(Action<T> action);
    • public int FindIndex(Predicate<T> match);
    • public T Find(Predicate<T> match);
    • public List<T> FindAll (Predicate<T> match);
    • public List<T> Sort(Comparison<T> comparer);
    • public List<T> Sort(IComparer<T> comparer);
    • public List<TOutput> ConvertAll(Converter<T, TOutput> converter);
  • ISet<T>: derives from ICollection<T> and adds union and intersection of sets;
  • IDictionary<TKey, TValue>: collection of values indexed by a key;
  • ILookup<TKey, TValue>: like IDictionary, but allows multiple values for the same key;
  • IComparer<T>: used to sort elements inside a collection via the Compare() method;
  • IEqualityComparer<T>: compare values;
  • IProducerConsumerCollection<T>: support thread-safe classes.

C#: delegates, anonymous methods, lambda expressions and events

Delegates = typed function pointere signatures:

delegate void MyMethodDelegate(int myParam)
MyMethodDelegate myMethod = new MyMethodDelegate(x.ToString()
MyMethodDelegate myMethod = x.ToString()

There are two generic shortcuts to delegate declaration:

  • Action: a method that takes up to 16 parameters and returns void
  • Func: a method that takes up to 16 parameters and return the last type

Anonymous methods allow instancing delegates faster:

Func anonDel = delegate(string param) {... return y;}

Lambda expressions simplify this even more:

Func lambdaExpr = param => {... return y;}
Func lambdaExpr = (double x, double y) => ...
Func lambdaExpr = x, y => xxx

Events allow subscribe/unsubscribe to/from delegates:

public event EventHandler myEvent;

C# co-variance and contro-variance

C# 4 introduces the concept of co-variance and contro-variance, so now you can have:

  • Invariance: it is what you have with the previous version of C#: GenericType<B> doesn't inherit from GenericType<A> even if B inherits from A;</a>
  • Co-variance: you mark your generic type with <out T> and you can have T only as method output parameter or return values;
  • Contro-variance: you mark your generic type with <in T> and you can have T only as method input parameter.

C# generics constraints

Here are the contraints you can enforce to generic types:

  • struct: for value types;
  • class: for reference types;
  • IFoo: for interfaces;
  • Foo: for base classes;
  • new(): for default constructors;
  • T2: for another generic type.

C#: Extension Methods

Here is a sample extension method:

public static void MyMethod (this MyExtendedClass myInstance, other parameters...)

I usually write extension methods in static classes placed in utility assemblies.

C#: Anonymous Types

C# 3 introduced the anonymous types.

Here are a few example on creating instances of them:

var captain1 = new { FirstName = "MyName", MiddleName = "MyMiddleName", Surname = "MySurname" }
var captain2 = new { sourceCaptain.FirstName, sourceCaptain.MiddleName, sourceCaptain.Surname }

A final note: captain1 and captain2 are of the same type because they are anonymous types with the same fields, so you can do:

captain2 = captain1

Reading Nested Classes from Reflection

Sometimes you need to create instances of nested classes via reflection, so you need to read the type of the nested class from a configuration file, for example web.config.
So, what it its format?

The answer is: "MyNamespace.MyParentClass+MyChildClass, MyAssemblyName, Version=version, Culture=culture, PublicKeyToken=publicKeyToken"

Just a few notes:

  • In bold is the + separating the Namespace.ParentClass from the ChildClass
  • The assembly name doesn't contain the .dll file extension
  • An example of version is and an example of culture is neutral