Type System.Math

extends System.Object

Variables:
Constructors:
Functions:

public static System.Decimal Abs(System.Decimal value)

Returns the absolute value of the specified System.Decimal .

Parameter value: A System.Decimal.

Returns: A System.Decimal containing the absolute value of value.

Example:

The following example demonstrates the System.Math.Abs(System.SByte)(System.Decimal) method.


using System;

public class MathAbsExample
{
   public static void Main()
   {
      Decimal d1 = Math.Abs( (Decimal)0.00 );
      Decimal d2 = Math.Abs( (Decimal)(-1.23) );
      Console.WriteLine("Math.Abs( (Decimal)0.00 ) returns {0}",d1);
      Console.WriteLine("Math.Abs( (Decimal)(-1.23) ) returns {0}",d2);
   }
}

The output is

Math.Abs( (Decimal)0.00 ) returns 0

Math.Abs( (Decimal)(-1.23) ) returns 1.23

public static System.Double Abs(System.Double value)

Returns the absolute value of the specified System.Double.

Parameter value: A System.Double.

Returns: A System.Double containing the absolute value of value. If value is equal to System.Double.NegativeInfinity or System.Double.PositiveInfinity, returns System.Double.PositiveInfinity. If value is equal to System.Double.NaN, returns System.Double.NaN.

public static System.Single Abs(System.Single value)

Returns the absolute value of the specified System.Single.

Parameter value: A System.Single.

Returns: A System.Single containing the absolute value of value. If value is equal to System.Single.NegativeInfinity or System.Single.PositiveInfinity, returns System.Single.PositiveInfinity. If value is equal to System.Single.NaN, returns System.Single.NaN.

public static System.Int64 Abs(System.Int64 value)

Returns the absolute value of the specified System.Int64.

Parameter value: A System.Int64.

Returns: A System.Int64 containing the absolute value of value.

Throws: : value equals System.Int64.MinValue.

public static System.Int32 Abs(System.Int32 value)

Returns the absolute value of the specified System.Int32.

Parameter value: A System.Int32.

Returns: A System.Int32 containing the absolute value of value.

Throws: : value equals System.Int32.MinValue.

public static System.Int16 Abs(System.Int16 value)

Returns the absolute value of the specified System.Int16.

Parameter value: A System.Int16.

Returns: A System.Int16 containing the absolute value of value.

Throws: : value equals System.Int16.MinValue.

public static System.SByte Abs(System.SByte value)

Returns the absolute value of the specified System.SByte.

Parameter value: A System.SByte.

Returns: A System.SByte containing the absolute value of value.

Throws: : value equals System.SByte.MinValue.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Abs(System.SByte)(System.Int16).

public static System.Double Acos(System.Double d)

Returns the angle whose cosine is the specified System.Double.

Parameter d: A System.Double representing a cosine, where -1 <= d <= 1.

Returns: A System.Double containing the value of an angle, , measured in radians, for which d is the cosine, such that 0 <= <= . If d < -1, d > 1, or d = System.Double.NaN, returns System.Double.NaN.

Multiply the return value by 180/ to convert from radians to degrees.

public static System.Double Asin(System.Double d)

Returns the angle whose sine is the specified System.Double.

Parameter d: A System.Double representing a sine, where -1 <= d <= 1.

Returns: A System.Double containing the value of an angle, , measured in radians, for which d is the sine, such that -/2 <= <= /2. If d < -1, d > 1, or d = System.Double.NaN, returns System.Double.NaN .

A positive return value represents a counterclockwise angle from the positive x-axis; a negative return value represents a clockwise angle. Multiply the return value by 180/ to convert from radians to degrees.

public static System.Double Atan(System.Double d)

Returns the angle whose tangent is the specified System.Double.

Parameter d: A System.Double that represents a tangent.

Returns: A System.Double containing the value of the angle, , measured in radians, for which d is the tangent, such that -/2 <= <= /2. The following table specifies the return value if d is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity .

A positive return value represents a counterclockwise angle from the positive x-axis; a negative return value represents a clockwise angle. Multiply the return value by 180/ to convert from radians to degrees.

public static System.Double Atan2(System.Double y, System.Double x)

Returns the angle whose tangent is the quotient of two specified System.Double values.

Parameter y: A System.Double representing the y coordinate of a point.

Parameter x: A System.Double representing the x coordinate of a point.

