POINTS

struct为可以包含数据和函数的值类型

struct为值类型所以不需要堆(heap)而是在栈(stack)上分配空间

struct将数据直接存在struct中,而class只存引用类型的指针

struct适用于小的数据结构

struct会影响性能

struct可以使用new操作可以调用构造器,但是不会在heap上分配内存

struct的构造器只返回struct的值本身(通常分配在stack上)

使用class时,多个变量可以引用同一个对象

使用sturct每个变量都保存自己的数据拷贝,不会相互影响

struct不支持继承,sturct继承自object类型

DEMO
  1. class Program
  2. {
  3. class PointClass
  4. {
  5. public int x;
  6. public int y;
  7. public PointClass(int x, int y)
  8. {
  9. this.x = x;
  10. this.y = y;
  11. }
  12. }
  13. struct PointStruct
  14. {
  15. public int x;
  16. public int y;
  17. public PointStruct(int x, int y)
  18. {
  19. this.x = x;
  20. this.y = y;
  21. }
  22. }
  23. static void Main(string[] args)
  24. {
  25. PointStruct pointStruct = new PointStruct(, );
  26. Console.WriteLine("Initial struct values are {0},{1}", pointStruct.x, pointStruct.y);
  27. ModifyStructPoint(pointStruct);
  28. Console.WriteLine("After ModifyStructPoint, struct values are {0},{1}", pointStruct.x, pointStruct.y);
  29.  
  30. Console.WriteLine();
  31. PointClass pointClass = new PointClass(, );
  32. Console.WriteLine("Initial Class values are {0},{1}", pointClass.x, pointClass.y);
  33. ModifyClassPoint(pointClass);
  34. Console.WriteLine("After ModifyClassPoint, class values are {0},{1}", pointClass.x, pointClass.y);
  35. Console.ReadLine();
  36. }
  37.  
  38. private static void ModifyStructPoint(PointStruct pointStruct)
  39. {
  40. pointStruct.x = ;
  41. pointStruct.y = ;
  42. Console.WriteLine("Modified Valuesare {0},{1}", pointStruct.x, pointStruct.y);
  43.  
  44. }
  45.  
  46. private static void ModifyClassPoint(PointClass pointClass)
  47. {
  48. pointClass.x = ;
  49. pointClass.y = ;
  50. Console.WriteLine("Modified Valuesare {0},{1}", pointClass.x, pointClass.y);
  51. }
  52. }

[c# 20问] 1. 何时使用class与struct的更多相关文章

  1. powershell玩转xml之20问

    powershell玩转xml之20问 powershell 传教士 原创文章 2014-01-30,2015-10-27改 允许转载,但必须保留名字和出处,否则追究法律责任 问:xml文件编码情况如 ...

  2. Java高质量20问

    问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...

  3. JDK集合面试20问

    1. HashMap的内部实现原理是什么? HashMap内部实现原理是数组+链表,通过散列算法将key值散列到数组中,如果到相同的位置,则通过拉链法解决散列冲突.在JDK8中新增了红黑树结构,当Ha ...

  4. Dubbo面试20问!这些题你都遇到过吗?

    作者:Dean Wang https://deanwang1943.github.io/bugs/2018/10/05/面试/饿了么/dubbo 面试题/ 1.dubbo是什么 dubbo是一个分布式 ...

  5. [c# 20问] 4.Console应用获取执行路径

    一行代码可以搞定了~ static void GetAppPath() { string path = System.Reflection.Assembly.GetExecutingAssembly( ...

  6. [c# 20问] 3.String和string的区别

    POINTS string类型为继承自object的sealed类. string类实例用来存储Unicode字符串. string关键字是System.String类的别名,因此既可以定义strin ...

  7. [c# 20问] 2.如何转换XML文件

    添加System.Xml引用 使用XmlReader转换字符串 DEMO #region Parse Xml private static void ParseXml(string xmlString ...

  8. Python夺命20问

    1.请观看下列代码并回答问题: import collections from random import choice Card = collection.namedtuple('Card', [' ...

  9. sql注入之你问我答小知识

    /*每日更新,珍惜少年时博客*/ 1.问:为啥order by 是排序.而在注入当中后面加的却不是字段而是数字捏? 答:第N个字段嘛.order by 5就是第五个字段,如果5存在,6不存在.就说明只 ...

随机推荐

  1. FilenameFilter 文件名过滤

    public static final FilenameFilter JSON_CONFIG_FILE_FILTER = new FilenameFilter() {         @Overrid ...

  2. bat文件

    bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cm ...

  3. js将UTC时间转化为当地时区时间(UTC转GMT)

    我们在进行网站开发的时候有可能会涉及到国外的用户或者用户身在国外,这时就会存在时差问题,比如说我们在中国的时间是08:00,但是此时韩国的时间是09:00,如果在网页上需要进行相关显示的话就会出现问题 ...

  4. vs2017运行时修改代码Changes are not allowed while code is running.

    vs2017代码运行时不允许进行更改 工具->选项->调试->常规->启用编辑并继续不选择“启用编辑并继续”,这样就可以在调试时修改cs代码了.

  5. Spring Boot使用@Scheduled定时器任务

      摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在Application中设置启用定时任务功能@EnableS ...

  6. 551. Student Attendance Record I + Student Attendance Record II

    ▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...

  7. SQL Server实时同步更新远程数据库遇到的问题

    工作中遇到这样的情况,需要在更新表TableA(位于服务器ServerA 172.16.8.100中的库DatabaseA)同时更新TableB(位于服务器ServerB 172.16.8.101中的 ...

  8. 个人tools封装

    /** * jQuery 扩展 */(function ($) { $.fn.extend({ /** * 目标为任意对象元素 * 同时绑定单击和双击事件 */ bindClick: function ...

  9. 文本操作 $(..).text() $(..).html() $(..).val()最后一种主要用于input

    文本操作: $(..).text() # 获取文本内容 $(..).text('<a>1</a>') # 设置文本内容 $(..).html() $(..).html('< ...

  10. Eureka 客户端 配置Eureka 爬坑

    配置客户端 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.servic ...