How can I get an extension method to change the original object?

后端 未结 3 1127
-上瘾入骨i
-上瘾入骨i 2020-11-29 07:22

I want to be able to write extension methods so that I can say:

lines.ForceSpaceGroupsToBeTabs();

instead of:



        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 07:44

    If it's a reference type, you'd have to change it's contents. If it's a value type you're passing in, you're out of luck. The very existence of extension methods is put into place to support functional paradigms in C#, and those functional paradigms, by their very essence, tend towards immutability of types, hence the inability to change the value off of which the extension method is called.

    In other words, while you could do it, it may not be in keeping with the "spirit" of functional programming.

提交回复
热议问题