extends System.ValueType
extends System.IComparable
extends System.IFormattable
| Variables: |
|---|
public System.DateTime Date |
public System.Int32 Day |
public System.Int32 DayOfYear |
public System.Int32 Hour |
public System.Int32 Millisecond |
public System.Int32 Minute |
public System.Int32 Month |
public static System.DateTime Now |
public System.Int32 Second |
public System.Int64 Ticks |
public System.TimeSpan TimeOfDay |
public static System.DateTime Today |
public static System.DateTime UtcNow |
public System.Int32 Year |
| Constructors: |
|---|
public System.DateTime(System.Int32 year, System.Int32 month, System.Int32 day, System.Int32 hour, System.Int32 minute, System.Int32 second, System.Int32 millisecond) Constructs and initializes a new instance of the System.DateTime structure with a specified year, month, day, hour, minute, second, and millisecond. Parameter year: A System.Int32 containing the year (1 through 9999). Parameter month: A System.Int32 containing the month (1 through 12). Parameter day: A System.Int32 containing the day (1 through the number of days in month). Parameter hour: A System.Int32 containing the hours (0 through 23). Parameter minute: A System.Int32 containing the minutes (0 through 59). Parameter second: A System.Int32 containing the seconds (0 through 59). Parameter millisecond: A System.Int32 containing the milliseconds. Throws: : year is less than 1 or greater than 9999 -or- month is less than 1 or greater than 12 -or- day is less than 1 or greater than the number of days in month -or- hour is less than 0 or greater than 23 -or- minute is less than 0 or greater than 59 -or- second is less than 0 or greater than 59 -or- millisecond is less than 0 or greater than 999 Throws: : The specified parameters evaluate to a date less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. |
public System.DateTime(System.Int32 year, System.Int32 month, System.Int32 day, System.Int32 hour, System.Int32 minute, System.Int32 second) Constructs and initializes a new instance of the System.DateTime structure with a specified year, month, day, hour, minute, and second. Parameter year: A System.Int32 containing the year (1 through 9999). Parameter month: A System.Int32 containing the month (1 through 12). Parameter day: A System.Int32 containing the day (1 through the number of days in month). Parameter hour: A System.Int32 containing the hours (0 through 23). Parameter minute: A System.Int32 containing the minutes (0 through 59). Parameter second: A System.Int32 containing the seconds (0 through 59). Throws: : year is less than 1 or greater than 9999 -or- month is less than 1 or greater than 12 -or- day is less than 1 or greater than the number of days in month -or- hour is less than 0 or greater than 23 -or- minute is less than 0 or greater than 59 -or- second is less than 0 or greater than 59 |
public System.DateTime(System.Int64 ticks) Constructs and initializes a new instance of the System.DateTime structure with the date and time expressed in 100-nanosecond units. Parameter ticks: A System.Int64containing the date and time expressed in 100-nanosecond units. Throws: : The date and time represented by ticks is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. |
public System.DateTime(System.Int32 year, System.Int32 month, System.Int32 day) Constructs and initializes a new instance of the System.DateTime structure with a specified year, month, and day. Parameter year: A System.Int32 containing the year (1 through 9999). Parameter month: A System.Int32 containing the month (1 through 12). Parameter day: A System.Int32 containing the day (1 through the number of days in month). Throws: : year is less than 1 or greater than 9999 -or- month is less than 1 or greater than 12 -or- day is less than 1 or greater than the number of days in month The time of day for the resulting System.DateTime is midnight (00:00:00). |
| Functions: |
|---|
public System.DateTime Add(System.TimeSpan value) Adds the value of a specified System.TimeSpan instance to the current instance. Parameter value: A System.TimeSpan instance. Returns: A System.DateTime instance set to the sum of the date and time of the current instance and the time interval represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. A specified System.TimeSpan is added to the current instance of System.DateTime, and the result is returned as a new System.DateTime. |
public System.DateTime AddDays(System.Double value) Adds a specified number of days to the value of the current instance. Parameter value: A System.Double containing the number of whole and fractional days. For example, 4.5 is equivalent to 4 days, 12 hours, 0 minutes, 0 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of days represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. value is rounded to the nearest tick. |
public System.DateTime AddHours(System.Double value) Adds a specified number of hours to the value of the current instance. Parameter value: A System.Double containing the number of whole and fractional hours. For example, 4.5 is equivalent to 4 hours, 30 minutes, 0 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of hours represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. value is rounded to the nearest tick. |
public System.DateTime AddMilliseconds(System.Double value) Adds a specified number of milliseconds to the value of the current instance. Parameter value: A System.Double containing the number of whole and fractional milliseconds. For example, 4.5 is equivalent to 4 milliseconds and 5,000 ticks. value can be negative or positive. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of milliseconds represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. value is rounded to the nearest tick. |
public System.DateTime AddMinutes(System.Double value) Adds a specified number of minutes to the value of the current instance. Parameter value: A System.Double containing the number of whole and fractional minutes. For example, 4.5 is equivalent to 4 minutes, 30 seconds, 0 milliseconds, and 0 ticks. value can be negative or positive. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of minutes represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. value is rounded to the nearest tick. |
public System.DateTime AddMonths(System.Int32 months) Adds a specified number of months to the value of the current instance. Parameter months: A System.Int32 containing the number of months. months can be positive or negative, and may be greater than the number of months in a year. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and months. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. -or- The months parameter is less than -120,000 or greater than 120,000 This method does not change the value of the current DateTime instance. Instead, a new DateTime instance is returned whose value is the result of this operation. |
public System.DateTime AddSeconds(System.Double value) Adds a specified number of seconds to the value of the current instance. Parameter value: A System.Double containing the number of whole and fractional seconds. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks. value can be positive or negative. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of seconds represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. value is rounded to the nearest tick. |
public System.DateTime AddTicks(System.Int64 value) Adds a specified number of ticks to the value of the current instance. Parameter value: A System.Int64 containing the number of 100-nanosecond ticks. value can be positive or negative. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the time represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. |
public System.DateTime AddYears(System.Int32 value) Adds a specified number of years to the value of the current instance. Parameter value: A System.Int32 containing the number of years. value can be positive or negative. Returns: A System.DateTime instance set to the sum of the date and time represented by the current instance and the number of years represented by value. Throws: : The resulting System.DateTime is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. |
public static System.Int32 Compare(System.DateTime t1, System.DateTime t2) Returns the sort order of the two specified instances of System.DateTime . Parameter t1: The first System.DateTime. Parameter t2: The second System.DateTime. Returns: A System.Int32 containing a value that reflects the sort order of the two specified instances of System.DateTime. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number. |
public System.Int32 CompareTo(System.Object value) Returns the sort order of the current instance compared to the specified System.Object . Parameter value: The System.Object to compare to the current instance. Returns: A System.Int32 containing a value that reflects the sort order of the current instance as compared to value. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number. Throws: : value is not a System.DateTime and is not a null reference. Any instance of System.DateTime, regardless of its value, is considered greater than a null reference. This method is implemented to support the System.IComparable interface. |
public static System.Int32 DaysInMonth(System.Int32 year, System.Int32 month) Returns the number of days in a specified month of a specified year. Parameter year: A System.Int32 containing the year. Parameter month: The month (a System.Int32 between 1 and 12). Returns: A System.Int32 set to the number of days in the specified month for the specified year. If the specified month is February, the return value is 28 or 29 depending upon whether the specified year is a leap year. Throws: : month is less than 1 or greater than 12. |
public System.Boolean Equals(System.Object value) Returns a System.Boolean indicating whether the current instance is equal to a specified object. Parameter value: A System.Object to compare with the current instance. Returns: true if value is a specified System.DateTime instance is equal to the current instance; otherwise, false. This method overrides System.Object.Equals(System.Object). |
public static System.Boolean Equals(System.DateTime t1, System.DateTime t2) Returns a System.Boolean indicating whether two specified instances of System.DateTime are equal. Parameter t1: The first System.DateTime. Parameter t2: The second System.DateTime. Returns: true if the two System.DateTime values are equal; otherwise, false. |
public System.Int32 GetHashCode() Generates a hash code for the current instance. Returns: A System.Int32 containing the hash code for this instance. The algorithm used to generate the hash code is unspecified. This method overrides System.Object.GetHashCode. |
public static System.Boolean IsLeapYear(System.Int32 year) Returns a System.Boolean value indicating whether a specified year is a leap year. Parameter year: A System.Int32 representing the year. year can be positive or negative. Returns: true if the specified year is a leap year; otherwise, false. |
public static System.DateTime op_Addition(System.DateTime d, System.TimeSpan t) Adds a specified System.TimeSpan value to a specified System.DateTime value. Parameter d: A System.DateTime value. Parameter t: A System.TimeSpan value. Returns: A System.DateTime instance that is the sum of the values of d and t . Throws: : The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. The returned value is equivalent to DateTime( d.Ticks + t.Ticks ). |
public static System.Boolean op_Equality(System.DateTime d1, System.DateTime d2) Returns a System.Boolean value indicating whether the two specified instances of System.DateTime are equal. Parameter d1: The first System.DateTime to compare. Parameter d2: The second System.DateTime to compare. Returns: true if d1.Ticks value is equal to the d2.Ticks value; otherwise, false. |
public static System.Boolean op_GreaterThan(System.DateTime t1, System.DateTime t2) Returns a System.Boolean value indicating whether one specified System.DateTime is greater than another specified System.DateTime. Parameter t1: A System.DateTime. Parameter t2: A System.DateTime. Returns: true if t1.Ticks value is greater than the t2.Ticks value; otherwise, false. |
public static System.Boolean op_GreaterThanOrEqual(System.DateTime t1, System.DateTime t2) Returns a System.Boolean value indicating whether one specified System.DateTime is greater than or equal to another specified System.DateTime. Parameter t1: A System.DateTime. Parameter t2: A System.DateTime. Returns: true if t1.Ticks value is greater than or equal to t2.Ticks value; otherwise, false. |
public static System.Boolean op_Inequality(System.DateTime d1, System.DateTime d2) Returns a System.Boolean value indicating whether two specified instances of System.DateTime are not equal. Parameter d1: A System.DateTime. Parameter d2: A System.DateTime. Returns: true if d1.Ticks value is not equal to d2.Ticks value; otherwise, false. |
public static System.Boolean op_LessThan(System.DateTime t1, System.DateTime t2) Returns a System.Boolean value indicating whether one specified System.DateTime is less than another specified System.DateTime. Parameter t1: A System.DateTime. Parameter t2: A System.DateTime. Returns: true if t1.Ticks value is less than t2.Ticks value; otherwise, false. |
public static System.Boolean op_LessThanOrEqual(System.DateTime t1, System.DateTime t2) Returns a System.Boolean value indicating whether one specified System.DateTime is less than or equal to another specified System.DateTime. Parameter t1: A System.DateTime. Parameter t2: A System.DateTime. Returns: true if t1.Ticks value is less than or equal to t2.Ticks value; otherwise, false. |
public static System.DateTime op_Subtraction(System.DateTime d, System.TimeSpan t) Subtracts a specified System.TimeSpan from a specified System.DateTime . Parameter d: A System.DateTime. Parameter t: A System.TimeSpan. Returns: A System.DateTime whose value is the value of d minus the value of t . Throws: : The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. The returned value is equivalent to System.DateTime( d.Ticks - t.Ticks ). |
public static System.TimeSpan op_Subtraction(System.DateTime d1, System.DateTime d2) Subtracts a specified System.DateTime from another specified System.DateTime value, producing a time interval. Parameter d1: A System.DateTime (the minuend). Parameter d2: A System.DateTime (the subtrahend). Returns: A System.TimeSpan that is the time interval between d1 and d2. Throws: : The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. The returned value is equivalent to System.TimeSpan( d1.Ticks - d2.Ticks). |
public static System.DateTime Parse(System.String s) Returns the specified System.String converted to a System.DateTime value. Parameter s: A System.String containing a value to convert. The string is interpreted using the System.Globalization.DateTimeStyles.None style. Returns: The System.DateTime value obtained from s. Throws: : s is a null reference. Throws: : s does not contain a valid string representation of a time or date and time. This version of System.DateTime.Parse(System.String) is equivalent to System.DateTime.Parse(System.String)(s, null, System.Globalization.DateTimeStyles.None ). The string s is parsed using the formatting information in a System.Globalization.DateTimeFormatInfo initialized for the current system culture. In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard System.DateTime patterns described in System.Globalization.DateTimeFormatInfo . If the string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m. Any leading, trailing, and inner white space characters are ignored. |
public static System.DateTime Parse(System.String s, System.IFormatProvider provider) Returns the specified System.String converted to a System.DateTime value. Parameter s: A System.String containing the value to convert. The string is interpreted using the System.Globalization.DateTimeStyles.None style. Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s . Returns: The System.DateTime value obtained from s. Throws: : s is a null reference. Throws: : s does not contain a valid string representation of a time or date and time. This version of System.DateTime.Parse(System.String) is equivalent to System.DateTime.Parse(System.String)(s, provider, System.Globalization.DateTimeStyles.None ). The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard System.DateTime patterns described in System.Globalization.DateTimeFormatInfo . If the string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m. Any leading, trailing, and inner white space characters are ignored. |
public static System.DateTime Parse(System.String s, System.IFormatProvider provider, System.Globalization.DateTimeStyles styles) Returns the specified System.String converted to a System.DateTime value. Parameter s: A System.String containing the value to convert. Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s . Parameter styles: One or more System.Globalization.DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator. Returns: The System.DateTime value obtained from s. Throws: : s is a null reference. Throws: : s does not contain a valid string representation of a time or date and time. The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. In order for the string to be successfully parsed, it is required to represent a date and time value in one of the standard System.DateTime patterns described in System.Globalization.DateTimeFormatInfo . If the string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used. In all other cases where a date is not specified, the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m. For all settings of the styles parameter, any leading, trailing, and inner white space characters are ignored. |
public static System.DateTime ParseExact(System.String s, System.String format, System.IFormatProvider provider, System.Globalization.DateTimeStyles style) Converts the System.String representation of a date and time to its System.DateTime equivalent using a specified style, the expected format, and culture-specific format information. Parameter s: A System.String containing a date and time to convert. The format of the string is required to match the specified format exactly. Parameter format: A System.String containing the expected format of s. For a list of valid format values, see System.Globalization.DateTimeFormatInfo . Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s . Parameter style: One or more System.Globalization.DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator. Returns: A System.DateTime equivalent to the date and time contained in s. Throws: : s or format is a null reference. Throws: : s or format is an empty string. -or- s does not contain a date and time that were recognized as the pattern specified in format. System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a System.DateTime from the string s. The string is required to specify a date and, optionally, a time in the provided format. The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. If the s string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used, and no leading, trailing, or inner white space characters are allowed. In all other cases where a date is not specified, the current date (System.DateTime.Now) is used. If the s string contains only a date and no time, this method assumes 12 a.m. For information on formatting system-supplied data types, see the System.IFormattable interface. Example: This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method. using System;
using System.Globalization;
public class DateTimeTest {
public static void Main() {
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
DateTime dt = DateTime.ParseExact(" January 22 ", dtfi.MonthDayPattern, null, DateTimeStyles.AllowWhiteSpaces);
Console.WriteLine(dt);
}
}
The output is 1/22/2001 12:00:00 AM |
public static System.DateTime ParseExact(System.String s, System.String[] formats, System.IFormatProvider provider, System.Globalization.DateTimeStyles style) Converts the System.String representation of a date and time to its System.DateTime equivalent using a specified style, an array of expected formats, and culture-specific format information. Parameter s: A System.String containing one or more dates and times to convert. The format of the string is required to match the specified format exactly. Parameter formats: A System.String array containing the expected formats of s. For a list of valid format values, see System.Globalization.DateTimeFormatInfo . Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s . Parameter style: One or more System.Globalization.DateTimeStyles values that specify the style of s. Specify multiple values for styles using the bitwise OR operator. Returns: A System.DateTime equivalent to the date and time contained in s. Throws: : s or formats is a null reference. Throws: : s or format is an empty string. -or- s does not contain a date and time that were recognized as the pattern specified in format System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a System.DateTime from the s System.String. The string is required to specify a date and, optionally, a time in the provided format. The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. If the s string contains only a time, and no date, and if the styles parameter is set to System.Globalization.DateTimeStyles.NoCurrentDateDefault the Gregorian year 1, month 1, day 1 are used, and no leading, trailing, or inner white space characters are allowed. In all other cases where a date is not specified, the current date (System.DateTime.Now) is used. If the s string contains only a date and no time, this method assumes 12 a.m. For information on formatting system-supplied data types, see the System.IFormattable interface. Example: This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method. using System;
using System.Globalization;
public class DateTimeTest {
public static void Main() {
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
string [] patterns = {dtfi.LongTimePattern, dtfi.ShortTimePattern};
DateTime dt = DateTime.ParseExact("10:11:12", patterns, null, DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine(dt);
}
}
The output is 1/1/0001 10:11:12 AM |
public static System.DateTime ParseExact(System.String s, System.String format, System.IFormatProvider provider) Converts the specified System.String representation of a date and time to its System.DateTime equivalent using a specified format and System.IFormatProvider . Parameter s: A System.String containing a date and time to convert. The format of the string is required to match the specified format exactly. Parameter format: A System.String containing the expected format of s. For a list of valid format values, see System.Globalization.DateTimeFormatInfo . Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s . Returns: A System.DateTime equivalent to the date and time contained in s. Throws: : s or format is a null reference. Throws: : s or format is an empty string. -or- s does not contain a date and time that were recognized as the pattern specified in format . System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) constructs a System.DateTime from the string s. The string is required to specify a date and, optionally, a time in the specified format. The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. If the s string contains only a time, and no date, then the current date (System.DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m. Leading, trailing, and inner white space characters are not allowed. For information on formatting system-supplied data types, see the System.IFormattable interface. Example: This example demonstrates the System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider) method. using System;
using System.Globalization;
public class DateTimeTest {
public static void Main() {
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
DateTime dt = DateTime.ParseExact("January 22", dtfi.MonthDayPattern, null);
Console.WriteLine(dt);
}
}
The output is 1/22/2001 12:00:00 AM |
public System.DateTime Subtract(System.TimeSpan value) Subtracts a specified System.TimeSpan from the current instance. Parameter value: An instance of System.TimeSpan. Returns: A new System.DateTime instance equal to the date and time represented by the current instance minus the time interval of the specified System.TimeSpan. Throws: : The resulting date and time is less than System.DateTime.MinValue or greater than System.DateTime.MaxValue. |
public System.TimeSpan Subtract(System.DateTime value) Subtracts a specified date and time from the current instance. Parameter value: An instance of System.DateTime. Returns: A System.TimeSpan interval equal to the date and time represented by the current instance minus the date and time represented by the specified System.DateTime. |
public System.DateTime ToLocalTime() Converts the universal time coordinate (UTC) time value in the current instance to local time. Returns: An instance of System.DateTime equivalent of the time value in the current instance, adjusted to the local time zone and daylight saving time. If the result is too large or too small to be represented as a System.DateTime , this method returns a System.DateTime set to System.DateTime.MaxValue or System.DateTime.MinValue . This method assumes that the current instance of System.DateTime holds the UTC time value, and not a local time. Each time it is invoked, this method performs the necessary modifications on the System.DateTime to derive the local time, whether the current System.DateTime holds the UTC time or not. The local time zone information is obtained from the operating system. |
public System.String ToLongDateString() Converts the date denoted by the current instance to its equivalent long date System.String representation. Returns: A System.String containing the same value as a System.String returned by System.DateTime.ToString ("D", null ). The value of the current instance is formatted using the long date format specifier, 'D'. This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString . For more information regarding the long date specifier, see System.Globalization.DateTimeFormatInfo . |
public System.String ToLongTimeString() Converts the time denoted by the current instance to its equivalent long time System.String representation. Returns: A System.String containing the same value as a System.String returned by System.DateTime.ToString ("T", null ). The value of the current instance is formatted using the long time format specifier, 'T'. This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString . For more information regarding the long time specifier, see System.Globalization.DateTimeFormatInfo . |
public System.String ToShortDateString() Converts the date denoted by the current instance to its equivalent short date System.String representation. Returns: A System.String containing the same value as a System.String returned by System.DateTime.ToString ("d", null ). The value of the current instance is formatted using the short date format specifier, 'd'. This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString . For more information regarding the short date specifier, see System.Globalization.DateTimeFormatInfo . |
public System.String ToShortTimeString() Converts the time denoted by this instance to its equivalent short time System.String representation. Returns: A System.String containing the same value as a System.String returned by System.DateTime.ToString ("t", null ). The value of the current instance is formatted using the short time format specifier, 't'. This format uses the culture of the current thread. To specify formatting using a different culture, use System.DateTime.ToString . For more information regarding the short time specifier, see System.Globalization.DateTimeFormatInfo . |
public System.String ToString(System.String format) Returns a System.String representation of the value of the current instance. Parameter format: A System.String that specifies the format of the returned string. For a list of valid values, see System.Globalization.DateTimeFormatInfo . Returns: A System.String representation of the current instance formatted as specified by format. The string takes into account the current system culture. Throws: : The length of the format string is 1, and it is not one of the format specifier characters defined for System.Globalization.DateTimeFormatInfo. -or- The format string does not contain a valid custom format pattern. This version of System.DateTime.ToString is equivalent to System.DateTime.ToString (format , null ). If format is a null reference, the general format specifier "G" is used. This method uses the culture information of the current thread. For information on formatting system-supplied data types, see the System.IFormattable interface. |
public System.String ToString() Returns a System.String representation of the value of the current instance. Returns: A System.String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture. This version of System.DateTime.ToString is equivalent to System.DateTime.ToString ("G", null ). For more information about the general format specifier ("G") see System.Globalization.DateTimeFormatInfo. This method overrides System.Object.ToString. |
public System.String ToString(System.String format, System.IFormatProvider provider) Returns a System.String representation of the value of the current instance. Parameter format: A System.String containing a character that specifies the format of the returned string. For a list of valid values, see System.Globalization.DateTimeFormatInfo . Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo instance containing culture-specific formatting information. Returns: A System.String representation of the current instance formatted as specified by format. The string takes into account the information in the System.Globalization.DateTimeFormatInfo supplied by provider. Throws: : The length of the format string is 1, and it is not one of the format specifier characters defined for System.Globalization.DateTimeFormatInfo. -or- The format string does not contain a valid custom format pattern. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. If format is a null reference, the general format specifier "G" is used. For more information regarding the standard format specifier, see System.Globalization.DateTimeFormatInfo. For information on formatting system-supplied data types, see the System.IFormattable interface. This method is implemented to support the System.IFormattable interface. |
public System.String ToString(System.IFormatProvider provider) Returns a System.String representation of the value of the current instance. Parameter provider: A System.IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo containing culture-specific formatting information. Returns: A System.String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the formatting information in the System.Globalization.DateTimeFormatInfo instance supplied by provider. This version of System.DateTime.ToString is equivalent to System.DateTime.ToString ("G", provider ). If provider is null or the System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used. The general format specifier ("G") provides the general date pattern including the long time form, equivalent to System.Globalization.DateTimeFormatInfo.ShortDatePattern combined with System.Globalization.DateTimeFormatInfo.LongTimePattern. For more information on format specifiers, see System.Globalization.DateTimeFormatInfo . For information on formatting system-supplied data types, see the System.IFormattable interface. |
public System.DateTime ToUniversalTime() Converts the current System.DateTime value to coordinated universal time (UTC). Returns: The UTC System.DateTime equivalent of the current System.DateTime value. If the result is too large or too small to be represented as a System.DateTime, the current function returns a System.DateTime set to System.DateTime.MaxValue or System.DateTime.MinValue . This method assumes that the current instance of System.DateTime holds the local time value, and not a UTC time. Therefore each time it is run, this method performs the necessary modifications on the System.DateTime to derive the UTC time, whether the current System.DateTime holds the local time or not. The local time zone information is obtained from the operating system. |
| Functions inherited from System.ValueType: |
|---|
public System.Boolean Equals(System.Object obj) Determines whether the current instance and a specified System.Object represent the same value. Parameter obj: The System.Object to compare the current instance to. Returns: true if obj and the current instance are of the same type and represent the same value; otherwise, false. This method overrides System.Object.Equals(System.Object) . |
public System.Int32 GetHashCode() Generates a hash code for the current instance. Returns: A System.Int32 containing the hash code for the current instance. The algorithm used to generate the hash code is unspecified. This method overrides System.Object.GetHashCode . |
public System.String ToString() Returns a System.String representation of the fully-qualified name of the type of the current instance. Returns: A System.String representation of the fully-qualified name of the type of the current instance. This method overrides System.Object.ToString. This method returns the System.Type.FullName property. |
| 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 |
| Functions inherited from System.IComparable: |
|---|
public System.Int32 CompareTo(System.Object obj) Returns the sort order of the current instance compared to the specified object. Parameter obj: The System.Object to compare to the current instance. Returns: A System.Int32 containing a value that reflects the sort order of the current instance as compared to object. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number. For any objects A, B and C, the following are required to be true: A.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero then B.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero then A.CompareTo(C) is required to return zero. If A.CompareTo(B) returns a value other than zero then B.CompareTo(A) is required to return a value of the opposite sign. If A.CompareTo(B) returns a value x not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to a value of the same sign as x and y . The exact behavior of this method is unspecified. The intent of this method is to provide a mechanism that orders instances of a class in a manner that is consistent with the mathematical definitions of the relational operators (<, >, and ==), without regard for class-specific definitions of the operators. Use the System.IComparable.CompareTo(System.Object) method to determine the ordering of instances of a class. |
| Functions inherited from System.IFormattable: |
|---|
public System.String ToString(System.String format, System.IFormatProvider formatProvider) Returns a System.String representation of the value of the current instance. Parameter format: A System.String that specifies the format of the returned string. If format is a null reference or the empty string, the default format defined for the type of the current instance is used. Parameter formatProvider: A System.IFormatProvider that supplies a formatting object containing culture-specific formatting information, or null . Returns: A System.String containing the value of the current instance formatted in accordance with format and formatProvider . Throws: : The specified format is invalid or cannot be used with the type of the current instance. Conforming implementations do not throw an exception when format and/or formatProvider are null references. If formatProvider is a null reference, the string is constructed using a system-supplied formatting object containing information for the current system culture. If format is null, the string is constructed using a system-supplied default format appropriate for the type of the current instance. If the object returned by formatProvider supplies a culture-specific representation of symbols or patterns included in format, the returned string is required to use the information supplied by formatProvider . Implement to allow consumers of a class to use format strings and formatting objects to control the way in which the class is represented as a string. Example: The following example demonstrates using the System.IFormattable.ToString(System.String,System.IFormatProvider) method to display values in a variety of formats. The current system culture is U.S. English, which provides the default values for the formatProvider parameter of System.IFormattable.ToString(System.String,System.IFormatProvider). using System;
class FormattableExample {
public static void Main() {
double d = 123.12345678901234;
string[] formats = {"C","E","e","F","G","N","P","R"};
for (int i = 0; i< formats.Length;i++)
Console.WriteLine("{0:R} as {1}: {2}",d,formats[i],d.ToString(formats[i],null));
string[]intFormats = {"D","x","X"};
int val = 255;
for (int i = 0; i< intFormats.Length;i++)
Console.WriteLine("{0} as {1}: {2}",val,intFormats[i],val.ToString(intFormats[i],null));
}
}
The output is 123.12345678901234 as C: $123.12 123.12345678901234 as E: 1.231235E+002 123.12345678901234 as e: 1.231235e+002 123.12345678901234 as F: 123.12 123.12345678901234 as G: 123.123456789012 123.12345678901234 as N: 123.12 123.12345678901234 as P: 12,312.35 % 123.12345678901234 as R: 123.12345678901234 255 as D: 255 255 as x: ff 255 as X: FF |