Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was reading about monkeypatching _ dynamically overriding the functionality of classes.

C# allows dynamic extending, but no monkeypatching, which I find reasonable. But I wondered why they wouldn't include the feature. Is it because you could hurt other programs running on your machine, or just the feature wasn't considered necessary?

share|improve this question

closed as not constructive by Bryan Oakley, jk., Oded, MainMa, gbjbaanb Aug 28 '12 at 12:11

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

Unless the answer comes straight from Microsoft, the only answers you will get are conjecture and opinions, and this site isn't for questions that invite conjecture. – Bryan Oakley Aug 28 '12 at 11:17
@Bryan Oakley, I was hoping that someone might link me to some article or post (by Microsoft). I suppose excluding such big features should somehow be explained. – superM Aug 28 '12 at 11:19
What do you mean by "dynamic extending"? Extension methods? And monkey patching isn't a good fit for the type system. You can do when you use types that are designed for dynamic typing together with the dynamic keyword. – CodesInChaos Aug 28 '12 at 11:25
Languages supporting monkeypatching are mostly weakly typed (if not all?), which is opposite of C#'s strongly typed philosophy. DLR was introduced in .NET 4, but it's intentionally limited to the dynamic type to keep the existing type system strong. It's almost like asking why C# is not weakly typed. – Groo Aug 28 '12 at 11:27
See the first part of this answer – filpen Aug 28 '12 at 11:45
up vote 0 down vote accepted

I dont like monkeypatching because in some cases its the reason for some problems and misunderstandings. Its the same why multiclass inheritiances are not available in languages like java or C#.

But what you can do is to consider if you should reuse a class and inherit it?

So what you can do in C# is to create a new class, extended from one of the .net classes. THe problem with the String class is not possible because it is sealed.

But you would be able to replace such a method with the "new" statement. In this way you can reuse it too.

But the main reason is maybe of the complexity to implement it. The way how the compiler works are different for oop languages as for prototype languages like JavaScript.

share|improve this answer
You think that monkey-patching leads to misunderstandings but method hiding doesn't? – pdr Aug 28 '12 at 11:56
I'll think that when you hide a method from a another class and have the possibilty for documentation and something like that, you got a better overview about the implementation, than doing monkey-patching. I think the "what do you like" more is leading to a never ending discussion ;-) – Smokefoot Aug 28 '12 at 11:59
Funnily enough, one day later, I've just seen a situation where a List was being initialised in a subclass constructor but the object was being cast to a superclass before Adding to the List. That caused some confusion. :) Method hiding should be banned. – pdr Aug 29 '12 at 17:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.