第一章 C#语法基础-----变量的范围

1、变量只能在定义之后使用

2、变量值在他所在的范围内有效,这个范围可以简单的理解成大括号{}内

3、但是大括号{}有嵌套的情况时,“父级”的变量可以用在“子级”中,“子级”的变量不可以用在“”父级中“”

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            同级无法调用
            //try
            //{
            //    int num = 1;
            //}
            //catch (Exception)
            //{
            //    Console.WriteLine(num);
            //}

            //父级可以用在子级中
            //int num = 1;
            //try
            //{
            //    Console.WriteLine(num);
            //}
            //catch(Exception)
            //{
            //    Console.WriteLine(num);
            //}

            //子级不能用在父级
            try
            {
                int num = 1;
            }
            catch (Exception)
            {

            }
            Console.WriteLine(num);
            Console.ReadKey();
        }
    }
}

算术运算符

+,-,*,/,%,++,--

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //数字类型的算术运算符
            int num = 1 * (int)1.25f;

            //string 类型的算术运算符
            //相当于连接符:"abc123",支持数字类型,但是不会做数学运算
            //为什么会得到一个乘法结果上的3
            string str = "abc" + "321" + 1 + 1.25*2;
            Console.WriteLine(num);
            Console.ReadKey();
        }
    }
}

赋值运算符

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 2;
            //赋值运算符左侧不能有非变量
            int num2 = num = 3;

            num++;
            num += 1;
            num *= 2;
            Console.WriteLine(num);
            Console.ReadKey();
        }
    }
}

布尔类型

用来表示“是”与“否”,比如是否已经登录,性别等状态

1、类型关键字  Bool

2、值只有两种,默认值是false;真(true),假(false);

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            定义bool
            //bool b1 = true;
            //bool b2 = false;
            //Boolean b3 = true;

            string password = "123456";
            //==是一个符号·
            bool check = password == "123456";
            //Console.WriteLine(b1);
            Console.ReadKey();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_41392061

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值