Returns: A System.Double containing the value of an angle, , measured in radians, such that - <= <= and tan = y/x, where (x, y) is a point in the Cartesian plane. If both x and y are any combination of System.Double.NegativeInfinity and System.Double.PositiveInfinity, System.Double.NaN is returned. If either x or y is equal to System.Double.NaN, System.Double.NaN is returned. The following table specifies the return value if x or y is equal to System.Double.NegativeInfinity or System.Double.PositiveInfinity.

The return value is the angle in the Cartesian plane formed by the x-axis, and a vector starting from the origin, (0,0), and terminating at the point, (x,y).

Example:

The following example demonstrates using the System.Math.Atan2(System.Double,System.Double) method.

using System;

public class MathAtan2Example
{

   public static void Main()
   {

      Double d1 = Math.Atan2(2,0);
      Double d2 = Math.Atan2(0,0);
      Console.WriteLine("Math.Atan2(2,0) returns {0}", d1);
      Console.WriteLine("Math.Atan2(0,0) returns {0}", d2);

   }

}

The output is

Math.Atan2(2,0) returns 1.5707963267949

Math.Atan2(0,0) returns 0

public static System.Int64 BigMul(System.Int32 a, System.Int32 b)

Produces the full product of two 32-bit numbers.

Parameter a: The first System.Int32 to multiply.

Parameter b: The second System.Int32 to multiply.

Returns: A System.Int64 containing the product of the specified numbers.

public static System.Double Ceiling(System.Double a)

Returns the smallest integer greater than or equal to the specified System.Double.

Parameter a: A System.Double .

Returns: A System.Double containing the value of the smallest integer greater than or equal to a. If a is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity , that value is returned.

Example:

The following example demonstrates using the System.Math.Ceiling(System.Double) method.

using System;

public class MathCeilingExample
{

   public static void Main()
   {

      Double d1 = Math.Ceiling(3.4);
      Double d2 = Math.Ceiling(-3.4);
      Console.WriteLine("Math.Ceiling(3.4) returns {0}", d1);
      Console.WriteLine("Math.Ceiling(-3.4) returns {0}", d2);

   }

}

The output is

Math.Ceiling(3.4) returns 4

Math.Ceiling(-3.4) returns -3

public static System.Double Cos(System.Double d)

Returns the cosine of the specified System.Double that represents an angle.

Parameter d: A System.Double that represents an angle measured in radians.

Returns: A System.Double containing the value of the cosine of d. If d is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity, returns System.Double.NaN.

Multiply by /180 to convert degrees to radians.

public static System.Double Cosh(System.Double value)

Returns the hyperbolic cosine of the specified System.Double that represents an angle.

Parameter value: A System.Double that represents an angle measured in radians.

Returns: The hyperbolic cosine of value. If value is equal to System.Double.NegativeInfinity or System.Double.PositiveInfinity, returns System.Double.PositiveInfinity. If value is equal to System.Double.NaN, returns System.Double.NaN.

Multiply by /180 to convert degrees to radians.

public static System.Int32 DivRem(System.Int32 a, System.Int32 b, System.Int32 result)

Returns the quotient of two numbers, also passing the remainder as an output parameter.

Parameter a: A System.Int32 that contains the dividend.

Parameter b: A System.Int32 that contains the divisor.

Parameter result: A System.Int32 that receives the remainder.

Returns: A System.Int32 containing the quotient of the specified numbers.

public static System.Int64 DivRem(System.Int64 a, System.Int64 b, System.Int64 result)

Returns the quotient of two numbers, also passing the remainder as an output parameter.

Parameter a: A System.Int64 that contains the dividend.

Parameter b: A System.Int64 that contains the divisor.

Parameter result: A System.Int64 that receives the remainder.

Returns: A System.Int64 containing the quotient of the specified numbers.

public static System.Double Exp(System.Double d)

Returns e raised to the specified System.Double that represents an exponent.

Parameter d: A System.Double that represents an exponent.

Returns: A System.Double equal to the number e raised to the power of d. If d equals System.Double.NaN or System.Double.PositiveInfinity, returns that value. If d equals System.Double.NegativeInfinity, returns 0.

Use the System.Math.Pow(System.Double,System.Double) method to calculate powers of other bases. System.Math.Exp(System.Double) is the inverse of System.Math.Log(System.Double) .

public static System.Double Floor(System.Double d)

Returns the largest integer less than or equal to the specified System.Double.

Parameter d: A System.Double .

