.ToString () creates a new string from StringBuilder, so why not use the string directly?

I saw questions related to the line builder, but could not find the answer.

My question is: "Is it useful to use a line builder here if you do not use it wisely here."

This method will work 100,000 times. To save some memory, I used stringbuilder here. but the problem is the method .ToString(). In any case, I will have to create a string using the method .ToString(), so why not initialize filenameas a string, not StringBuilder.

internal bool isFileExists()
{
    StringBuilder fileName = new StringBuilder(AppDomain.CurrentDomain.BaseDirectory + "Registry\\" + postalCode + ".html");
    if (System.IO.File.Exists(fileName.ToString()))
    {
        return true;
    }
    else
    {
        return false;
    }
}

All library methods use a string as a parameter, not a string builder, why? I think I have a lot of confusion in my concepts.

+4
7

. :

AppDomain.CurrentDomain.BaseDirectory + "Registry\\" + postalCode + ".html"

.

StringBuilder:

StringBuilder fileName = new StringBuilder();
fileName.Append(AppDomain.CurrentDomain.BaseDirectory);
fileName.Append("Registry\\");
fileName.Append(postalCode );
fileName.Append(".html");

, :

string filenamestr = AppDomain.CurrentDomain.BaseDirectory + "Registry\\" + postalCode + ".html";

, :

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Registry\\" , postalCode , ".html");
+4

4 strings . - AppDomain.CurrentDomain.BaseDirectory + "Registry\\".
- + postalCode .
+ ".html".
fileName.ToString().

StringBuilder, :

StringBuilder fileName = new StringBuilder(AppDomain.CurrentDomain.BaseDirectory);
fileName.Append("Registry\\");
fileName.Append(".html");

, 4 strings. String.Concat().

+2

, (, ), , , ... :

internal bool isFileExists()
{
    return System.IO.File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Registry\\" , postalCode , ".html"));
}
+2

a StringBuilder, string.

Strings #, (, ..), . , .

StringBuilder - , ( ) , .

, , , , , StringBuilder.

+1

string.Format. StringBuilder.

:

    var fileName = string.Format("{0}Registry\\{1}.html", AppDomain.CurrentDomain.BaseDirectory, postalCode);

String.Format , StringBuilder

+1

IO ( File.Exists) , String; File.Exists, , IMHO, :

  • if

:

internal bool isFileExists() {
  return File.Exists(String.Format("{0}Registry\\{1}.html", 
    AppDomain.CurrentDomain.BaseDirectory,
    postalCode));
}
+1

. isFileExists() postalCode. ?

File.Exists() . hashtset .

0

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


All Articles