object
类型在 .NET Framework 中是 Object 的别名。 在 C# 的统一类型系统中,所有类型(预定义类型、用户定义类型、引用类型和值类型)都是直接或间接从 Object 继承的。 可以将任何类型的值赋给 object
类型的变量。 将值类型的变量转换为对象的过程称为“装箱”。 将对象类型的变量转换为值类型的过程称为“取消装箱”。 有关更多信息,请参见装箱和取消装箱。
System.Object
所有类、结构、枚举和委托
****************************
using System.Runtime;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
namespace System
{
// Summary:
// Supports all classes in the .NET Framework class hierarchy and provides low-level
// services to derived classes. This is the ultimate base class of all classes
// in the .NET Framework; it is the root of the type hierarchy.
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Object
{
// Summary:
// Initializes a new instance of the System.Object class.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public Object();
// Summary:
// Determines whether the specified System.Object is equal to the current System.Object.
//
// Parameters:
// obj:
// The System.Object to compare with the current System.Object.
//
// Returns:
// true if the specified System.Object is equal to the current System.Object;
// otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen p_w_picpath boundaries")]
public virtual bool Equals(object obj);
//
// Summary:
// Determines whether the specified System.Object instances are considered equal.
//
// Parameters:
// objA:
// The first System.Object to compare.
//
// objB:
// The second System.Object to compare.
//
// Returns:
// true if the objects are considered equal; otherwise, false.
[TargetedPatchingOptOut("Performance critical to inline across NGen p_w_picpath boundaries")]
public static bool Equals(object objA, object objB);
//
// Summary:
// Serves as a hash function for a particular type.
//
// Returns:
// A hash code for the current System.Object.
[TargetedPatchingOptOut("Performance critical to inline across NGen p_w_picpath boundaries")]
public virtual int GetHashCode();
//
// Summary:
// Gets the System.Type of the current instance.
//
// Returns:
// The System.Type instance that represents the exact runtime type of the current
// instance.
[SecuritySafeCritical]
public Type GetType();
//
// Summary:
// Creates a shallow copy of the current System.Object.
//
// Returns:
// A shallow copy of the current System.Object.
[SecuritySafeCritical]
protected object MemberwiseClone();
//
// Summary:
// Determines whether the specified System.Object instances are the same instance.
//
// Parameters:
// objA:
// The first System.Object to compare.
//
// objB:
// The second System.Object to compare.
//
// Returns:
// true if objA is the same instance as objB or if both are null references;
// otherwise, false.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[TargetedPatchingOptOut("Performance critical to inline across NGen p_w_picpath boundaries")]
public static bool ReferenceEquals(object objA, object objB);
//
// Summary:
// Returns a System.String that represents the current System.Object.
//
// Returns:
// A System.String that represents the current System.Object.
public virtual string ToString();
}
}
****************************
下面的示例演示了 object
类型的变量如何接受任何数据类型的值,以及 object
类型的变量如何在 .NET Framework 中使用 Object 的方法。
class ObjectTest { public int i = 10; }class MainClass2 { static void Main() { object a; a = 1; // an example of boxing Console.WriteLine(a); Console.WriteLine(a.GetType()); Console.WriteLine(a.ToString()); a = new ObjectTest(); ObjectTest classRef; classRef = (ObjectTest)a; Console.WriteLine(classRef.i); } }/* Output 1 System.Int32 1 * 10 */
备注:摘自https://msdn.microsoft.com/zh-cn/library/9kkx3h3c.aspx