Returns: A System.Double containing the value of the largest integer less than or equal to d. If d is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity, that value is returned..

The behavior of this method follows IEEE Standard 754, section 4.

Example:

The following example demonstrates using the System.Math.Floor(System.Double) method.

using System;

public class MathFloorExample
{

   public static void Main()
   {

      Double d1 = Math.Floor(3.4);
      Double d2 = Math.Floor(-3.4);
      Console.WriteLine("Math.Floor(3.4) returns {0}", d1);
      Console.WriteLine("Math.Floor(-3.4) returns {0}", d2);

   }

}

The output is

Math.Floor(3.4) returns 3

Math.Floor(-3.4) returns -4

public static System.Double IEEERemainder(System.Double x, System.Double y)

Returns the remainder resulting from the division of one specified System.Double by another specified System.Double.

Parameter x: A System.Double that represents a dividend.

Parameter y: A System.Double that represents a divisor.

Returns: A System.Double whose value is as follows:

This operation complies with the remainder operation defined in Section 5.1 of ANSI/IEEE Std 754-1985; IEEE Standard for Binary Floating-Point Arithmetic; Institute of Electrical and Electronics Engineers, Inc; 1985. For more information regarding the use of +0 and -0, see Section 3.1 of ANSI/IEEE Std 754-1985; IEEE Standard for Binary Floating-Point Arithmetic; Institute of Electrical and Electronics Engineers, Inc; 1985.

Example:

The following example demonstrates using the System.Math.IEEERemainder(System.Double,System.Double) method.

using System;

public class MathIEEERemainderExample
{

   public static void Main()
   {

      Double d1 = Math.IEEERemainder(3.54,0);
      Double d2 = Math.IEEERemainder(9.99,-3.33);
      Double d3 = Math.IEEERemainder(-9.99,3.33);
      Double d4 = Math.IEEERemainder(9.5,1.5);
      Console.WriteLine("Math.IEEERemainder(3.54,0) returns {0}", d1);
      Console.WriteLine("Math.IEEERemainder(9.99,-3.33) returns {0}", d2);
      Console.WriteLine("Math.IEEERemainder(-9.99,3.33) returns {0}", d3);
      Console.WriteLine("Math.IEEERemainder(9.5,1.5) returns {0}", d4);

   }

}

The output is

Math.IEEERemainder(3.54,0) returns NaN

Math.IEEERemainder(9.99,-3.33) returns 0

Math.IEEERemainder(-9.99,3.33) returns 0

Math.IEEERemainder(9.5,1.5) returns 0.5

public static System.Double Log(System.Double d)

Returns the natural logarithm of the specified System.Double.

Parameter d: A System.Double whose natural logarithm is to be found.

Returns: Returns a System.Double whose value is as follows.

d is specified as a base 10 number.

public static System.Double Log(System.Double a, System.Double newBase)

Returns the logarithm of the specified System.Double in the specified base.

Parameter a: A System.Double whose logarithm is to be found.

Parameter newBase: A System.Double containing the value of the base of the logarithm.

Returns: Returns a System.Double whose value is as follows: If a is equal to System.Double.PositiveInfinity and newBase is not equal System.Double.PositiveInfinity, System.Double.NegativeInfinity, or System.Double.NaN, returns System.Double.PositiveInfinity. If newBase is equal to System.Double.PositiveInfinity and a is not equal to System.Double.PositiveInfinity, System.Double.NegativeInfinity, or System.Double.NaN, returns 0. If both a and newBase are equal to System.Double.PositiveInfinity, or a or newBase is equal to System.Double.NaN or System.Double.NegativeInfinity, returns System.Double.NaN.

public static System.Double Log10(System.Double d)

Returns log of the specified System.Double.

Parameter d: A System.Double whose logarithm is to be found.

Returns: Returns a System.Double as indicated by the following table.

public static System.SByte Max(System.SByte val1, System.SByte val2)

Returns the greater of two specified System.SByte values.

Parameter val1: The first of two specified System.Byte values to compare.

Parameter val2: The second of two specified System.Byte values to compare.

Returns: A System.SByte that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Max(System.SByte,System.SByte)(System.Int16, System.Int16).

public static System.Byte Max(System.Byte val1, System.Byte val2)

Returns the greater of two specified System.Byte values.

Parameter val1: The first of two specified System.Byte values to compare.

Parameter val2: The second of two specified System.Byte values to compare.

Returns: A System.Byte that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

