There are 8 String class constructors, we can do the following:
1.
String
(char* Value)
Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. (***)
~EXAMPLE:
char
[] chars
=
...
{'f','a','n','t','a','s','t','i','c' }
;
unsafe
... {
fixed (char* vv = chars)
...{
string name1 = new string(vv);
}
}
unsafe
... {
fixed (char* vv = chars)
...{
string name1 = new string(vv);
}
}
2.
String
(char[] Value)
Initializes a new instance of the String class to the value indicated by an array of Unicode characters.
~EXAMPLE:
char [] chars = ... {'f','a','n','t','a','s','t','i','c' } ;
String name2 = new String(chars);
3.
String
(sbyte* Value)
Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. (***)
~EXAMPLE:
unsafe
... {
String name3 = null;
sbyte[] sbArr = new sbyte[] ...{ 0x41, 0x42, 0x43, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiUpper = sbArr)
...{
name3 = new String(pAsciiUpper);
}
}
... {
String name3 = null;
sbyte[] sbArr = new sbyte[] ...{ 0x41, 0x42, 0x43, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiUpper = sbArr)
...{
name3 = new String(pAsciiUpper);
}
}
.. name3 = “
ABC”
4.
String
(char c,int count)
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
~EXAMPLE:
string
name4
=
new
String(
'
Y
'
,
20
);
.. name4 = “YYYYYYYYYYYYYYYYYYYY”
5.
String
(char* value, int StartIndex, int length)
Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters, a starting character position within that array, and a length. (***)
~EXAMPLE:
char
[] chars
=
...
{'f','a','n','t','a','s','t','i','c' }
;
unsafe
... {
fixed (char* vv = chars)
...{
string name5 = new string(vv, 2, 5);
}
}
unsafe
... {
fixed (char* vv = chars)
...{
string name5 = new string(vv, 2, 5);
}
}
.. name5 = “
ntast”
6.
String
(char[] Value, int StartIndex, int length)
Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length.
~EXAMPLE:
char
[] vv
=
...
{'f','a','n','t','a','s','t','i','c' }
;
string name6 = new String(vv, 5 , 3 );
string name6 = new String(vv, 5 , 3 );
7.
String
(sbyte* Value, int StartIndex, int length)
Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, and a length.
~EXAMPLE:
unsafe
... {
String name7 = null;
sbyte[] sbArr = ...{ 0x61, 0x62, 0x63, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiLower = sbArr)
...{
name7 = new String(pAsciiLower, 0, sbArr2.Length-2);
}
}
... {
String name7 = null;
sbyte[] sbArr = ...{ 0x61, 0x62, 0x63, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiLower = sbArr)
...{
name7 = new String(pAsciiLower, 0, sbArr2.Length-2);
}
}
.. name7 = “ab”
String
(sbyte* value, int startIndex, int length, System.Text.Encoding enc)
Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, a length, and an Encoding object.
unsafe
... {
string name8 = null;
sbyte[] asciiChars = new sbyte[] ...{ 0x52, 0x53, 0x54, 0x54, 0x56 };
UTF8Encoding encoding = new UTF8Encoding(true, true);
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiChars = asciiChars)
...{
name8 = new String(pAsciiChars, 0, asciiChars.Length,encoding);
}
}
... {
string name8 = null;
sbyte[] asciiChars = new sbyte[] ...{ 0x52, 0x53, 0x54, 0x54, 0x56 };
UTF8Encoding encoding = new UTF8Encoding(true, true);
// Instruct the Garbage Collector not to move the memory
fixed (sbyte* pAsciiChars = asciiChars)
...{
name8 = new String(pAsciiChars, 0, asciiChars.Length,encoding);
}
}
(***):
In C#, this constructor is defined only in the context of unsafe code.
*****
To set this compiler option in the VS development environment
1.
Open the project's Properties page.
2.
Click the Build property page.
3.
Select the Allow Unsafe Code check box.