extends System.Object
| Variables: |
|---|
public System.Int32 Capacity |
public System.Char Chars |
public System.Int32 Length |
| Constructors: |
|---|
public System.Text.StringBuilder() Constructs and initializes a new, empty instance of the System.Text.StringBuilder class. The new instance of System.Text.StringBuilder represents a string equal to System.String.Empty. The System.Text.StringBuilder.Capacity is set to the default capacity. The default value of the System.Text.StringBuilder.Capacity property is implementation dependent. |
public System.Text.StringBuilder(System.String value) Constructs and initializes a new instance of the System.Text.StringBuilder class, with the specified System.String as its value. Parameter value: A System.String containing the string value of the new instance of System.Text.StringBuilder. The System.Text.StringBuilder.Capacity of the new instance is implementation defined. |
public System.Text.StringBuilder(System.Int32 capacity) Constructs and initializes a new, empty instance of the System.Text.StringBuilder class, with a specified System.Text.StringBuilder.Capacity . Parameter capacity: A System.Int32 containing the starting number of characters allowed in the System.Text.StringBuilder . If the specified capacity is less than the default capacity, the System.Text.StringBuilder.Capacity of the new instance of System.Text.StringBuilder is set to the default value. The System.Text.StringBuilder will dynamically allocate more space when it is required. The new System.Text.StringBuilder is initialized to represent an empty string. The default value of the System.Text.StringBuilder.Capacity property is implementation dependent. |
| Functions: |
|---|
public System.Text.StringBuilder Append(System.Char value, System.Int32 repeatCount) Appends multiple copies of a character to the end of the current System.Text.StringBuilder . Parameter value: The System.Char to be appended. Parameter repeatCount: A System.Int32 containing the number of times to append value . Returns: The current instance after the operation has occurred. Throws: : repeatCount is less than zero. This method appends repeatCount copies of the specified character to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Char[] value, System.Int32 startIndex, System.Int32 charCount) Appends the string representation of an array of Unicode characters to the end of the current instance. Parameter value: The System.Char array to be appended. Parameter startIndex: A System.Int32 containing the index in value at which the subarray starts. Parameter charCount: A System.Int32 containing the number of characters to copy from value . Returns: The current instance after the operation has occurred. Throws: : value is a null reference, and startIndex and charCount are not both zero. Throws: : charCount or startIndex is less than zero. -or- The sum of startIndex and charCount is greater than the length of value. This method appends with the specified range of characters from the value array to the current instance. If value is a null reference, and both startIndex and charCount are zero, no changes are made. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.String value) Appends a copy of a string to the end of the current instance. Parameter value: The System.String to be appended. Returns: The current instance after the operation has occurred. This method appends the value string to the current instance. If value is a null reference, no changes are made. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.String value, System.Int32 startIndex, System.Int32 count) Appends a copy of an array of Unicode characters, specified by a starting index and length, of a specified System.String to the end of the current instance. Parameter value: The System.String from which the substring will be taken. Parameter startIndex: A System.Int32 containing the index in value from which to start copying. Parameter count: A System.Int32 containing the number of characters to copy from value . Returns: The current instance after the operation has occurred. Throws: : value is a null reference, and startIndex and count are not both zero. Throws: : charCount or startIndex is less than zero. -or- The sum of startIndex and charCount is greater than the length of value . This method appends the specified range of characters in the value string to the current instance. If value is a null reference and startIndex and count are both zero, no changes are made. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Boolean value) Appends the string representation of a System.Boolean to the end of the current instance. Parameter value: A System.Boolean to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.SByte value) Appends the string representation of a System.SByte to the end of the current instance. Parameter value: The System.SByte to be appended. Returns: The current instance after the operation has occurred. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(System.Int16). This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Byte value) Appends the string representation of a System.Byte to the end of the current instance. Parameter value: The System.Byte to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Char value) Appends the string representation of a Unicode character to the end of the current instance. Parameter value: The System.Char to be appended. Returns: The current instance after the operation has occurred. This method appends the specified character to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Int16 value) Appends the string representation of a System.Int16 to the end of the current instance. Parameter value: The System.Int16 to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Int32 value) Appends the string representation of a System.Int32 to the end of the current instance. Parameter value: The System.Int32 to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Int64 value) Appends the string representation of a System.Int64 to the end of the current instance. Parameter value: The System.Int64 to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Single value) Appends the string representation of a System.Single to the end of the current instance. Parameter value: The System.Single to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Double value) Appends the string representation of a System.Double to the end of the current instance. Parameter value: The System.Double to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Decimal value) Appends the string representation of a System.Decimal to the end of the current instance. Parameter value: The System.Decimal to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.UInt16 value) Appends the string representation of a System.UInt16 to the end of the current instance. Parameter value: The System.UInt16 to be appended. Returns: The current instance after the operation has occurred. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(System.Int32). This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.UInt32 value) Appends the string representation of a System.UInt32 to the end of the current instance. Parameter value: The System.UInt32 to be appended. Returns: The current instance after the operation has occurred. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(System.Int64). This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.UInt64 value) Appends the string representation of a System.UInt64 to the end of the current instance. Parameter value: The System.UInt64 to be appended. Returns: The current instance after the operation has occurred. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(System.Decimal). This method appends value.ToString() to the current instance. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Object value) Appends the string representation of an object to the end of the current instance. Parameter value: The System.Object to be appended. Returns: The current instance after the operation has occurred. This method appends value.ToString() to the current instance. If value is a null reference, no changes are made. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder Append(System.Char[] value) Appends the string representation of all of the characters in a System.Array to the end of the current instance. Parameter value: The array of System.Char to be appended. Returns: The current instance after the operation has occurred. This method appends all of the characters in the specified array to the current instance in the same order as they appear in value. If value is a null reference no changes are made. The System.Text.StringBuilder.Capacity of the current instance is increased as necessary. |
public System.Text.StringBuilder AppendFormat(System.String format, System.Object arg0, System.Object arg1, System.Object arg2) Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the specified objects. Parameter format: A System.String containing zero or more format specifications. Parameter arg0: The first System.Object to be formatted. Can be a null reference. Parameter arg1: The second System.Object to be formatted. Can be a null reference. Parameter arg2: The third System.Object to be formatted. Can be a null reference. Returns: The current instance after the operation has occurred. Throws: : format is a null reference. Throws: : format is invalid. This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place. This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0, arg1, arg2} ). For more information on the format specification, see the System.String class overview. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
StringBuilder sb = new StringBuilder("The high ");
Console.WriteLine( sb.AppendFormat("temperature today was {0} {1} {2}.", "very", "very", "high") );
}
}
The output is The high temperature today was very very high. |
public System.Text.StringBuilder AppendFormat(System.IFormatProvider provider, System.String format, System.Object[] args) Appends the specified string to the current instance, with the format specifications in that string being replaced with the string values of the specified array of objects, formatted in accordance with the formatting object returned by the specified System.IFormatProvider. Parameter provider: A System.IFormatProvider that supplies a formatting object that provides culture specific formatting information. Can be a null reference. Parameter format: A System.String containing zero or more format specifications. Parameter args: A System.Object array to be formatted. Returns: The current instance after the operation has occurred. Throws: : format or args is a null reference. Throws: : format is invalid. This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place. The format parameter is embedded with zero or more format specifications of the form, {N [, M ][: formatString ]}, where N is a zero-based integer indicating the argument to be formatted, M is an optional integer indicating the width of the region to contain the formatted value, and formatString is an optional string of formatting codes. For more information on the format specification see the System.String class overview. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
string a = "very";
string b = "very";
string c = "high";
StringBuilder sb = new StringBuilder("The high ");
Console.WriteLine(sb.AppendFormat(null, "temperature today was {0}, {1} {2}.", a, b, c) );
}
}
The output is The high temperature today was very, very high. |
public System.Text.StringBuilder AppendFormat(System.String format, System.Object[] args) Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the elements in the specified array. Parameter format: A System.String containing zero or more format specifications. Parameter args: A System.Object array to be formatted. Returns: The current instance after operation has occurred. Throws: : format or args is a null reference. Throws: : format is invalid. This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place. This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, args ). For more information on the format specification see the System.String class overview. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
string [] strings = {"very", "very", null, "high"};
StringBuilder sb = new StringBuilder("The high ");
Console.WriteLine( sb.AppendFormat("temperature today was {0}, {1} {2}{3}.", strings) );
}
}
The output is The high temperature today was very, very high. |
public System.Text.StringBuilder AppendFormat(System.String format, System.Object arg0, System.Object arg1) Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the specified objects. Parameter format: A System.String containing zero or more format specifications. Parameter arg0: The first System.Object to be formatted. Can be a null reference. Parameter arg1: The second System.Object to be formatted. Can be a null reference. Returns: The current instance after the operation has occurred. Throws: : format is a null reference. Throws: : format is invalid. This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place. This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0, arg1} ). For more information on the format specification, see the System.String class overview. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
StringBuilder sb = new StringBuilder("The high ");
Console.WriteLine( sb.AppendFormat("temperature today was {0} {1}.", "very", "high") );
}
}
The output is The high temperature today was very high. |
public System.Text.StringBuilder AppendFormat(System.String format, System.Object arg0) Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string value of the specified object. Parameter format: A System.String containing zero or more format specifications. Parameter arg0: A System.Object to be formatted. Returns: The current instance after the operation has occurred. Throws: : format is a null reference. Throws: : format is invalid. This method appends the formatted copy of the specified string to the current instance. This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0} ). For more information on the format specification, see the System.String class overview. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
StringBuilder sb = new StringBuilder("The high ");
Console.WriteLine( sb.AppendFormat("temperature today was {0, 6}.", 88) );
}
}
The output is The high temperature today was 88.
|
public System.Int32 EnsureCapacity(System.Int32 capacity) Ensures that the capacity of the current instance is at least a specified value. Parameter capacity: A System.Int32 containing the minimum capacity to ensure. Returns: A System.Int32 equal to the new System.Text.StringBuilder.Capacity of the current instance. Throws: : capacity is less zero. If the specified value is less than the current System.Text.StringBuilder.Capacity, no changes are made and System.Text.StringBuilder.Capacity remains the same. For performance reasons, the new System.Text.StringBuilder.Capacity might be larger than the specified value. The amount of memory allocated by this method is implementation specific. |
public System.Boolean Equals(System.Text.StringBuilder sb) Determines whether the current instance and a specified System.Text.StringBuilder have the same value. Parameter sb: A System.Text.StringBuilder. Returns: true if the current instance and sb have the same value; otherwise, false. |
public System.Text.StringBuilder Insert(System.Int32 index, System.String value) Inserts the string representation of a string object into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.String to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts value into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. If value is Empty or a null reference, the System.Text.StringBuilder is not changed. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Boolean value) Inserts the string representation of a System.Boolean value into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Boolean value to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.String value, System.Int32 count) Inserts multiple copies of a string into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.String to be inserted. Parameter count: A System.Int32 containing the number of times the string is to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero -or- count is less than zero This method inserts the value string count times into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. If value is Empty or a null reference, the System.Text.StringBuilder is not changed. |
public System.Text.StringBuilder Insert(System.Int32 index, System.SByte value) Inserts the string representation of a System.SByte into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.SByte to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(System.Int32, System.Int16). This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Byte value) Inserts the string representation of a System.Byte into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Byte to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Int16 value) Inserts the string representation of a System.Int16 into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Int16 to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Char value) Inserts the string representation of a Unicode character into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Char to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Char[] value) Inserts the string representation of an array of Unicode characters into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Char array to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts all of the characters in the specified array into the current instance in the same order as they appear in value. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. If value is empty or a null reference, the System.Text.StringBuilder is not changed. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Char[] value, System.Int32 startIndex, System.Int32 charCount) Inserts the string representation of a subarray of Unicode characters into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Char array from which to get the characters to be inserted. Parameter startIndex: A System.Int32 containing the starting index within value. Parameter charCount: A System.Int32 containing the number of characters to insert from value. Returns: The current instance after insertion has occurred. Throws: : value is a null reference, and startIndex and charCount are not both zero. Throws: : index is greater than the length of the current instance or less than zero. -or- startIndex or charCount is less than zero or their sum is greater than the length of value. This method inserts the specified range of characters from value array into the current instance in the same order as they appear in value. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. If value is an empty array or a null reference and startIndex and charCount are both zero, the System.Text.StringBuilder is not changed. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Object value) Inserts the string representation of an object into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Object to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. If value is a null reference, the System.Text.StringBuilder is not changed. |
public System.Text.StringBuilder Insert(System.Int32 index, System.UInt64 value) Inserts the string representation of a System.UInt64 into the current instance at a specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.UInt64 to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(System.Int32, System.Decimal). This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.UInt32 value) Inserts the string representation of a System.UInt32 into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.UInt32 to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(System.Int32, System.Int64). This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.UInt16 value) Inserts the string representation of a System.UInt16 into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.UInt16 to insert. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(System.Int32, System.Int32). This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Decimal value) Inserts the string representation of a System.Decimal into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Decimal to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Double value) Inserts the string representation of a System.Double into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Double to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Single value) Inserts the string representation of a System.Single into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Single to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Int64 value) Inserts the string representation of a System.Int64 into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Int64 to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero. This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Insert(System.Int32 index, System.Int32 value) Inserts the string representation of a System.Int32 into the current instance at the specified index. Parameter index: A System.Int32 containing the index at which to insert. Parameter value: The System.Int32 to be inserted. Returns: The current instance after insertion has occurred. Throws: : index is greater than the length of the current instance -or- index is less than zero This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary. |
public System.Text.StringBuilder Remove(System.Int32 startIndex, System.Int32 length) Removes a specified range of characters from the current instance. Parameter startIndex: A System.Int32 containing the index at which to begin removal. Parameter length: A System.Int32 containing the number of characters to be removed. Returns: The current instance after removal has occurred. Throws: : startIndex or length is less than zero -or- The sum of startIndex and length is greater than the length of the current instance. This method removes the specified range of characters from the current instance. The characters at (startIndex + length) are moved to startIndex, and the string value of the current instance is shortened by length. The System.Text.StringBuilder.Replace(System.String,System.String) method can be used to remove all instances of a string from a System.Text.StringBuilder. Example: using System;
using System.Text;
public class StringBuilderTest {
public static void Main() {
StringBuilder sb = new StringBuilder("0123456789");
Console.WriteLine(sb);
sb.Remove(3, 4);
Console.WriteLine(sb);
}
}
The output is 0123456789 012789 |
public System.Text.StringBuilder Replace(System.Char oldChar, System.Char newChar, System.Int32 startIndex, System.Int32 count) Replaces all instances of a specified character in a specified range with another specified character. Parameter oldChar: The System.Char to replace. Parameter newChar: The System.Char with which to replace oldChar . Parameter startIndex: A System.Int32 containing the index from which to start replacing oldChar. Parameter count: A System.Int32 containing the length of the range in which to replace oldChar . Returns: The current instance after substitution has occurred. Throws: : The sum of startIndex and count is larger than the length of the current instance -or- startIndex or count is less than zero. This method substitutes each occurrence of oldChar in the specified range of the current instance with newChar. This method is case-sensitive. |
public System.Text.StringBuilder Replace(System.Char oldChar, System.Char newChar) Replaces all instances of a specified character in the current instance with another specified character. Parameter oldChar: The System.Char to replace. Parameter newChar: The System.Char with which to replace oldChar . Returns: The current instance after substitution has occurred. This method substitutes each occurrence of oldChar in the current instance with newChar. This method is case-sensitive. |
public System.Text.StringBuilder Replace(System.String oldValue, System.String newValue, System.Int32 startIndex, System.Int32 count) Replaces all instances of a specified string in a specified range with another specified string. Parameter oldValue: A System.String containing the string value to replace. Parameter newValue: A System.String containing the string value with which to replace oldValue. Can be a null reference. Parameter startIndex: A System.Int32 containing the location from which to start replacing oldValue. Parameter count: A System.Int32 containing the length of the range in which to replace oldValue. Returns: The current instance after substitution has occurred. Throws: : oldValue is a null reference. Throws: : startIndex or count is less than zero. -or- The sum of startIndex and count is greater than the length of the current instance. Throws: : The length of oldvalue is zero. This method substitutes each occurrence of oldValue in the specified range of the current instance with newValue. If newValue is null , instances of oldValue are removed. This method is case-sensitive. |
public System.Text.StringBuilder Replace(System.String oldValue, System.String newValue) Replaces all instances of a specified string with another specified string. Parameter oldValue: A System.String containing the string value to replace. Parameter newValue: A System.String containing the string value with which to replace oldValue. Can be a null reference. Returns: The current instance after substitution has occurred. Throws: : oldValue is a null reference. Throws: : The length of oldvalue is zero. This method substitutes each occurrence of oldValue in the current instance with newValue. If newValue is null , instances of oldValue are removed. This method is case-sensitive. |
public System.String ToString(System.Int32 startIndex, System.Int32 length) Returns a System.String representation of a substring of the current instance. Parameter startIndex: A System.Int32 containing the index at which the substring begins. Parameter length: A System.Int32 containing the length of the substring. Returns: A new System.String representing the characters in the specified range. Throws: : startIndex or length is less than zero. -or- The sum of startIndex and length is greater than the length of the current instance. |
public System.String ToString() Returns a System.String representation of the value of the current instance. Returns: A System.String representing the current instance. This method overrides System.Object.ToString. |
| Functions inherited from System.Object: |
|---|
public virtual System.Boolean Equals(System.Object obj) Determines whether the specified System.Object is equal to the current instance. Parameter obj: The System.Object to compare with the current instance. Returns: true if obj is equal to the current instance; otherwise, false. The statements listed below are required to be true for all implementations of the System.Object.Equals(System.Object) method. In the list, x, y, and z represent non-null object references. See System.Object.GetHashCode for additional required behaviors pertaining to the System.Object.Equals(System.Object) method. Implementations of System.Object.Equals(System.Object) should not throw exceptions. The System.Object.Equals(System.Object) method tests for referential equality , which means that System.Object.Equals(System.Object) returns true if the specified instance of Object and the current instance are the same instance; otherwise, it returns false . An implementation of the System.Object.Equals(System.Object) method is shown in the following C# code: public virtual bool Equals(Object obj) { return this == obj; } For some kinds of objects, it is desirable to have System.Object.Equals(System.Object) test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same "value", even if they are not the same instance. The definition of what constitutes an object's "value" is up to the implementer of the type, but it is typically some or all of the data stored in the instance variables of the object. For example, the value of a System.String is based on the characters of the string; the Equals method of the System.String class returns true for any two string instances that contain exactly the same characters in the same order. When the Equals method of a base class provides value equality, an override of Equals in a class derived from that base class should invoke the inherited implementation of Equals . It is recommended (but not required) that types overriding System.Object.Equals(System.Object) also override System.Object.GetHashCode. Hashtables cannot be relied on to work correctly if this recommendation is not followed. If your programming language supports operator overloading, and if you choose to overload the equality operator for a given type, that type should override the Equals method. Such implementations of the Equals method should return the same results as the equality operator. Following this guideline will help ensure that class library code using Equals (such as System.Collections.ArrayList and System.Collections.Hashtable ) behaves in a manner that is consistent with the way the equality operator is used by application code. If you are implementing a value type, you should follow these guidelines: For reference types, the guidelines are as follows: If you implement System.IComparable on a given type, you should override Equals on that type. The System.Object.Equals(System.Object) method is called by methods in collections classes that perform search operations, including the System.Array.IndexOf(System.Array,System.Object) method and the System.Collections.ArrayList.Contains(System.Object) method. Example: Example 1: The following example contains two calls to the default implementation of System.Object.Equals(System.Object) . using System;
class MyClass {
static void Main() {
Object obj1 = new Object();
Object obj2 = new Object();
Console.WriteLine(obj1.Equals(obj2));
obj1 = obj2;
Console.WriteLine(obj1.Equals(obj2));
}
}
The output is False True Example 2: The following example shows a Point class that overrides the System.Object.Equals(System.Object) method to provide value equality and a class Point3D, which is derived from Point . Because Point's override of System.Object.Equals(System.Object) is the first in the inheritance chain to introduce value equality, the Equals method of the base class (which is inherited from System.Object and checks for referential equality) is not invoked. However, Point3D.Equals invokes Point.Equals because Point implements Equals in a manner that provides value equality. using System;
public class Point: object {
int x, y;
public override bool Equals(Object obj) {
//Check for null and compare run-time types.
if (obj == null || GetType() != obj.GetType()) return false;
Point p = (Point)obj;
return (x == p.x) && (y == p.y);
}
public override int GetHashCode() {
return x ^ y;
}
}
class Point3D: Point {
int z;
public override bool Equals(Object obj) {
return base.Equals(obj) && z == ((Point3D)obj).z;
}
public override int GetHashCode() {
return base.GetHashCode() ^ z;
}
}
The Point.Equals method checks that the obj
argument is non-null and that it references an instance of the same type as this
object. If either of those checks fail, the method returns false. The
System.Object.Equals(System.Object) method uses
System.Object.GetType to determine whether
the run-time types of the two objects are identical. (Note that
typeof is not used here because it returns the static type.) If
instead the method had used a check of the form
In Point3D.Equals , the inherited Equals method is invoked before anything else is done; the inherited Equals method checks to see that obj is non-null, that obj is an instance of the same class as this object, and that the inherited instance variables match. Only when the inherited Equals returns true does the method compare the instance variables introduced in the subclass. Specifically, the cast to Point3D is not executed unless obj has been determined to be of type Point3D or a subclass of Point3D . Example 3: In the previous example, operator == (the equality operator) is used to compare the individual instance variables. In some cases, it is appropriate to use the System.Object.Equals(System.Object) method to compare instance variables in an Equals implementation, as shown in the following example: using System;
class Rectangle {
Point a, b;
public override bool Equals(Object obj) {
if (obj == null || GetType() != obj.GetType()) return false;
Rectangle r = (Rectangle)obj;
//Use Equals to compare instance variables
return a.Equals(r.a) && b.Equals(r.b);
}
public override int GetHashCode() {
return a.GetHashCode() ^ b.GetHashCode();
}
}
Example 4: In some languages, such as C#, operator overloading is supported. When a type overloads operator ==, it should also override the System.Object.Equals(System.Object) method to provide the same functionality. This is typically accomplished by writing the Equals method in terms of the overloaded operator ==. For example: using System;
public struct Complex {
double re, im;
public override bool Equals(Object obj) {
return obj is Complex && this == (Complex)obj;
}
public override int GetHashCode() {
return re.GetHashCode() ^ im.GetHashCode();
}
public static bool operator ==(Complex x, Complex y) {
return x.re == y.re && x.im == y.im;
}
public static bool operator !=(Complex x, Complex y) {
return !(x == y);
}
}
Because Complex is a C# struct (a value type), it is known that there will be no subclasses of Complex . Therefore, the System.Object.Equals(System.Object) method need not compare the GetType() results for each object, but can instead use the is operator to check the type of the obj parameter. |
public static System.Boolean Equals(System.Object objA, System.Object objB) Determines whether two object references are equal. Parameter objA: First object to compare. Parameter objB: Second object to compare. Returns: true if one or more of the following statements is true: otherwise returns false. This static method checks for null references before it calls objA.Equals(objB ) and returns false if either objA or objB is null. If the Equals(object obj) implementation throws an exception, this method throws an exception. Example: The following example demonstrates the System.Object.Equals(System.Object) method. using System;
public class MyClass {
public static void Main() {
string s1 = "Tom";
string s2 = "Carol";
Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = "Tom";
s2 = "Tom";
Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = null;
s2 = "Tom";
Console.WriteLine("Object.Equals(null, \"{1}\") => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = "Carol";
s2 = null;
Console.WriteLine("Object.Equals(\"{0}\", null) => {2}",
s1, s2, Object.Equals(s1, s2));
s1 = null;
s2 = null;
Console.WriteLine("Object.Equals(null, null) => {2}",
s1, s2, Object.Equals(s1, s2));
}
}
The output is Object.Equals("Tom", "Carol") => False Object.Equals("Tom", "Tom") => True Object.Equals(null, "Tom") => False Object.Equals("Carol", null) => False Object.Equals(null, null) => True |
public System.Void Finalize() Allows a System.Object to perform cleanup operations before the memory allocated for the System.Object is automatically reclaimed. During execution, System.Object.Finalize is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to System.GC.SuppressFinalize(System.Object). During shutdown of an application domain, System.Object.Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. System.Object.Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object) has not been subsequently called. Conforming implementations of the CLI are required to make every effort to ensure that for every object that has not been exempted from finalization, the System.Object.Finalize method is called after the object becomes inaccessible. However, there may be some circumstances under which Finalize is not called. Conforming CLI implementations are required to explicitly specify the conditions under which Finalize is not guaranteed to be called. For example, Finalize might not be guaranteed to be called in the event of equipment failure, power failure, or other catastrophic system failures. In addition to System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object), conforming implementations of the CLI are allowed to provide other mechanisms that affect the behavior of System.Object.Finalize . Any mechanisms provided are required to be specified by the CLI implementation. The order in which the Finalize methods of two objects are run is unspecified, even if one object refers to the other. The thread on which Finalize is run is unspecified. Every implementation of System.Object.Finalize in a derived type is required to call its base type's implementation of Finalize . This is the only case in which application code calls System.Object.Finalize . The System.Object.Finalize implementation does nothing. A type should implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed. Because Finalize methods may be invoked in any order (including from multiple threads), synchronization may be necessary if the Finalize method may interact with other objects, whether accessible or not. Furthermore, since the order in which Finalize is called is unspecified, implementers of Finalize (or of destructors implemented through overriding Finalize) must take care to correctly handle references to other objects, as their Finalize method may already have been invoked. In general, referenced objects should not be considered valid during finalization. See the System.IDisposable interface for an alternate means of disposing of resources. For C# developers: Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type's destructor. In C# code, System.Object.Finalize cannot be called or overridden. |
public virtual System.Int32 GetHashCode() Generates a hash code for the current instance. Returns: A System.Int32 containing the hash code for the current instance. System.Object.GetHashCode serves as a hash function for a specific type. A hash function is used to quickly generate a number (a hash code) corresponding to the value of an object. Hash functions are used with hashtables. A good hash function algorithm rarely generates hash codes that collide. For more information about hash functions, see The Art of Computer Programming , Vol. 3, by Donald E. Knuth. All implementations of System.Object.GetHashCode are required to ensure that for any two object references x and y, if x.Equals(y) == true, then x.GetHashCode() == y.GetHashCode(). Hash codes generated by System.Object.GetHashCode need not be unique. Implementations of System.Object.GetHashCode are not permitted to throw exceptions. The System.Object.GetHashCode implementation attempts to produce a unique hash code for every object, but the hash codes generated by this method are not guaranteed to be unique. Therefore, System.Object.GetHashCode may generate the same hash code for two different instances. It is recommended (but not required) that types overriding System.Object.GetHashCode also override System.Object.Equals(System.Object) . Hashtables cannot be relied on to work correctly if this recommendation is not followed. Use this method to obtain the hash code of an object. Hash codes should not be persisted (i.e. in a database or file) as they are allowed to change from run to run. Example: Example 1 In some cases, System.Object.GetHashCode is implemented to simply return an integer value. The following example illustrates an implementation of System.Int32.GetHashCode , which returns an integer value: using System;
public struct Int32 {
int value;
//other methods...
public override int GetHashCode() {
return value;
}
}
Example 2 Frequently, a type has multiple data members that can participate in generating the hash code. One way to generate a hash code is to combine these fields using an xor (exclusive or) operation, as shown in the following example: using System;
public struct Point {
int x;
int y;
//other methods
public override int GetHashCode() {
return x ^ y;
}
}
Example 3 The following example illustrates another case where the type's fields are combined using xor (exclusive or) to generate the hash code. Notice that in this example, the fields represent user-defined types, each of which implements System.Object.GetHashCode (and should implement System.Object.Equals(System.Object) as well): using System;
public class SomeType {
public override int GetHashCode() {
return 0;
}
}
public class AnotherType {
public override int GetHashCode() {
return 1;
}
}
public class LastType {
public override int GetHashCode() {
return 2;
}
}
public class MyClass {
SomeType a = new SomeType();
AnotherType b = new AnotherType();
LastType c = new LastType();
public override int GetHashCode () {
return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
}
}
Avoid implementing System.Object.GetHashCode in a manner that results in circular references. In other words, if AClass.GetHashCode calls BClass.GetHashCode, it should not be the case that BClass.GetHashCode calls AClass.GetHashCode. Example 4 In some cases, the data member of the class in which you are implementing System.Object.GetHashCode is bigger than a System.Int32. In such cases, you could combine the high order bits of the value with the low order bits using an XOR operation, as shown in the following example: using System;
public struct Int64 {
long value;
//other methods...
public override int GetHashCode() {
return ((int)value ^ (int)(value >> 32));
}
}
|
public System.Type GetType() Gets the type of the current instance. Returns: The instance of System.Type that represents the run-time type (the exact type) of the current instance. For two objects x and y that have identical run-time types, System.Object.ReferenceEquals(System.Object,System.Object)(x.GetType(),y.GetType()) returns true . Example: The following example demonstrates the fact that System.Object.GetType returns the run-time type of the current instance: using System;
public class MyBaseClass: Object {
}
public class MyDerivedClass: MyBaseClass {
}
public class Test {
public static void Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;
Console.WriteLine("mybase: Type is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
}
}
The output is mybase: Type is MyBaseClass myDerived: Type is MyDerivedClass object o = myDerived: Type is MyDerivedClass MyBaseClass b = myDerived: Type is MyDerivedClass |
protected System.Object MemberwiseClone() Creates a shallow copy of the current instance. Returns: A shallow copy of the current instance. The run-time type (the exact type) of the returned object is the same as the run-time type of the object that was copied. System.Object.MemberwiseClone creates a new instance of the same type as the current instance and then copies each of the object's non-static fields in a manner that depends on whether the field is a value type or a reference type. If the field is a value type, a bit-by-bit copy of all the field's bits is performed. If the field is a reference type, only the reference is copied. The algorithm for performing a shallow copy is as follows (in pseudo-code): for each instance field f in this instance if (f is a value type) bitwise copy the field if (f is a reference type) copy the reference end for loop This mechanism is referred to as a shallow copy because it copies rather than clones the non-static fields. Because System.Object.MemberwiseClone implements the above algorithm, for any object, a, the following statements are required to be true: System.Object.MemberwiseClone does not call any of the type's constructors. If System.Object.Equals(System.Object) has been overridden, a.MemberwiseClone().Equals(a) might return false . For an alternate copying mechanism, see System.ICloneable . System.Object.MemberwiseClone is protected (rather than public) to ensure that from verifiable code it is only possible to clone objects of the same class as the one performing the operation (or one of its subclasses). Although cloning an object does not directly open security holes, it does allow an object to be created without running any of its constructors. Since these constructors may establish important invariants, objects created by cloning may not have these invariants established, and this may lead to incorrect program behavior. For example, a constructor might add the new object to a linked list of all objects of this class, and cloning the object would not add the new object to that list -- thus operations that relied on the list to locate all instances would fail to notice the cloned object. By making the method protected, only objects of the same class (or a subclass) can produce a clone and implementers of those classes are (presumably) aware of the appropriate invariants and can arrange for them to be true without necessarily calling a constructor. Example: The following example shows a class called MyClass as well as a representation of the instance of MyClass returned by System.Object.MemberwiseClone . using System;
class MyBaseClass {
public static string CompanyName = "My Company";
public int age;
public string name;
}
class MyDerivedClass: MyBaseClass {
static void Main() {
//Create an instance of MyDerivedClass
//and assign values to its fields.
MyDerivedClass m1 = new MyDerivedClass();
m1.age = 42;
m1.name = "Sam";
//Do a shallow copy of m1
//and assign it to m2.
MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
}
}
A graphical representation of m1 and m2 might look like this
+---------------+
| 42 | m1
+---------------+
| +---------|-----------------> "Sam"
+---------------+ /|\
|
+---------------+ |
| 42 | | m2
+---------------+ |
| +--------|---------------------|
+---------------+
|
public static System.Boolean ReferenceEquals(System.Object objA, System.Object objB) Determines whether two object references are identical. Parameter objA: First object to compare. Parameter objB: Second object to compare. Returns: True if a and b refer to the same object or are both null references; otherwise, false. This static method provides a way to compare two objects for reference equality. It does not call any user-defined code, including overrides of System.Object.Equals(System.Object) . Example: using System;
class MyClass {
static void Main() {
object o = null;
object p = null;
object q = new Object();
Console.WriteLine(Object.ReferenceEquals(o, p));
p = q;
Console.WriteLine(Object.ReferenceEquals(p, q));
Console.WriteLine(Object.ReferenceEquals(o, p));
}
}
The output is True True False |
public virtual System.String ToString() Creates and returns a System.String representation of the current instance. Returns: A System.String representation of the current instance. System.Object.ToString returns a string whose content is intended to be understood by humans. Where the object contains culture-sensitive data, the string representation returned by System.Object.ToString takes into account the current system culture. For example, for an instance of the System.Double class whose value is zero, the implementation of System.Double.ToString might return "0.00" or "0,00" depending on the current UI culture. Although there are no exact requirements for the format of the returned string, it should as much as possible reflect the value of the object as perceived by the user. System.Object.ToString is equivalent to calling System.Object.GetType to obtain the System.Type object for the current instance and then returning the result of calling the System.Object.ToString implementation for that type. The value returned includes the full name of the type. It is recommended, but not required, that System.Object.ToString be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as System.Int32, implement System.Object.ToString so that it returns the string form of the value the object represents. Subclasses that require more control over the formatting of strings than System.Object.ToString provides should implement System.IFormattable, whose System.Object.ToString method uses the culture of the current thread. Example: The following example outputs the textual description of the value of an object of type System.Object to the console. using System;
class MyClass {
static void Main() {
object o = new object();
Console.WriteLine (o.ToString());
}
}
The output is System.Object |