[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不存在.就说明只 ...
随机推荐
- java web 程序---购物车项目内容:
1.项目介绍 典型电子商务系统(在线购物平台).模拟了当当系统部分功能.2.功能需求 1)用户管理模块(3天)user 实现登录.注册功能 2)产品浏览模块(2天)ma ...
- Train-Alypay-Cloud:分布式微服务中间件sofa 开发培训(第二次)
ylbtech-Train-Alypay-Cloud:分布式微服务中间件sofa 开发培训(第二次) 1.返回顶部 1. 这是本次培训的内容,望各位提前配好环境.工具.2.6-2.7 我们在环球金融8 ...
- Introduction to Spring Data MongoDB
Introduction to Spring Data MongoDB I just announced the new Spring 5 modules in REST With Spring: & ...
- 并发包学习(一)-Atomic包小记
此篇是J.U.C学习的第一篇Atomic包相关的内容,希望此篇总结能对自己的基础有所提升.本文总结来源自<Java并发编程的艺术>第七章并配以自己的实践理解.如有错误还请指正. 一.案例分 ...
- 详解jenkins几个有用的插件如何使用(emma,findbugs)
原文:http://myeyeofjava.iteye.com/blog/1765552 findbugs使用方式: 目的:进行代码走查的自动化,能够提示垃圾代码或者提供代码优化的建议 1.首先下载f ...
- MVVMLight介绍以及在项目中的使用
http://www.des8.me/detail-1822826.html 一.MVVM 和 MVVMLight介绍 MVVM是Model-View-ViewModel的简写.类似于目前比较流行的M ...
- 7_bootstap之综合案例
13.综合案例 13.1.案例需求 要求:页面顶部的三部分在PC屏幕上显示为一行,在移动设备屏幕上显示为一部分一行: 导航条在大屏幕展示全部内容,在移动设备上需要将内容能够折叠/展开: 用户名/密码/ ...
- ganglia-Monitor
- javascript的中的new
考察 ECMAScript 语言规范中 new 运算符的定义: The new Operator The production NewExpression : new NewExpression is ...
- c# 数据集调试工具插件
DataSetSpySetup,调试期查看dataset数据集的记录内容, Debug DataSet