结构体和类的区别

结构体式值类型,类是引用类型。这个很像C语言里面的那个指针和数值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class MyClass
    {
        public int val;

    }

    struct myStuct
    {

        public int val;
    }
    public class Program : ReadClass
    {
        static void Main(string[] args)
        {
            MyClass objectA = new MyClass();
            MyClass objectB = objectA;
            myStuct stuctA = new myStuct();
            myStuct stuctB=stuctA;
            objectA.val = 10;
            objectB.val = 20;
            stuctA.val = 30;
            stuctB.val = 40;
            Console.WriteLine("objectA.val = {0};", objectA.val);
            Console.WriteLine("objectB.val = {0};", objectB.val);
            Console.WriteLine("stuctA.val = {0};", stuctA.val);
            Console.WriteLine("stuctA.val = {0};", stuctB.val);
            Console.ReadKey();
        }
     
    }
}

输出的结果为

stuct是开辟了两个数值型的空间存放书,class是开辟了两个存放地址的空间这两个地址都指向同一个地方

posted @ 2011-09-01 15:40  haiziguo  阅读(350)  评论(0编辑  收藏  举报