You want to ensure that the data entered by a user and assigned to a string does not exceed a certain number of characters.
the overloaded constructor of the
class, which accepts a maximum capacity. The following code creates a
StringBuilder object that has a maximum size of 10
System.Text.StringBuilder sbMax = new System.Text.StringBuilder(10, 10); sbMax.Append("123456789"); sbMax.Append("0");
This code creates a
sbMax, which has a maximum length of
10 characters. Nine characters are appended to
this string and then a tenth character is appended without a problem.
However, if the next line of code is executed:
The length of
sbMax goes beyond
10 characters and an
string object is immutable and, as such, does
not have a built-in method to prevent its length from going beyond a
certain point. Fortunately, the
object contains an overloaded constructor that allows the maximum
size of its string to be set. The
constructor that we are concerned with is defined as follows:
For most applications, the
maxCapacity can be identical. This way gives you the best performance, overall. If these two parameters are not identical, it is critical that these two parameters can coexist. Take, for ...