Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Base Class สำหรับ Data Types ทั้งหมดใน C#.NET

Object เป็น Base Class สำหรับ Data Types ทั้งหมดใน C# Object Type คือ Base Class สุดท้ายใน CTS (Common Type System) object เป็น Alias ของ System.Object

Value Type -> Object Type = Boxing
Object Type -> Value Type = Unboxing

ตัวอย่าง

using System;
using System.IO;

namespace Demo
{
    class objectClass { public int x = 56; }
    class MyApplication
    {
        static void Main()
        {
            object obj;
            obj = 96; // Boxing
            Console.WriteLine(obj);
            obj = new objectClass();
            objectClass newRef;
            newRef = (objectClass)obj; // Unboxing
            Console.WriteLine(newRef.x);
        }
    }
}