Can you have an extension method on a type instead of on an instance of a type?

Posted by SLC on Stack Overflow See other posts from Stack Overflow or by SLC
Published on 2010-04-23T08:33:57Z Indexed on 2010/04/23 8:43 UTC
Read the original article Hit count: 201

Filed under:
|

I can have an extension method like this:

DateTime d = new DateTime();
d = d.GetRandomDate();

GetRandomDate is my extension method. However the above doesn't make much sense. What would be better is:

DateTime d = DateTime.GetRandomDate();

However, I don't know how to do this. An extension method created as:

public static DateTime GetRandomDate(this System.DateTime dt)

will only add the GetRandomDate() in the first example above, not the second one. Is there a way to achieve the desired behaviour?

© Stack Overflow or respective owner

Related posts about extension-methods

Related posts about c#