Fixed line size in C #

Short question: Is it possible to create a fixed-size string in C #? I know that in VB you can declare something like this: str AS string * 20

Long story: I need to read a binary file containing a 20-byte field in a string. I read the contents of the file into an object (class). I want to limit the lines in an object in a class definition.

Thanks so much for your concern.

swarms

+6
source share
4 answers

You can declare and populate an array with a fixed char size, and then pass this to the String constructor to make a string out of it.

var newString = new string(theCharArray);

+5
source

You need to create a property of type string that checks the value.Length in the installer and truncates it or throws an exception.

+4
source

You should use 'StringBuilder' for your problem.

+1
source

Try using the VBFixedString attribute. This attribute is for information only and does not limit the actual length of the string. It is used to communicate with API calls that expect a fixed-length string in VB6 style. See http://msdn.microsoft.com/en-us/library/x14b6s77.aspx

+1
source

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


All Articles