• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
博雅居
要想跟上时代,就得不断学习!
博客园    首页    新随笔    联系   管理    订阅  订阅
结构和枚举

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

枚举表示有限数据结合。

他们都是值类型

 

结构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 );
    }
}

 

If opportunity doesn’t knock, build a door
posted on 2015-01-18 09:26  博雅居  阅读(165)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3