String, StringBuilder, and Var

I’ve recently learned about the StringBuilder and why it is better over a variable of type string - especially when the string will be modified a lot.

What does C# do when a variable of type var is used for a sting? Does it treat it like it was declared as a string, as a StringBuilder, or something else?

FWIW - I’m old school and am used to strings being char[], and having to handle strings like:

char body[1000];
strcpy(body, "Dear customer\n");
strcat(body, "Your order has shipped");

And I’ve not seen anyone use calloc, malloc, realloc, or free lately :wink:

var is just a the same as using string, or int except the compiler determines the type. If the “returning” type is StringBuilder then var will be of StringBuilder so it all depends on what the right side of the assignment is doing.

1 Like
char body[1000];
strcpy(body, "Dear customer\n");
strcat(body, "Your order has shipped");

Are you missing a ‘\0’ in there? :thinking:

And if they are using free, they’re forgetting to clear the pointer a lot these days. :roll_eyes:

CWE - CWE-416: Use After Free (4.5) (mitre.org)

I don’t think so. You using that escape code for null (ASCI 0x00)?

the str___() functions handle all the null terminations.

I forgot if the str functions did the nul terminator or not. Non-terminated string errors were such a pain to debug…

They seemed to just go on and on and on … :wink:

1 Like