• c#中,struct和class的区别


    1、struct不允许显示声明其无参数构造函数,这不同于class
    2、struct不允许声明时,初始化其数据成员值
    3、struct作为参数传递时,可考虑使用ref,以优化性能:因为是值类型(但要注意其值的改变)
    4、struct无继承,但其本身继承自System.ValueType ----> System.Object
    5、struct可看作是缩小的class,适宜小数据成员时使用
    6、理解如下代码:
    class Class1
        
    {
        [STAThread]
        
    static void Main(string[] args)
        
    {
                Dimensions point 
    = new Dimensions();   
                Console.WriteLine(point);
                
                Dimensions point1;
                point1.length 
    = 100;
                point1.width 
    = 200;
                Console.WriteLine(point1);

                Console.ReadLine();
            }
       
        }


        
    public struct Dimensions
        
    {
            
    public double length;
            
    public double width;

            
    public override string ToString()
            
    {
                
    return this.length + " : " + this.width;
            }

        }

  • 相关阅读:
    openCR-用ROS代码获取电压的方法
    openCR-用ROS代码控制按键的方法
    TurtleBot3-基础例程
    TurtleBot3-键盘远程控制
    TurtleBot3-Topic Monitor
    openCR-串口打印HelloWorld
    Ubuntu播放音乐 mp3 wav
    SQL Server2008创建约束图解 转
    height、clientHeight、scrollHeight、offsetHeight区别
    第一次弄项目有感
  • 原文地址:https://www.cnblogs.com/FallingAutumn/p/432276.html
Copyright © 2020-2023  润新知