public static System.Int16 Max(System.Int16 val1, System.Int16 val2)

Returns the greater of two specified System.Int16 values.

Parameter val1: The first of two specified System.Int16 values to compare.

Parameter val2: The second of two specified System.Int16 values to compare.

Returns: A System.Int16 that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

public static System.UInt16 Max(System.UInt16 val1, System.UInt16 val2)

Returns the greater of two specified System.UInt16 values.

Parameter val1: The first of two specified System.UInt16 values to compare.

Parameter val2: The second of two specified System.UInt16 values to compare.

Returns: A System.UInt16 that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Max(System.SByte,System.SByte)(System.Int32, System.Int32).

public static System.Int32 Max(System.Int32 val1, System.Int32 val2)

Returns the greater of two specified System.Int32 values.

Parameter val1: The first of two specified System.Int32 values to compare.

Parameter val2: The second of two specified System.Int32 values to compare.

Returns: A System.Int32 that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

public static System.UInt32 Max(System.UInt32 val1, System.UInt32 val2)

Returns the greater of two specified System.UInt32 values.

Parameter val1: The first of two specified System.UInt32 values to compare.

Parameter val2: The second of two specified System.UInt32 values to compare.

Returns: A System.UInt32 that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Max(System.SByte,System.SByte)(System.Int64, System.Int64).

public static System.Int64 Max(System.Int64 val1, System.Int64 val2)

Returns the greater of two specified System.Int64 values.

Parameter val1: The first of two specified System.Int64 values to compare.

Parameter val2: The second of two specified System.Int64 values to compare.

Returns: A System.Int64 that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

public static System.UInt64 Max(System.UInt64 val1, System.UInt64 val2)

Returns the greater of two specified System.UInt64 values.

Parameter val1: The first of two specified System.UInt64 values to compare.

Parameter val2: The second of two specified System.UInt64 values to compare.

Returns: A System.UInt64 equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Max(System.SByte,System.SByte)(System.Decimal, System.Decimal).

public static System.Single Max(System.Single val1, System.Single val2)

Returns the greater of two specified System.Single values.

Parameter val1: The first of two specified System.Single values to compare.

Parameter val2: The second of two specified System.Single values to compare.

Returns: A System.Single equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2. If val1, val2, or both are equal to System.Single.NaN, System.Single.NaN is returned.

public static System.Double Max(System.Double val1, System.Double val2)

Returns the greater of two specified System.Double values.

Parameter val1: The first of two specified System.Double values to compare.

Parameter val2: The second of two specified System.Double values to compare.

Returns: A System.Double equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2. If val1 , val2, or both are equal to System.Double.NaN , System.Double.NaN is returned.

public static System.Decimal Max(System.Decimal val1, System.Decimal val2)

Returns the greater of two specified System.Decimal values.

Parameter val1: The first of two specified System.Decimal values to compare.

Parameter val2: The second of two specified System.Decimal values to compare.

Returns: A System.Decimal that is equal to val1 if val1 is greater than or equal to val2; otherwise, the return value is equal to val2.

public static System.UInt16 Min(System.UInt16 val1, System.UInt16 val2)

Returns the lesser of two specified System.UInt16 values.

Parameter val1: The first of two specified System.UInt16 values to compare.

Parameter val2: The second of two specified System.UInt16 values to compare.

Returns: A System.UInt16 equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2 .

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Min(System.SByte,System.SByte)(System.Int32, System.Int32).

public static System.Int16 Min(System.Int16 val1, System.Int16 val2)

Returns the lesser of two specified System.Int16 values.

Parameter val1: The first of two specified System.Int16 values to compare.

Parameter val2: The second of two specified System.Int16 values to compare.

Returns: A System.Int16 that is equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

public static System.Byte Min(System.Byte val1, System.Byte val2)

Returns the lesser of two specified System.Byte values.

Parameter val1: The first of two specified System.Byte values to compare.

Parameter val2: The second of two specified System.Byte values to compare.

Returns: A System.Byte equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

public static System.SByte Min(System.SByte val1, System.SByte val2)

Returns the lesser of two specified System.SByte values.

Parameter val1: The first of two specified System.SByte values to compare.

Parameter val2: The second of two specified System.SByte values to compare.

Returns: A System.SByte equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Min(System.SByte,System.SByte)(System.Int16, System.Int16).

public static System.UInt64 Min(System.UInt64 val1, System.UInt64 val2)

