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();
}
}
}