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,553
  • 2
  • 33
  • 58

2 Answers2

5

No, you can't.

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

Ben Jenkinson
  • 1,806
  • 1
  • 16
  • 31
  • @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,897
  • 1
  • 12
  • 28