类和结构是C#语言中最常用的内型,在使用和申明结构、类型时,需注意哪几个方面?

来源:百度知道 编辑:UC知道 时间:2024/06/04 04:22:52
高手指点哦

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