Is there a way to make "destructive" string methods a-la Ruby?
In Ruby, methods that change an object have an end at the end: string.downcase!
In C #, you need to do: foo = foo.ToLower()
Is there a way to make an extension method like:
foo.ConvertToLower()
who will manipulate foo
?
(I think the answer is no, since strings are immutable and you cannot do ref this
in an extension method.)
source to share
No, you cannot do this in an extension method. To reassign the value of a variable passed as a parameter, you need to pass it by reference using a parameter modifier ref
, which is not allowed for extension methods. Even if it were possible, there cannot be a variable to reassign, for example in "foo".ConvertToLower()
.
source to share