c# string 转 datetime_C# 类型

C# 类型

在C#中类型可分为值类型和引用类型,下图列出了我们使用的所有类型及他们的类别。

14c4994dbc617ec79c3d1aae7824359a.png

值类型

sbyte与byte

static void Main(string[] args){  //sbyte取值范围  Console.WriteLine("sbyte的最小值为:"+ sbyte.MinValue);  Console.WriteLine("sbyte的最大值为:" + sbyte.MaxValue);    //byte 与各个类型的转换  byte[] intTobyte = BitConverter.GetBytes(int.MaxValue);  Console.WriteLine("int所占字节数:" + intTobyte.Length);   byte[] boolTobyte = BitConverter.GetBytes(false);  Console.WriteLine("bool所占字节数:" + boolTobyte.Length);  byte[] charTobyte = BitConverter.GetBytes(char.MaxValue);  Console.WriteLine("char所占字节数:" + charTobyte.Length);  byte[] doubleTobyte = BitConverter.GetBytes(double.MaxValue);  Console.WriteLine("double所占字节数:" + doubleTobyte.Length);  byte[] shortTobyte = BitConverter.GetBytes(short.MaxValue);  Console.WriteLine("short所占字节数:" + shortTobyte.Length);  byte[] ulongTobyte = BitConverter.GetBytes(ulong.MaxValue);  Console.WriteLine("ulong所占字节数:" + ulongTobyte.Length);  byte[] longTobyte = BitConverter.GetBytes(long.MaxValue);  Console.WriteLine("long所占字节数:" + longTobyte.Length);  byte[] floatTobyte = BitConverter.GetBytes(float.MaxValue);  Console.WriteLine("float所占字节数:" + floatTobyte.Length);  byte[] ushortTobyte = BitConverter.GetBytes(ushort.MaxValue);  Console.WriteLine("ushort所占字节数:" + ushortTobyte.Length);  byte[] uintTobyte = BitConverter.GetBytes(uint.MaxValue);  Console.WriteLine("uint所占字节数:" + uintTobyte.Length);}

输出:

SByte的最小值为:-128
SByte是最大值为:127
sbyte的最小值为:-128
sbyte的最大值为:127
int所占字节数:4
bool所占字节数:1
char所占字节数:2
double所占字节数:8
short所占字节数:2
ulong所占字节数:8
long所占字节数:8
float所占字节数:4
ushort所占字节数:2
uint所占字节数:4

数值

在C#下列类型用于数值的计算,根据类型的取值范围不同,它们被用于各种各种精度不通的计算。
float/short/ushort/double/int/uint/long/ulong/decimal

下面将介绍几个常用数值的使用

int

例:两数相加

  class Program    {        static void Main(string[] args)        {            //int 类型的使用 例:两数相加            int a = Sum(1, 2);            int b = Sum(-1, -2);            int c = Sum(-2, 2);            Console.WriteLine("a的值为:" + a);            Console.WriteLine("b的值为:" + b);            Console.WriteLine("c的值为:" + c);        }        //两数相加        public static int Sum(int a, int b)        {            return a + b;        }    }

输出:
a的值为:3
b的值为:-3
c的值为:0

float

//两数之商

 class Program    {        static void Main(string[] args)        {            float f = Quotient(6.2f, 0.2f);            Console.WriteLine("f的值为:"+ f);        }        //两数之商        public static float Quotient(float a, float b)        {            return a / b;        }    }

输出:
f的值为:30.999998

double

例:两数之积

   
 class Program    {        static void Main(string[] args)        {            double d = Product(3.1415926, 3.1415926);            Console.WriteLine("d的值为:" + d);        }        //两数之积        public static double Product(double a, double b)        {            return a * b;        }    }

输出:
d的值为:9.86960406437476

struct

例:定义一本书

class Program {        struct Book        {            public string title;            public string author;            public string subject;            public int book_id;        }        static void Main(string[] args){             //结构体            Book Book1;            Book1.title = "C Programming";            Book1.author = "Nuha Ali";            Book1.subject = "C Programming Tutorial";            Book1.book_id = 6495407;            Console.WriteLine("Book 1 title : {0}", Book1.title);            Console.WriteLine("Book 1 author : {0}", Book1.author);            Console.WriteLine("Book 1 subject : {0}", Book1.subject);            Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);        }}

输出:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id :6495407

enum

例:周一到周末

 
class Program    {        enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };        static void Main(string[] args)        {            int x = (int)Day.Sun;            int y = (int)Day.Fri;            Console.WriteLine("Sun = {0}", x);            Console.WriteLine("Fri = {0}", y);        }    }

输出:
Sun = 0
Fri = 5

引用类型

object

    class Program    {        static void Main(string[] args)        {            objInt = 5;            objString = "字符串";            objBool = false;            Console.WriteLine("objInt:" + objInt);            Console.WriteLine("objString:" + objString);            Console.WriteLine("objBool:" + objBool);        }    }

输出:
objInt:5
objString:字符串
objBool:False

string

例:拼接字符串

class Program    {        static void Main(string[] args)        {            string str = Splicing("我是", "小明");            Console.WriteLine(str);        }         //拼接字符串        public static string Splicing(string a, string b)        {            return a + b;        }    }

输出:
我是小明

dynamic

动态对象

  
class Program    {        static void Main(string[] args)        {            //dynamic            dynamic d1 = 7;            dynamic d2 = "a string";            dynamic d3 = System.DateTime.Today;            dynamic d4 = System.Diagnostics.Process.GetProcesses();            int i = d1;            string strd = d2;            DateTime dt = d3;            System.Diagnostics.Process[] procs = d4;            Console.WriteLine("i:" + i);            Console.WriteLine("str:" + strd);            Console.WriteLine("dt:" + dt);        }    }

输出:
i:7
str:a string
dt:2020/6/13 0:00:00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值