Returns the lesser of two specified System.UInt64 values.

Parameter val1: The first of two specified System.UInt64 values to compare.

Parameter val2: The second of two specified System.UInt64 values to compare.

Returns: A System.UInt64 equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Min(System.SByte,System.SByte)(System.Decimal, System.Decimal).

public static System.Single Min(System.Single val1, System.Single val2)

Returns the lesser of two specified System.Single values.

Parameter val1: The first of two specified System.Single values to compare.

Parameter val2: The second of two specified System.Single values to compare.

Returns: A System.Single equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2. If val1, val2, or both are equal to System.Single.NaN, System.Single.NaN is returned.

public static System.Int64 Min(System.Int64 val1, System.Int64 val2)

Returns the lesser of two specified System.Int64 values.

Parameter val1: The first of two specified System.Int64 values to compare.

Parameter val2: The second of two specified System.Int64 values to compare.

Returns: A System.Int64 equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

public static System.UInt32 Min(System.UInt32 val1, System.UInt32 val2)

Returns the lesser of two specified System.UInt32 values.

Parameter val1: The first of two specified System.UInt32 values to compare.

Parameter val2: The second of two specified System.UInt32 values to compare.

Returns: A System.UInt32 equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Min(System.SByte,System.SByte)(System.Int64, System.Int64).

public static System.Int32 Min(System.Int32 val1, System.Int32 val2)

Returns the lesser of two specified System.Int32 values.

Parameter val1: The first of two specified System.Int32 values to compare.

Parameter val2: The second of two specified System.Int32 values to compare.

Returns: A System.Int32 equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

public static System.Double Min(System.Double val1, System.Double val2)

Returns the lesser of two specified System.Double values.

Parameter val1: The first of two specified System.Double values to compare.

Parameter val2: The second of two specified System.Double values to compare.

Returns: A System.Double equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2. If val1, val2, or both are equal to System.Double.NaN, System.Double.NaN is returned.

public static System.Decimal Min(System.Decimal val1, System.Decimal val2)

Returns the lesser of two specified System.Decimal values.

Parameter val1: The first of two specified System.Decimal values to compare.

Parameter val2: The second of two specified System.Decimal values to compare.

Returns: A System.Decimal equal to val1 if val1 is less than or equal to val2; otherwise, the return value is equal to val2.

public static System.Double Pow(System.Double x, System.Double y)

Returns the specified System.Double raised to the specified power.

Parameter x: A System.Double to be raised to a power.

Parameter y: A System.Double that specifies that power.

Returns: A System.Double equal to x raised to the power y. The following table specifies the results if x or y is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity.

public static System.Decimal Round(System.Decimal d)

Returns the integer nearest the specified System.Decimal.

Parameter d: A System.Decimal to be rounded.

Returns: A System.Decimal containing the value of the integer nearest d. If d is exactly halfway between two integers, one of which is even and the other odd, then the even integer is returned.

The behavior of this method follows IEEE Standard 754, section 4.1.

Example:

The following example demonstrates using the System.Math.Round(System.Double)(System.Decimal) method.

using System;

public class MathRoundExample
{

   public static void Main()
   {

      Double d1 = Math.Round(4.4);
      Double d2 = Math.Round(4.5);
      Double d3 = Math.Round(4.6);
      Console.WriteLine("Math.Round(4.4) returns {0}", d1);
      Console.WriteLine("Math.Round(4.5) returns {0}", d2);
      Console.WriteLine("Math.Round(4.6) returns {0}", d3);

   }

}

The output is

Math.Round(4.4) returns 4

Math.Round(4.5) returns 4

Math.Round(4.6) returns 5

public static System.Double Round(System.Double value, System.Int32 digits)

Returns the number nearest the specified System.Double within the specified precision.

Parameter value: A System.Double to be rounded.

Parameter digits: A System.Int32 containing the value of the number of significant fractional digits (precision) in the return value. This number is required to be greater than or equal to 0 and less than or equal to 15.

Returns: A System.Double containing the value of the number nearest value with a precision equal to digits . If the digit in value that is in the 10 place is equal to 5 and there are no non-zero numbers in any less significant place, then the digit in the 10 place will be unchanged if it is even, else it will be set to the closest even integer value in the direction of the digit in the 10 place. If the precision of value is less than digits, then value is returned unchanged. If digits is zero, this method behaves in the same manner as System.Math.Round(System.Double) (value ).

