# String, StringBuilder, and Var

**URL:** <https://www.epiusers.help/t/string-stringbuilder-and-var/83270>\
**Category:** Off Topic\
**Tags:** c-sharp\
**Created:** [October 6, 2021, 12:42pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270 "2021-10-06T12:42:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 6, 2021, 12:42pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/1 "2021-10-06T12:42:23Z")

</div>

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:

```cs
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 😉

---

<div class="post-metadata">

**Author:** ![josecgomez](https://www.epiusers.help/user_avatar/www.epiusers.help/josecgomez/32/55731_2.png) [@josecgomez](https://www.epiusers.help/u/josecgomez)\
**Post date:** [October 6, 2021, 1:07pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/2 "2021-10-06T13:07:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [October 6, 2021, 2:24pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/3 "2021-10-06T14:24:51Z")

</div>

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

```

Are you missing a ‘\0’ in there? 🤔

> [@ckrusen](#):
>
> I’ve not seen anyone use calloc, malloc, realloc, or free lately

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

[CWE - CWE-416: Use After Free (4.5) (mitre.org)](https://cwe.mitre.org/data/definitions/416.html)

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 6, 2021, 4:16pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/4 "2021-10-06T16:16:37Z")

</div>

> [@Mark\_Wonsil](#):
>
> Are you missing a ‘\0’ in there?

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

the `str___()` functions handle all the null terminations.

---

<div class="post-metadata">

**Author:** ![Mark\_Wonsil](https://www.epiusers.help/user_avatar/www.epiusers.help/mark_wonsil/32/51594_2.png) [@Mark\_Wonsil](https://www.epiusers.help/u/Mark_Wonsil)\
**Post date:** [October 6, 2021, 4:56pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/5 "2021-10-06T16:56:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [October 6, 2021, 4:58pm UTC](https://www.epiusers.help/t/string-stringbuilder-and-var/83270/6 "2021-10-06T16:58:12Z")

</div>

> [@Mark\_Wonsil](#):
>
> Non-terminated string errors were such a pain to debug…

They seemed to just go on and on and on … 😉
