C#学习五 表达式

本文详细介绍了C#中的数值提升概念,通过查看VS编译后的低级源代码来理解表达式的数据类型。此外,文章还探讨了语句的定义,包括声明语句、嵌入式语句、循环语句以及continue和break的用法,并提醒读者在处理输入类型错误时的注意事项。最后,对foreach循环和return语句进行了讲解,并提出了尽早return的原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数值提升

表达式的数据类型是各数据最高的数据类型
表达式也有类型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式类型
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 100;
            int b;
            Console.WriteLine(b=a);
            Console.WriteLine((b=a).GetType().FullName);
        }
    }
}
//运行结果:100
//System.Int32

查看经过VS编译后的低级源代码

  • 打开文件代码在资源管理器中的位置

在这里插入图片描述

  • 依次打开bin-debug:在里面可以看见编译后生成的.exe文件
    在这里插入图片描述

  • 选择VS工具:Developer Command Prompt for VS 2019
    在这里插入图片描述

  • 在弹出的控制台中输入:ildasm,将程序拖入窗口程序中,就可以看见程序结构
    在这里插入图片描述

  • 双击对应的函数就可以看见CPU的执行语句

  • 在这里插入图片描述

语句的定义

陈述算法思想,控制逻辑走向,完成有意义的动作
C#的程序以“;”结束,但是由分号结尾的不一定是语句(引入名称空间的代码,虽然他是以";"结尾,但是他不是语句。同样的,字段的声明也不是语句)
语句一定要出现在方法体里面

处理输入的类型错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 异常处理
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = Console.ReadLine();
            try
            {
                double t = double.Parse(a);
                if (t>69)
                {
                    Console.WriteLine("这是一个大于69的数");

                }
                else
                {
                    Console.WriteLine("这个数字小于69");
                }
            }
            catch       //用于处理可能出现的所有错误
            {
                Console.WriteLine("这不是一个数字");
            }
        }
    }
}

//输入:34    输出:这个数字小于69
//输入:dgwywqgqwu   输出:这不是一个数字

语句的详解

标签语句(不常见)

声明语句

嵌入式语句

嵌入结构:例如:if嵌套的语句(块语句)

循环语句

  • while语句
    注意使用try-catch语句处理可能出现的异常(类型异常和超限异常)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 循环结构
{
    class Program
    {
        static void Main(string[] args)
        {
			try
			{
                bool run_or_not = true;
                while (run_or_not)
                {
                    Console.WriteLine("请输入第一个数字");
                    int first_number = Int16.Parse(Console.ReadLine());
                    Console.WriteLine("请输入第二个数字");
                    int second_number = Int16.Parse(Console.ReadLine());
                    int sum = first_number + second_number;
                    run_or_not = (sum == 100) ? false : true;
                    if(run_or_not)
                    {
                        Console.WriteLine("你输入的数字和不为100!");
                    }
                    else
                    {
                        Console.WriteLine("你输入的数字和为1003");
                    }
                }

            }
			catch (FormatException)
			{
                Console.WriteLine("对不起你输入的内容格式不正确!!!!");
			}
            catch(OverflowException)
            {
                Console.WriteLine("对不起您输入的数值太大了!!");
            }

        }
    }
}

continue-break

continue是放弃当前执行的循环,然后开始下一次的循环;而break是直接跳出循环,所有循环都将不再执行。

foreach

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace foreach语句
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = new int[] { 1, 2, 3, 4, 5, 6 };
            foreach (var i in intArray)
            {
                Console.WriteLine(i);
            }
        }
    }
}

return

尽早return原则:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 尽早return原则
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Jom";
            Greeting(name);
            Greeting2(name);
            name = "";
            Greeting(name);
            Greeting2(name);

        }
        static void Greeting(string name)       //原始表达
        {
            if (!string.IsNullOrEmpty(name))
            {
                Console.WriteLine("Hello1,{0}!",name);
            }
        }

        static void Greeting2(string name)		//尽早return
        {
            if (string.IsNullOrEmpty(name))
            {
                return; //此处有return语句,将不会再执行后面的语句
            }
            Console.WriteLine("Hello2,{0}!", name);
        }
    }
}
//运行结果: Hello1,Jom!
//			Hello2,Jom!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值