c# - Extension methods require declaring class to be static -


why extension methods require declaring class static? compiler requirement?

it's dictated in language specification, section 10.6.9 of c# 4 spec:

when first parameter of method includes modifier, method said extension method. extension methods can declared in non-generic, non-nested static classes. first parameter of extension method can have no modifiers other this, , parameter type cannot pointer type.

it's not clear me why all of these restrictions necessary - other potentially compiler (and language spec) simplicity. can see why makes sense restrict non-generic types, can't see why have non-nested , static. suspect makes lookup rules considerably simpler if don't have worry types contained within current type etc, dare possible.

i suspect complexity of not having these restrictions deemed less benefit gained.

edit: clear, i'm not suggesting make sense have instance methods extension methods - i'm suggesting make sense have static extension method in nested and/or non-static class.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -