C#语法基础

  • 定义数组
// 先声明再赋值
string[] strs = new string[3];
int[] arr = new int[3];

strs[0] == null     // true,默认为null
arr[0]              // 0,默认为0

strs = {"a", "b", "c"};     // 报错,不能这样赋值

// 声明并初始化,都正确
string[] strs = new string[3]{"a", "b", "c"};
string[] strs = new string[]{"a", "b", "c"};
string[] strs = {"a", "b", "c"};

// 引用类型,指向同一内存地址
string[] temp = strs;
temp[0] = "temp";
strs[0]             // "temp"

string类型

  • 格式化输出
Console.WriteLine("{0} is {1} {2}.", "this", "a", "test");
// this is a test.

// 顺序对应
Console.WriteLine("{2} is {1} {1}.", "this", "a", "test");
// test is a a.
  • 其他
"" == string.empty;                 // true
string.IsNullOrEmpty(string.empty); // true
string.IsNullOrEmpty(null);         // true

DateTime类型

  • 属性
DateTime dt = DateTime.Now;

// 月和天都不带0,从1开始,7月16日
dt.Year
dt.Month    // 7
dt.Day      // 16
dt.Hour     // 14,24小时制
dt.Minute
dt.Second
dt.Millisecond
  • 格式化
DateTime dt = new DateTime(2018, 7, 7, 7, 7, 7, 7);

// 注意不足10的情况,是否带0
dt.ToString();          // 2018/7/7 7:07:07
dt.ToLongDateString()   // 2018年7月7日
dt.ToShortDateString()  // 2018/7/7
dt.ToLongTimeString()   // 7:07:07
dt.ToShortTimeString()  // 7:07

// H表示24h制,h表示12小时制
dt.ToString("yyyy-MM-dd HH:mm:ss");     // 2018-07-07 07:07:07
dt.ToString("y-M-d h:m:s");             // 18-7-7 7:7:7

// Y、D、S不会格式化,其他字符对应输出
dt.ToString("Y-D-S s/d/y");             // Y-D-S 7/7/18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值