[c# 20问] 1. 何时使用class与struct
POINTS
struct为可以包含数据和函数的值类型
struct为值类型所以不需要堆(heap)而是在栈(stack)上分配空间
struct将数据直接存在struct中,而class只存引用类型的指针
struct适用于小的数据结构
struct会影响性能
struct可以使用new操作可以调用构造器,但是不会在heap上分配内存
struct的构造器只返回struct的值本身(通常分配在stack上)
使用class时,多个变量可以引用同一个对象
使用sturct每个变量都保存自己的数据拷贝,不会相互影响
struct不支持继承,sturct继承自object类型
DEMO
class Program
{
class PointClass
{
public int x;
public int y;
public PointClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
struct PointStruct
{
public int x;
public int y;
public PointStruct(int x, int y)
{
this.x = x;
this.y = y;
}
}
static void Main(string[] args)
{
PointStruct pointStruct = new PointStruct(, );
Console.WriteLine("Initial struct values are {0},{1}", pointStruct.x, pointStruct.y);
ModifyStructPoint(pointStruct);
Console.WriteLine("After ModifyStructPoint, struct values are {0},{1}", pointStruct.x, pointStruct.y); Console.WriteLine();
PointClass pointClass = new PointClass(, );
Console.WriteLine("Initial Class values are {0},{1}", pointClass.x, pointClass.y);
ModifyClassPoint(pointClass);
Console.WriteLine("After ModifyClassPoint, class values are {0},{1}", pointClass.x, pointClass.y);
Console.ReadLine();
} private static void ModifyStructPoint(PointStruct pointStruct)
{
pointStruct.x = ;
pointStruct.y = ;
Console.WriteLine("Modified Valuesare {0},{1}", pointStruct.x, pointStruct.y); } private static void ModifyClassPoint(PointClass pointClass)
{
pointClass.x = ;
pointClass.y = ;
Console.WriteLine("Modified Valuesare {0},{1}", pointClass.x, pointClass.y);
}
}
[c# 20问] 1. 何时使用class与struct的更多相关文章
- powershell玩转xml之20问
		
powershell玩转xml之20问 powershell 传教士 原创文章 2014-01-30,2015-10-27改 允许转载,但必须保留名字和出处,否则追究法律责任 问:xml文件编码情况如 ...
 - Java高质量20问
		
问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...
 - JDK集合面试20问
		
1. HashMap的内部实现原理是什么? HashMap内部实现原理是数组+链表,通过散列算法将key值散列到数组中,如果到相同的位置,则通过拉链法解决散列冲突.在JDK8中新增了红黑树结构,当Ha ...
 - Dubbo面试20问!这些题你都遇到过吗?
		
作者:Dean Wang https://deanwang1943.github.io/bugs/2018/10/05/面试/饿了么/dubbo 面试题/ 1.dubbo是什么 dubbo是一个分布式 ...
 - [c# 20问] 4.Console应用获取执行路径
		
一行代码可以搞定了~ static void GetAppPath() { string path = System.Reflection.Assembly.GetExecutingAssembly( ...
 - [c# 20问] 3.String和string的区别
		
POINTS string类型为继承自object的sealed类. string类实例用来存储Unicode字符串. string关键字是System.String类的别名,因此既可以定义strin ...
 - [c# 20问] 2.如何转换XML文件
		
添加System.Xml引用 使用XmlReader转换字符串 DEMO #region Parse Xml private static void ParseXml(string xmlString ...
 - Python夺命20问
		
1.请观看下列代码并回答问题: import collections from random import choice Card = collection.namedtuple('Card', [' ...
 - sql注入之你问我答小知识
		
/*每日更新,珍惜少年时博客*/ 1.问:为啥order by 是排序.而在注入当中后面加的却不是字段而是数字捏? 答:第N个字段嘛.order by 5就是第五个字段,如果5存在,6不存在.就说明只 ...
 
随机推荐
- Bootstrap-Plugin:弹出框(Popover)插件
			
ylbtech-Bootstrap-Plugin:弹出框(Popover)插件 1.返回顶部 1. Bootstrap 弹出框(Popover)插件 弹出框(Popover)与工具提示(Tooltip ...
 - [html][javascript] 正则匹配示例
			
var str="akdlfaklhello 1234klfd1441ksalfd9000kals8998j2345fd;lsa"; var reg = new RegExp(/( ...
 - eclipse 创建dynamic web project不能运行
			
按照李刚<轻量级java ee企业应用实战>第三版,第二章的配置,发现eclipse 创建dynamic web project不能运行,原来作者第二章时还不是在eclipse中开发的,只 ...
 - 7_bootstap之综合案例
			
13.综合案例 13.1.案例需求 要求:页面顶部的三部分在PC屏幕上显示为一行,在移动设备屏幕上显示为一部分一行: 导航条在大屏幕展示全部内容,在移动设备上需要将内容能够折叠/展开: 用户名/密码/ ...
 - Mysql  索引概论
			
Mysql性能下降原因 JOIN连接过多 ,索引失效(单值,复合), 查询SQL过水, explian 语法分析SQL性能 https://blog.csdn.net/b1303110335/arti ...
 - 13 MySQL--存储过程
			
1.存储过程的介绍 对于存储过程,可以接收参数,其参数有三类: in 仅用于传入参数用 out 仅用于返回值用 inout 既可以传入又可以当作返回值 存储过程包含了一系列可执行的sql语句,存储过程 ...
 - socket & pipe note
			
[socket & pipe note] 1.socket类型 2.大小端 3.socketpair 如何创建全双工管道? 直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢? ...
 - Spring中的注解配置-注入bean
			
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
 - c++程序的多文件组织
			
当程序规模变大后,一个程序用多个文件组织,便于组织生产.这样,不必每次都重复对所有代码进行编译,而只需编译一次即可.把编译后所形成的目标文件保存起来,以后在需要时把它调出来直接与程序的目标文件相连接即 ...
 - iOS对HTTPS证书链的验证原理
			
今天看到所在的某个开发群问https原理,之前做HTTPS ,下面简单说下原理.希望能帮助你理解. HTTPS从最终的数据解析的角度,与HTTP相同.HTTPS将HTTP协议数据包放到SSL/TSL层 ...