Throws: : digits < 0. digits > 15.

The behavior of this method follows IEEE Standard 754, section 4.1.

Example:

The following example demonstrates using the System.Math.Round(System.Double)(System.Double, System.Int32) method.

using System;

public class MathRoundExample
{

   public static void Main()
   {

      Double d1 = Math.Round(3.44,1);
      Double d2 = Math.Round(3.45,1);
      Double d3 = Math.Round(3.55,1);
      Console.WriteLine("Math.Round(3.44, 1) returns {0}", d1);
      Console.WriteLine("Math.Round(3.45, 1) returns {0}", d2);
      Console.WriteLine("Math.Round(3.55, 1) returns {0}", d3);

   }

}

The output is

Math.Round(3.44, 1) returns 3.4

Math.Round(3.45, 1) returns 3.4

Math.Round(3.55, 1) returns 3.6

public static System.Double Round(System.Double a)

Returns the integer nearest the specified System.Double.

Parameter a: A System.Double to be rounded.

Returns: A System.Double containing the value of the integer nearest a. If a is exactly halfway between two integers, one of which is even and the other odd, then the even integer is returned.

The behavior of this method follows IEEE Standard 754, section 4.1.

Example:

The following example demonstrates using the System.Math.Round(System.Double)(System.Double) method.

using System;

public class MathRoundExample
{

   public static void Main()
   {

      Double d1 = Math.Round(4.4);
      Double d2 = Math.Round(4.5);
      Double d3 = Math.Round(4.6);
      Console.WriteLine("Math.Round(4.4) returns {0}", d1);
      Console.WriteLine("Math.Round(4.5) returns {0}", d2);
      Console.WriteLine("Math.Round(4.6) returns {0}", d3);

   }

}

The output is

Math.Round(4.4) returns 4

Math.Round(4.5) returns 4

Math.Round(4.6) returns 5

public static System.Int32 Sign(System.Decimal value)

Returns a value indicating the sign of the specified System.Decimal.

Parameter value: A System.Decimal number whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

public static System.Int32 Sign(System.Double value)

Returns a value indicating the sign of the specified System.Double.

Parameter value: A System.Double whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

Throws: : value is equal to System.Double.NaN.

Example:

The following example demonstrates using the System.Math.Sign(System.SByte)(System.Double) method.

using System;

public class MathSignExample
{

   public static void Main()
   {

      Double d1 = Math.Sign(4.4);
      Double d2 = Math.Sign(0.0);
      Double d3 = Math.Sign(-4.5);
      Console.WriteLine("Math.Sign(4.4) returns {0}", d1);
      Console.WriteLine("Math.Sign(0.0) returns {0}", d2);
      Console.WriteLine("Math.Sign(-4.5) returns {0}", d3);

   }

}

The output is

Math.Sign(4.4) returns 1

Math.Sign(0.0) returns 0

Math.Sign(-4.5) returns -1

public static System.Int32 Sign(System.Single value)

Returns a value indicating the sign of the specified System.Single.

Parameter value: A System.Single whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

Throws: : value is equal to System.Single.NaN.

public static System.Int32 Sign(System.Int64 value)

Returns a value indicating the sign of the specified System.Int64.

Parameter value: A System.Int64 whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

public static System.Int32 Sign(System.Int32 value)

Returns a value indicating the sign of the specified System.Int32 .

Parameter value: A System.Int32 whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

public static System.Int32 Sign(System.Int16 value)

Returns a value indicating the sign of the specified System.Int16 .

Parameter value: A System.Int16 whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

public static System.Int32 Sign(System.SByte value)

Returns a value indicating the sign of the specified System.SByte .

Parameter value: A System.SByte whose sign is to be determined.

Returns: A System.Int32 indicating the sign of value.

This method is not CLS-compliant. For a CLS-compliant alternative, use System.Math.Sign(System.SByte)(System.Int16).

public static System.Double Sin(System.Double a)

Returns the sine of the specified System.Double that represents an angle.

Parameter a: A System.Double containing the value of an angle measured in radians.

Returns: A System.Double containing the value of the sine of a. If a is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity, returns System.Double.NaN.

Multiply by /180 to convert degrees to radians.

Example:

The following example demonstrates using the System.Math.Sin(System.Double) method.

using System;

public class MathSinExample
{

