C#.NET中的结构类型(结构体与类的区别)
/*
* Created by SharpDevelop.
* User: noo
* Date: 2009-8-15
* Time: 17:42
*
* 结构类型
*/
using System ;
class myClass//类
{
public int val;
}
struct myStruct//结构体
{
public int val;
}
class Test
{
static void Main()
{
myClass a=new myClass ();
myClass b=a;//类传递的是地址,等于是a把它的指针地址给了b,所以它们指向的是同一块地址指针,只是名字不同而已。引用类型
a.val =10;
b.val =20;
Console.WriteLine ("a.val的值为:"+a.val);//20
Console.WriteLine ("b.val的值为:"+b.val);//20
myStruct aa=new myStruct ();
myStruct bb=aa;//结构体传递的是值。即结构是值类型
aa.val =10;
bb.val =20;
Console.WriteLine ("aa.val的值为:"+aa.val);//10
Console.WriteLine ("bb.val的值为:"+bb.val);//20
}
}
* Created by SharpDevelop.
* User: noo
* Date: 2009-8-15
* Time: 17:42
*
* 结构类型
*/
using System ;
class myClass//类
{
public int val;
}
struct myStruct//结构体
{
public int val;
}
class Test
{
static void Main()
{
myClass a=new myClass ();
myClass b=a;//类传递的是地址,等于是a把它的指针地址给了b,所以它们指向的是同一块地址指针,只是名字不同而已。引用类型
a.val =10;
b.val =20;
Console.WriteLine ("a.val的值为:"+a.val);//20
Console.WriteLine ("b.val的值为:"+b.val);//20
myStruct aa=new myStruct ();
myStruct bb=aa;//结构体传递的是值。即结构是值类型
aa.val =10;
bb.val =20;
Console.WriteLine ("aa.val的值为:"+aa.val);//10
Console.WriteLine ("bb.val的值为:"+bb.val);//20
}
}