1

I was wondering if there is a way to extend the string class with a method called GenerateRandom() which generates a random string. Maybe this would look like

string.CreateRandom();

As I have read there is no way than creating an instance of a string and then calling the extension method. Maybe there is one and you know it.

Thanks for your help!

DirtyNative
  • 2,261
  • 2
  • 23
  • 48

2 Answers2

4

No, you can't.

For more information, and an explanation, see these earlier questions:

Ben Jenkinson
  • 1,786
  • 1
  • 14
  • 30
  • @Daniel One of the C# language designers [gave an explanation here](https://stackoverflow.com/a/4914207/590382) back in 2011, I don't know how the rationale will have changed since. _P.S_ If this answered your question, don't forget to [upvote & mark an answer as accepted](http://stackoverflow.com/help/someone-answers). – Ben Jenkinson Apr 07 '17 at 13:17
1

You can't.

The same question was asked here: Static extension methods

You can also read there, why you cannot do that.

Community
  • 1
  • 1
Adam Jachocki
  • 1,789
  • 1
  • 10
  • 22