C# 语言类型(五)—其他

总目录
C# 语法总目录

参考链接:
C#语法系列:C# 语言类型(一)—预定义类型值之数值类型
C#语法系列:C# 语言类型(二)—预定义类型之字符串及字符类型简述
C#语法系列:C# 语言类型(三)—数组/枚举类型/结构体
C#语法系列:C# 语言类型(四)—传递参数及其修饰符
C#语法系列:C# 语言类型(五)—其他

一、类型默认值

所有类型的实例都有默认值。

char类型默认值 是 ‘\0’

bool类型默认值 是 false

所有数字类型默认值 是 0

所有引用类型默认值 是 null

static int a;
static bool b;
static char c;
static string d;
static void Main(string[] args)
{
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
}
//输出
0
False



二、null 运算符 ?

C#提供了两个null处理运算符: null合并运算符null条件运算符

1. null 合并运算符

  null合并运算符写作 ?? 。意思是 “如果操作数不是null,则结果是操作数,否则结果是一个默认的值”。

namespace _036_显示转换和隐式转换 {
    class Program {
        static void Main(string[] args)
        {
            string ss = null;
            //ss = "ts";
            string res = ss ?? "nothing";
            Console.WriteLine(res);		//nothing

        }
    }
}
2. null条件运算符

  null条件运算符写作 ?. 意思是如果左值为null,则为null,否则为该值内的成员。

例如 :

namespace _036_显示转换和隐式转换 {
    class Program {

        static void Main(string[] args)
        {
            string ss = null;

            //ss = "NIHAO";				//输出nihao	
            ss = ss?.ToLower();
            Console.WriteLine(ss);		//输出空行
        }
    }
}

注意: null 两种运算符的混合使用如下

StringBuilder? sb = null;
string a = sb?.ToString() ?? "sb is null";
Console.WriteLine(a);
//输出
sb is null

三、语句特性

1. 带模式的 switch 语句(C#7)

object可以是任何类型的变量

static void TellMeTheType(object x)
{
    switch (x)
    {
        //这里的 i 称作模式变量
        case int i:
            Console.WriteLine("It's an int");
            Console.WriteLine($" {i} square is {i*i}");
            break;
        case double i:
            Console.WriteLine("It's an double");
            Console.WriteLine($" {i} square is {i * i}");
            break;
        case string i:
            Console.WriteLine("It's an string");
            Console.WriteLine("string is " + i);
            break;
        default:
            break;
    }
}

static void Main(string[] args)
{
    TellMeTheType(12);
    TellMeTheType("hello");
    TellMeTheType(12.5);
}
//It's an int
//12 square is 144
//It's an string
//string is hello
//It's an double
//12.5 square is 156.25
2. switch 语句 case使用条件 when

类似if判断

switch(x)
{
    case float f when f>100:
    case double d when d>100:
    case decimal m when m>100:
        Console.WriteLine("We can refer to x here but not f or d or m");
        break;
    case null:
        Console.WriteLine("Nothing here");
        break;
}
3. 跳转语句

C#中的跳转语句有 break , continue,goto,return和throw

(1) goto语句使用
int myInteger = 5;
goto mylabel;//goto语句用来控制程序跳转到某个标签的位置
myInteger++;
mylabel:Console.WriteLine(myInteger);
Console.ReadKey();

如果在try语句里面执行goto,那么在goto之前会执行finally块内语句。

goto语句无法从finally块中跳到块外,除非用throw

(2) break 语句

退出当前最近一层的 for 循环

(3)continue 语句

跳过当前最近一层的 for 循环

(4) throw 语句

抛出异常

4. using 语句

  这里的 using 语句和using指令用于命名空间不一样,这里的using语句主要作用是关闭一个持续流,例如自动关闭一个打开的文件,主要利用的是 调用 Dispose方法

{
    //这样写,可以省略 a.close()方法
	using var a = File.Open(@"D:\\a.txt", FileMode.Open);
}

//等同于
{
	var a = File.Open(@"D:\\a.txt", FileMode.Open);
	try
	{
		Console.ReadLine();
	}
	finally
	{
        //释放资源
		a.Dispose();
	}
}

总目录
C# 语法总目录

参考链接:
C#语法系列:C# 语言类型(一)—预定义类型值之数值类型
C#语法系列:C# 语言类型(二)—预定义类型之字符串及字符类型简述
C#语法系列:C# 语言类型(三)—数组/枚举类型/结构体
C#语法系列:C# 语言类型(四)—传递参数及其修饰符
C#语法系列:C# 语言类型(五)—其他

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值