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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
SDL Developer
  • 612
  • 1
  • 5
  • 15
  • Given that the OP contrasts extension methods with "Microsoft's methods", I wonder if he means "custom methods" vs. .NET methods? – John Saunders Dec 01 '11 at 02:54

3 Answers3

2

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 – SDL Developer Feb 22 '10 at 11:19
2

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
  • 4,158
  • 2
  • 23
  • 30
  • 1
    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 Feb 22 '10 at 08:39
0

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

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539