zoukankan      html  css  js  c++  java
  • c#结构体总结

    https://www.cnblogs.com/jhlxyp/articles/4322964.html

    一、结构和类的区别

    1、结构的级别和类一致,写在命名空间下面,可以定义字段、属性、方法、构造方法也可以通过关键字new创建对象。

    2、结构中的字段不能赋初始值。

    3、无参数的构造函数无论如何C#编译器都会自动生成,所以不能为结构定义一个无参构造函数。

    4、在构造函数中,必须给结构体的所有字段赋值。

    5、在构造函数中,为属性赋值,不认为是对字段赋值,因为属性不一定是去操作字段。

    6、结构是值类型,在传递结构变量的时候,会将结构对象里的每一个字段复制一份拷贝到新的结构变量的字段中。

    7、不能定义自动属性,因为字段属性会生成一个字段,而这个字段必须要求在构造函数中,但我们不知道这个字段叫什么名字。

    8、声明结构体对象,可以不使用new关键字,但是这个时候,结构体对象的字段没有初始值,因为没有调用构造函数,构造函数中必须为字段赋值,所以,通过new关键字创建结构体对象,这个对象的字段就有默认值。

    9、栈的访问速度快,但空间小,堆的访问速度慢,但空间大,当我们要表示一个轻量级的对象的时候,就定义为结构,以提高速度,根据传至的影响来选择,希望传引用,则定义为类,传拷贝,则定义为结构。

    二、Demo

    复制代码
    复制代码
     1 struct Point
     2     {
     3         public Program p;
     4         private int x;
     5 
     6         public int X
     7         {
     8             get { return x; }
     9             set { x = value; }
    10         }
    11         private int y;
    12 
    13         public int Y
    14         {
    15             get { return y; }
    16             set { y = value; }
    17         }
    18         public void Show()
    19         {
    20             Console.Write("X={0},Y={1}", this.X, this.Y);
    21         }
    22         public Point(int x,int y)
    23         {
    24             this.x = x;
    25             this.y = y;
    26             this.p = null;
    27         }
    28         public Point(int x)
    29         {
    30             this.x = x;
    31             this.y = 11;
    32             this.p = null;
    33         }
    34         public Point(int x, int y, Program p)
    35         {
    36             this.x = x;
    37             this.y = y;
    38             this.p = p;
    39         }
    40     }
    41     class Program
    42     {
    43         public string Name { get; set; }
    44         static void Main(string[] args)
    45         {
    46             //Point p = new Point();
    47             //p.X = 120;
    48             //p.Y = 100;
    49             //Point p1 = p;
    50             //p1.X = 190;
    51             //Console.WriteLine(p.X);
    52 
    53             //Point p;
    54             //p.X = 12;//不赋值就会报错
    55             //Console.WriteLine(p.X);
    56             //Point p1 = new Point();
    57             //Console.WriteLine(p1.X);//此处不赋值不会报错,原因见区别8
    58 
    59             Program p = new Program() { Name="小花"};
    60             Point point1 = new Point(10, 10, p);
    61             Point point2 = point1;
    62             point2.p.Name = "小明";
    63             Console.WriteLine(point1.p.Name);//结果为小明,分析见下图
    64         }
    65     }
    复制代码
    复制代码

  • 相关阅读:
    Html 回顾
    Parallel 并行编程
    Task---常用的多线程(基于多线程线程)
    Threadpool 可以对线程加以管理的封装
    AsyncThreads---异步多线程
    Abstract 封装,继承,多态
    IO&&Serize 利用线程Thread.Sleep实现"自动输出"
    Ling && Lambda
    Delegate&&Event
    Delegate &&Lambda
  • 原文地址:https://www.cnblogs.com/eve612/p/13840643.html
Copyright © 2011-2022 走看看