首页 > 代码库 > 结构和枚举

结构和枚举

结构可用来表示二维,三维数据。

枚举表示有限数据结合。

他们都是值类型

 

结构struct 

using System;  struct Point{    public double x, y;    public Point(int x, int y) {        this.x = x;        this.y = y;    }    public double R(){        return Math.Sqrt(x*x+y*y);    }}  class Test{    static void Main() {        Point[] points = new Point[100];        for (int i = 0; i < 100; i++)            points[i] = new Point(i, i*i);    }}

枚举 enum

using System;enum LightColor {    Red,    Yellow,    Green}class TrafficLight{    public static void WhatInfo(LightColor color) {        switch(color) {            case LightColor.Red:                Console.WriteLine(  "Stop!" );                break;            case LightColor.Yellow:                Console.WriteLine(  "Warning!" );                break;            case LightColor.Green:                Console.WriteLine(  "Go!" );                break;            default:                break;        }    }}  class Test{    static void Main()    {        LightColor c = LightColor.Red;        Console.WriteLine( c.ToString() );        TrafficLight.WhatInfo( c );    }}

 

结构和枚举