码迷,mamicode.com
首页 > Windows程序 > 详细

C#的数据类型,strut和枚举的使用方法

时间:2016-01-07 16:42:36      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

using System;

namespace Holle_World
{
  class Program
  {
  //enum枚举
  enum Week{ Monday, Tuesday, Wenesday, Thursday, Friday, Saturday, Sunday };

  // struct ,可自定义类型
  struct Person
  {  
    public string name;
    private bool sex;
    internal int age;
    //protected string x;

  }
  static void Main(string[] args)
  {  
    ///多少种变量类型,各有什么特点
    /// 常用的分为整型、浮点型、bool型、char型、引用型、decimal型
    //整型
    sbyte a = 0;//sbyte=System.Sbyte 8位有符号整数
    short b = 0;//short=System.Int16 16位有符号整数
    int c = 0; //int=System.Int32 32位有符号整数
    long d = 0;//long=System.Int64 64位有符号整数
    byte e = 0;//byte=System.Byte 8位无符号整数
    ushort f = 0;//ushort=System.UInt16 16位无符号整数
    uint g = 0;//uint=System.UInt32 32位无符号整数
    ulong h = 0;//ulong=System.UInt64 64位无符号整数

    //浮点型
    float I = 1.1f; //单精度
    double J = 1.1;//双进度,若无“f”,默认为双精度

    //bool(布尔)型
    bool k= true; //bool=System.Boolean true or false

    //char(字符)型
    char l = ‘a‘;//cahr=System.Char 16位的字符

    //应用类型
    object m;
    string n = "abc";

    //类型的强制装换
    int o= Convert.ToInt16(k);//把布尔型转换成整型
  
    ///如何调用struct
    /// struct必须在main方法之外编写
    ///1、实例化对象
    ///2、实例化后的对象调用struct中所定义的变量并进行赋值
    Person person = new Person();
    person.name = "李";
    Console.WriteLine(person.name);

    /// enum 枚举的调用
    /// enum是给变量数个限定的选项,限定了变量的可能性
    /// var可以代替数据的类型,在.net3.5以上使用
    /// enum需要在main方法之外赋值申明
    var day = Week.Tuesday;
    Console.WriteLine(day);
    Console.WriteLine((int)Week.Wenesday);
    Console.ReadLine();
    }
  }
}

C#的数据类型,strut和枚举的使用方法

标签:

原文地址:http://www.cnblogs.com/Small-Transparent/p/5109952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!