您的位置:首页 > 编程语言 > C#

c# 学习之2(值类型与引用类型的区别)

2017-03-17 16:08 417 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace value
{
class Program
{
static void Main(string[] args)
{
ReferenceAndValue.Demonstration();
for (int i = 0; i < 1000000000; i++) ;
}
}
public class stamp
{
public string Name { get; set; }
public int age { get; set; }

}
public static class ReferenceAndValue
{
public static void Demonstration()
{
stamp Stamp_1 = new stamp { Name = "sdeg", age = 23 };
stamp Stamp_2 = new stamp { Name = "xxsdegs", age = 25 };

stamp guru1, guru2;
guru2 = Stamp_2;
//值类型
int age = Stamp_1.age;
Stamp_1.age = 33;
Console.WriteLine(" Stamp_1.age={0}", Stamp_1.age);
Console.WriteLine(" age={0}", age);
//引用类型
stamp guru = Stamp_2;
Stamp_2.age = 35;
Console.WriteLine(" Stamp_2.age={0}", Stamp_2.age);
Console.WriteLine(" guru.age={0}", guru.age);
}

}
}

////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication_str
{
    class ks
    {
        public int obj;

    }
    class Program
    {
        static void Main(string[] args)
        {
            //值类型
            int i = new int();
            i = 5;
            int j = i;
            Console.WriteLine("first i,j**** {0},{1}", i,j);
            i = 8;
            Console.WriteLine("second i,j**** {0},{1}", i, j);

            int s, k;
            s = 9;
            k = s;
            Console.WriteLine("first s,k**** {0},{1}", s, k);
            s = 10;
            Console.WriteLine("second s,k**** {0},{1}", s, k);

            //引用类型
            ks s1, s2,s3;
            s1 = new ks();
            s1.obj = 1;
            s2 = s1;
            Console.WriteLine("first s1,s2**** {0},{1}", s1.obj, s2.obj);
            s1.obj = 2;
            Console.WriteLine("second s1,s2**** {0},{1}", s1.obj, s2.obj);

            s3 = new ks();
            s3.obj = 4;
            s2 = s3;
            Console.WriteLine("three s1 ,s2,s3**** {0},{1},{2}", s1.obj, s2.obj, s3.obj);

            s1 = s3;
            Console.WriteLine("fore s1,s2 ,s3**** {0},{1},{2}", s1.obj, s2.obj, s3.obj);
            for (int q = 0; q < 10000000000; q++) ;
        }
    }
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: