0

I am wondering if there is a representation of the maximum length of a string in C#.

The same way there is for String.Empty but for the maximum length. Thing is I am writing some integration tests and I am testing with the same test (different inputs) the same piece of code. I don't want to use a:

var myString = "I have to make a string longer than 200 characthers to see if my test works".

I would like to have a representation of such string but couldn't find any.

Tarta
  • 1,729
  • 1
  • 29
  • 63

1 Answers1

1

The maximum length of a string is very big, as linked in the comments.

But there is a constructor for string which lets you create your string with the desired length from a character, repeated n times (answered here):

string myString = new string('*', 200);
Annosz
  • 992
  • 6
  • 23
  • 1
    Did you just give 2 links to other answers as "answer" ? – Selvin Oct 21 '20 at 12:10
  • Yes. I'm sorry if it's not correct. I wanted to include these links in my answer, to show where I got the informations, and that the question could have been solved in the first place by searching for these. I also included the final answer from those links in my answer, so if the links are not available, they will not be lost. And I also think that the way I wrote it answers this question. What can you suggest to improve it, or how should I have done it? – Annosz Oct 21 '20 at 12:15
  • with your rep - nothing ... but as soon as you get 3000 you may cast close as duplicate :) – Selvin Oct 21 '20 at 12:19
  • Okay, thanks for the advice :) I will do that in the future. – Annosz Oct 21 '20 at 12:21
  • But guys I understood (and read in other answers) that there is a way to construct a string of the size I want.. but this is not the question I made. This solution eventually helps me IF there is no solution to my question: is there a representation "somewhere" of a string of max length? Like there is for example for an int: Int32.MaxValue ? This is my question... not how to generate a string of the size I want or what's the max length of a string – Tarta Oct 21 '20 at 12:47
  • @Annosz At your rep, you can flag as a duplicate. You should see a flag link under the question; clicking that should allow you flag as a duplicate. I'd leave more detailed instructions, but I can't flag for duplicates any more. Anyway, something else for the future :). – Heretic Monkey Oct 21 '20 at 13:40
  • @Tarta If you feel the current duplicates do not answer your question, please edit your question with details on how the answers to those duplicates don't answer your question and your question will be added to a queue for reopening. I am afraid, however, that there is no representation of a string of the maximum length of a string variable, as that would be extremely large to be included in a framework distributed to millions, especially when creating a string of that size is as simple as the line of code shown in this answer, and the answers to the duplicates. – Heretic Monkey Oct 21 '20 at 13:44
  • @HereticMonkey this is already an answer that is inherent to my question :) Thanks a lot! – Tarta Oct 22 '20 at 09:12