Category Archives: CSharp

A very good one to explain the difference between New and Override.

My feeling is, ‘New’ acts like a partial override, or not very stable override. In another word, it will call the base method under some circumstance you didn’t expect.

One typical example is, as shown in the post I linked in the beginning of this post, when you try to do the casting back to base class, the one in your ‘New’ method will completely back to the hidden one, while the override never do.

I will post more when I found more about this.

A very simple request, I was surprised how hard csharp implemented it. At first I thought it should be as easy as this:

string[] myList = {“1″, “2″, “3″};
string[] newList = myList.Add(“4″);

But it turns out Array doesn’t have an Add() method. OK, my stupid way, copying…

string[] newList = new string[myList.Length + 1];
Array.Copy(myList, newList, myList.Length);
newList[myList.Length] = “4″;

I feelvery shameful to write those code out. Re-try conversion to ArrayList, I feel much better when I figured out the ArrayList constructor and Initialize block.

ArrayList newArrayList = new ArrayList(myList) {“4″};
string[] newList = newArrayList.ToArray(typeof(string)) as string[];

And, how about using string join?

string[] myList = {“1″, “2″, “3″};
string[] newList = {string.Join(“,”, myList), “4″);

If I want repository return a group of data, which type should I choose? I saw some NHibernate DAO just return IList<T>, JP’s factory pattern returns IEnumerable, Rocky’s CSLA return costomized collection.

Which one is better? After I read this post about collection, I feel the power of the DDD, this post described a typical filter problem:

public class BookCatalog {
List<Book> findBooksByAuthor( … ) {..}
List<Book> findBooksByPublisher( … ) {..}
List<Book> findBooksByAuthorAndPublisher( … ) {..}

Can be easily solve by changing method return type from List<> to costomized collection:

public class BookCatalog {
BookCatalog findByAuthor( … ) {..}
BookCatalog findByPublisher( … ) {..}
BookCatalog findByPublishDates( DateTime,DateTime ) {..}
BookCatalog findByPublishingCountry( … ) {..}

The method chain is here:

BookCatalog.findByAuthor(“Frank”).findByPublisher(“Frank Inc.”)…

The new LinQ where syntax has the similar effect.

I still don’t know why IEnumeralbe should be used, seems it’s usefule when passing data to UI / controls, but why it’s also popular in passing data from DAL to upper layers.