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;
        }

    }

posted @ 2006-06-22 07:23  FallingAutumn  阅读(2705)  评论(4编辑  收藏  举报