tags:

views:

111

answers:

3

I want to ask, does adding extension methods to datatypes works in the same way as Microsoft's methods or do they have any limitaion.

This is relevant to experienced programmers who had find some limitations while using them.

A: 

The main difference from the user's perspective is that they are called on null references too.

Matthew Flaschen
I would have said 'can be called', and that is an advantage, not a limitation.
Benjol
+1  A: 

What sort of limitations are you thinking of? I'm not particularly fond of the way they're discovered - and in particular, the way that if the extended type later gains a method with the same signature, that will silently be called with no warnings around the extension method at all.

There are some ways in which they don't work as well as one might like - for example, you can't write an extension method for a delegate type and call it directly on a lambda expression or anonymous method - but that's reasonable enough.

Beyond that, I'm not aware of significant limitations - and I certainly haven't seen any situations where Microsoft code has had special rules applied around extension methods.

Your last sentence makes it sound like you have seen some limitations - could you share them with us?

Jon Skeet
jon you are right apart from your above stated stuffs i have found 1 limitation but that when i started debugging was part of code inconsistency. anyways hats off to you i wish you kepp going this way
Rahul Malhotra
+1  A: 

You can do anything in an extension method that you could do in a normal static method taking the object that you are extending as an argument. That is to say, you cannot suddenly break encapsulation or do other silly things with it.

wasatz
I think that this was the sense of the question: with "Microsoft's methods" he meant the methods of the class itself, while the extension methods have the "limitation" of not being able to access protected and private members of the class.
Paolo Tedesco