program.cs
来自「csharp课本的源代码」· CS 代码 · 共 51 行
CS
51 行
using System;
using System.Collections.Generic;
using System.Text;
namespace TypeExample
{
//引用类型的RefTypeRectangle类的声明
class RefTypeRectangle
{
public int Width;
public int Heigth;
}
//值类型的ValTypeRectangle结构的声明
struct ValTypeRetangle
{
public int Width;
public int Heigth;
}
class Program
{
public static void Main()
{
//rect1和rect2均为引用类型的变量,且指向同一对象
RefTypeRectangle rect1 = new RefTypeRectangle();
rect1.Width = 10;
rect1.Heigth = 15;
RefTypeRectangle rect2 = rect1;
Console.WriteLine("Dimensions of rect2 are" + rect2.Width + "x" + rect2.Heigth);
Console.WriteLine("Changing dimensions of rect1...");
//通过rect1更改对象的值一,此时由rect2得到的值即更改后的结果
rect1.Width = 20;
rect1.Heigth = 25;
Console.WriteLine("Dimensions of rect2 now are" + rect2.Width + "x" + rect2.Heigth);
//rect3和rect4均为值类型的变量,且初始值相同
ValTypeRetangle rect3 = new ValTypeRetangle();
rect3.Width = 10;
rect3.Heigth = 15;
ValTypeRetangle rect4 = rect3;
Console.WriteLine("Dimensions of rect4 are" + rect4.Width + "x" + rect4.Heigth);
Console.WriteLine("Changing dimensions of rect3...");
//更改rect3的值,rect4的值不受影响
rect3.Width = 20;
rect3.Heigth = 25;
Console.WriteLine("Dimensions of rect4 now are" + rect4.Width + "x" + rect4.Heigth);
//按回车键结束
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?