最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
c#中结构与类的区别
时间:2022-07-02 11:11:25 编辑:袖梨 来源:一聚教程网
目录
类与结构的实例比较
类与结构的差别
如何选择结构还是类
一.类与结构的示例比较:
结构示例:
public struct Person
{
string Name;
int height;
int weight
public bool overWeight()
{
//implement something
}
}
类示例:
public class TestTime
{
int hours;
int minutes;
int seconds;
public void passtime()
{
//implementation of behavior
}
}
调用过程:
public class Test
{
public static ovid Main
{
Person Myperson=new Person //声明结构
TestTime Mytime=New TestTime //声明类
}
}
从上面的例子中我们可以看到,类的声明和结构的声明非常类似,只是限定符后面是 struct 还是 class 的区别,而且使用时,定义新的结构和定义新的类的方法也非常类似。那么类和结构的具体区别是什么呢?
二 .类与结构的差别
1. 值类型与引用类型
结构是值类型:值类型在堆栈上分配地址,所有的基类型都是结构类型,例如:int 对应System.int32 结构,string 对应 system.string 结构 ,通过使用结构可以创建更多的值类型
类是引用类型:引用类型在堆上分配地址
相关文章
- 重返未来1999迷思海450-3打法攻略 08-25
- 金铲铲之战s15超级战队阵容玩法教学 08-25
- 胜利女神新的希望梅里海湾女神介绍一览 08-25
- 偃武甘宁阵法搭配推荐 08-25
- 金铲铲之战s15延长手臂船长阵容运营思路 08-25
- 洛克王国世界鸭吉吉怎么配招-鸭吉吉技能搭配推荐 08-25