c#中struct和class的区别 z
1.struct 是值类型,class是对象类型
2.struct 不能被继承,class可以被继承
3.struct 默认的访问权限是public,而class默认的访问权限是private.
4..struct的new和class的new是不同的。struct的new就是执行一下构造函数创建一个新实例再对所 有的字段进行Copy。而class则是在堆上分配一块内存然后再执行构造函数,struct的内存并不是在new的时候分配的,而是在定义的时候分配, 换句话话说struct是在线程栈中创建,其定义时就已经被初始化,而class必须用new在托管堆中开辟空间。 因此struct与class在作为 参数传值过程中一个,struct是重新产生一个副本,而class则是传递的指针引用
举例说明:
class NewClass {
public int x;
}
struct NewStruct {
public int x;
}
class TestClass {
public static void structMethod(NewStruct s)
{
s.x = 5;
}
public static void classMethod(NewClass c)
{
c.x = 5;
}
public static void Main() {
NewStruct a = new NewStruct();//栈上分配
NewClass b = new NewClass();//堆上分配
a.x = 1;
b.x = 1;
structMethod(a);//产生了一个副本
classMethod(b);//指针一直只想堆中同一块内存
Console.WriteLine("a.x = {0}", a.x);//1
Console.WriteLine("b.x = {0}", b.x);//5
}
}
所以,如果想要让a.x中也产生一个a.x=5,怎要在方法应为structMethod(ref NewStruct s) ,在此设计到引用传参。
c#中struct和class的区别 z的更多相关文章
- C#中struct和class的区别详解
本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...
- C#中struct和class的区别详解 (转载)
本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...
- C#中Struct与Class的区别
class和struct最本质的区别是class是引用类型,而struct是值类型,它们在内存中的分配情况有所区别. 什么是class? class(类)是面向对象编程的基本概念,是一种自定义数据结构 ...
- 【zz】C++中struct与class的区别
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- C++中struct和class的区别 [转]
一. C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能. struct能包含成员函数吗? 能! struct能继承吗? ...
- C++ 中 struct和class 的区别
来自:http://hi.baidu.com/pengxiangbobin19890125/blog/item/b05586eee77300212df53411.html C++ prime 中 ...
- c#中struct和class的区别 详细[转]
转自:http://blog.csdn.net/justlovepro/archive/2007/11/02/1863734.aspx 有这么几点不同: 1.struct 是值类型,class是对象类 ...
- C#中struct与class的区别详解
转自:http://blog.csdn.net/justlovepro/archive/2007/11/02/1863734.aspx 有这么几点不同: 1.struct 是值类型,class是对象类 ...
- C++中struct 和 class的区别
首先,C++中类的定义,从狭义上理解,就是我们使用的class类型.从广义上,类就是定义了一个新的类型和新的作用域,它具有成员函数和成员数据. 而对广义类定义的实现分为两种,一种是使用struct实现 ...
随机推荐
- Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...
- [HTTP] HTTP Verb
HEAD: HEAD / HTTP/1.1 nc.exmaple.com HEAD is a interesting method, it allow you to get a header of f ...
- [Javascript] Drawing Styles on HTML5 Canvas
window.onload = function() { var canvas = document.getElementById("canvas"), context = can ...
- [React] React Fundamentals: Owner Ownee Relationship
The owner-ownee relationship is used to designate a parent-child relationship with React components ...
- mysqldump 备份原理8
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; http://www.cnblogs.com/lyhabc/p/38 ...
- xcode 高亮
Cmd+E, Cmd+F and Cmd+G combo is usefull. Depending on why you want to do this, edit all in scope (Ct ...
- Java基础知识强化之集合框架笔记65:Map集合之集合多层嵌套的数据分析
1. 为了更符合要求: 这次的数据就看成是学生对象. 传智播客 bj 北京校区 jc 基础班 林青霞 27 风清扬 30 jy 就业班 赵雅芝 28 武鑫 29 sh 上海 ...
- AIDL通信原理
AIDL (Android Interface Definition Language),通过定义通信接口来实现进程间通信.这是Google提供的一种在安卓应用进程间通信的工具.所以要了解AIDL的通 ...
- 深入理解计算机系统第二版习题解答CSAPP 2.8
给出位微量的布尔去处的求值结果. 运算 结果 a 0110 1001 b 0101 0101 ~a 1001 0110 ~b 1010 1010 a&b 0100 0001 a ...
- rndc: connect failed: 127.0.0.1#953: connection refused
[root@localhost sbin]# ./named -v bind 9.5.1-p3-v3.0.9 问题现象: [root@localhost sbin]# ./rndc flush -p ...