How slowly are large strings passed as return values ​​in C #?

I want to know how return values ​​for strings work for strings in C #. In one of my functions, I generate html code, and the string is really huge, I return it from the function and paste it into the page. But I want to know if I should pass a huge string as a return value or just insert it into a page from the same function?

When C # returns a string, does it create a new string from the old one and return it?

Thanks.

+4
source share
3 answers

Strings (or any other reference type) are not copied when returning from a function, only value types.

+20
source

System.String is a reference type (class) and therefore is passed as a parameter and is returned only when the link is copied (32 or 64 bits).

The size of the string does not matter.

+8
source

Returning a string is a cheap operation - as mentioned, it is just a matter of returning 32 or 64 bits (4 or 8 bytes).

However, as Stephen Petrov points out, operations with line + are associated with creating a new line and can be a bit expensive. If you want to save performance and memory, I would suggest doing something like this:

 static int i = 0; static void Main(string[] args) { while (Console.ReadLine() == "") { var pageSB = new StringBuilder(); foreach (var section in new[] { AddHeader(), AddContent(), AddFooter() }) for (int i = 0; i < section.Length; i++) pageSB.Append(section[i]); Console.Write(pageSB.ToString()); } } static StringBuilder AddHeader() { return new StringBuilder().Append("Hi ").AppendLine("World"); } static StringBuilder AddContent() { return new StringBuilder() .AppendFormat("This page has been viewed: {0} times\n", ++i); } static StringBuilder AddFooter() { return new StringBuilder().Append("Bye ").AppendLine("World"); } 

Here we use StringBuilders to store a link to all the strings that we want to concatenate, and wait until the end before merging them. This will save a lot of unnecessary add-ons (which are compared with the memory and processor).

Of course, I doubt that you will actually see any need for this in practice - and if you do, I will spend some time exploring the union, etc., to help reduce the garbage created by all string collectors, and perhaps consider creating a custom "string holder" that is best suited for your purposes.

+1
source

Source: https://habr.com/ru/post/1491450/


All Articles