Food for thought: C# Extension Methods

Disclaimer, I have not tested any of the following code.

Write the following extension method:

public static int ToInt(this A a)
{
return Int32.Parse(a.ToString());
}

public class A
{
}
Now, say the class designer of A implemented it's own ToInt method.
public class A
{
public int ToInt(){ return 0; }
}

What happens now to the extension method?
My guess is that it would shadow the class method.


Now, assuming that the extension method shadows the class method, how could one access the class method in the extension method?

public static int ToInt(this A a)
{
return a.ToInt(); // infinite recursion?
}

Having different scope for your property

C# 2.0 gave us this:

private string _property;
public string Property
{
    get { return _property; }
    private set { _property = value; }
}

C# 3.0 gave us this:

public string Property
{
    get;
    private set;
}

Due to the rules of encapsulation, the accessor/getter usually has a more relaxed scope than the mutator/setter.

I've recently came across cases where the tables are turned.  Hurray for some dependency-injectioness!

[CreateNew]
IService MyService
{
   private get;
   internal set;
}

Love hate relationship with Asp:DropDownList.AppendDataBoundItems

Here's the deal.
I want to bind the dropdownlist to a List<Blob>.
But I want the dropdownlist to have an initial empty field.

Most people do this:

<asp:DropDownList ... EnableViewState="true" AppendDataBoundItems="true">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>

Which is nice and dandy, until you need to update the datasource of the dropdownlist.

AppendDataBoundItems will make sure all the current items stays in the list, and all the ones in the new datasource gets added to the list.

So you ended up writing silly code that looks like this:

string selectedValue = dropdownlist.SelectedValue;
dropdownlist.Items.Clear();
dropdownlist.Items.Add(new ListItem());
dropdownlist.DataSource= List<Blob>;
dropdownlist.DataBind();
dropdownlist.selectedValue = selectedValue;

 

I promise my next code rant will look a lot better than this rubbish!

An interesting quit smoking help

A colleague mentioned a site quitext.com which is a quit smoking support website.  They send you daily SMS' to help encourage you to quit smoking.  You can also buy it as a gift for someone you know.

I don't think I'll ever smoke myself, but I can understand wanting to quit smoking can be very difficult at times.  It's good to have different ways to help out.

MIX08

Everybody is blogging about it.  I'm not there, but still the amount of betas rolling out from Microsoft is making me very happy.

  • ASP.NET MVP beta 2
  • IE8 beta
  • Silverlight 2 beta 1
  • Expression 2.5

Colleague forwarded a news article to me where Microsoft is partnering up with Nokia to deliver Silverlight on the Symbian platform.  So that's going to make this platform very tidy.

Good times.