C#值类型和引用类型2

c#所有的类型都继承自System.Object:

  1>值类型继承自System.ValueType;

namespace System
{
    // Summary:
    //     Provides the base class for value types.
    [Serializable]
    [ComVisible(true)]
    public abstract class ValueType
    {
        // Summary:
        //     Initializes a new instance of the System.ValueType class.
        protected ValueType();

        // Summary:
        //     Indicates whether this instance and a specified object are equal.
        //
        // Parameters:
        //   obj:
        //     Another object to compare to.
        //
        // Returns:
        //     true if obj and this instance are the same type and represent the same value;
        //     otherwise, false.
        public override bool Equals(object obj);
        //
        // Summary:
        //     Returns the hash code for this instance.
        //
        // Returns:
        //     A 32-bit signed integer that is the hash code for this instance.
        [SecuritySafeCritical]
        public override int GetHashCode();
        //
        // Summary:
        //     Returns the fully qualified type name of this instance.
        //
        // Returns:
        //     A System.String containing a fully qualified type name.
        public override string ToString();
    }
}

值类型重写了ToString(),GetHashCode(),Equals(object o);

  2>引用类型继承自System.Object;

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 image 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 image 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 image 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 image 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();
    }
}

引用类型有3个Virtual虚方法:Equals(object obj), GetHashCode(),ToString();

             2个Static静态方法: Equals(object objA, object objB),ReferenceEquals(object objA, object objB);

             1个公共方法:GetType();

             1个保护方法:MemberwiseClone();

3>值类型也可以为null;

namespace Nullable
{
    class Program
    {
        static void Main(string[] args)
        {
            //有时候值类型是空是很有用的,泛型使用System.Nullanle<T>类型提供了使值类型为空的一种方式;
            System.Nullable<int> i = null;

            //也可以这样写,是System.Nullable<int>的缩写;
            int? j = null;

            Console.WriteLine(i);

            Console.ReadLine();
        }
    }
}