   public static void Main()
   {

      Double d1 = Math.Sin(0);
      Double d2 = Math.Sin(Math.PI/2.0);
      Console.WriteLine("Math.Sin(0) returns {0}", d1);
      Console.WriteLine("Math.Sin(Math.PI/2.0) returns {0}", d2);

   }

}

The output is

Math.Sin(0) returns 0

Math.Sin(Math.PI/2.0) returns 1

public static System.Double Sinh(System.Double value)

Returns the hyperbolic sine of the specified System.Double that represents an angle.

Parameter value: A System.Double containing the value of an angle measured in radians.

Returns: A System.Double containing the value of the hyperbolic sine of value. If value is equal to System.Double.NegativeInfinity, System.Double.PositiveInfinity, or System.Double.NaN, returns a System.Double equal to value.

Multiply by /180 to convert degrees to radians.

Example:

The following example demonstrates using the System.Math.Sinh(System.Double) method.

using System;

public class MathSinhExample
{

   public static void Main()
   {

      Double d1 = Math.Sinh(0);
      Double d2 = Math.Sinh(Math.PI);
      Console.WriteLine("Math.Sinh(0) returns {0}", d1);
      Console.WriteLine("Math.Sinh(Math.PI) returns {0}", d2);

   }

}

The output is

Math.Sinh(0) returns 0

Math.Sinh(Math.PI) returns 11.5487393572577

public static System.Double Sqrt(System.Double d)

Returns the square root of the specified System.Double.

Parameter d: A System.Double .

Returns: A System.Double whose value is indicated as follows:

Example:

The following example demonstrates using the System.Math.Sqrt(System.Double) method.

using System;

public class MathSqrtExample
{

   public static void Main()
   {

      Double d1 = Math.Sqrt(16.0);
      Double d2 = Math.Sqrt(0.0);
      Double d3 = Math.Sqrt(-10.0);
      Console.WriteLine("Math.Sqrt(16.0) returns {0}", d1);
      Console.WriteLine("Math.Sqrt(0.0) returns {0}", d2);
      Console.WriteLine("Math.Sqrt(-10.0) returns {0}", d3);

   }

}

The output is

Math.Sqrt(16.0) returns 4

Math.Sqrt(0.0) returns 0

Math.Sqrt(-10.0) returns NaN

public static System.Double Tan(System.Double a)

Returns the tangent of the specified System.Double that represents an angle.

Parameter a: A System.Double that represents an angle measured in radians.

Returns: A System.Double containing the value of the tangent of a. If a is equal to System.Double.NaN, System.Double.NegativeInfinity, or System.Double.PositiveInfinity, returns System.Double.NaN.

Multiply by /180 to convert degrees to radians.

Example:

The following example demonstrates using the System.Math.Tan(System.Double) method.

using System;

public class MathTanExample
{

   public static void Main()
   {

      Double d1 = Math.Tan(0);
      Double d2 = Math.Tan(Math.PI/2.0);
      Console.WriteLine("Math.Tan(0) returns {0}", d1);
      Console.WriteLine("Math.Tan(Math.PI/2.0) returns {0}", d2);

   }

}

The output is

Math.Tan(0) returns 0

Math.Tan(Math.PI/2.0) returns 1.63317787283838E+16

public static System.Double Tanh(System.Double value)

Returns the hyperbolic tangent of the specified System.Double that represents an angle.

Parameter value: A System.Double that represents an angle measured in radians.

Returns: A System.Double containing the value of the hyperbolic tangent of value. If value is equal to System.Double.NegativeInfinity, returns -1. If value is equal to System.Double.PositiveInfinity, returns 1. If value is equal to System.Double.NaN, returns System.Double.NaN.

Multiply by /180 to convert degrees to radians.

Example:

The following example demonstrates using the System.Math.Tanh(System.Double) method.

using System;

public class MathTanhExample
{

   public static void Main()
   {

      Double d1 = Math.Tanh(0);
      Double d2 = Math.Tanh(Math.PI);
      Console.WriteLine("Math.Tanh(0) returns {0}", d1);
      Console.WriteLine("Math.Tanh(Math.PI) returns {0}", d2);

   }

}

The output is

Math.Tanh(0) returns 0

Math.Tanh(Math.PI) returns 0.99627207622075

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

<doc:param name="obj"/>
is Point , the check would return true in cases where obj is an instance of a subclass of Point , even though obj and the current instance are not of the same runtime type. Having verified that both objects are of the same type, the method casts obj to type Point and returns the result of comparing the instance variables of the two objects.

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