1.变量
using System;
namespace lesson2_变量
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("变量");
#region 如何声明变量
//1.有符号的整形变量 是能存储 一定范围 正负数包括0的变量类型
// sbyte -128到127
// int -21亿到21亿多
// short -32768到32767
// long -9百万兆到9百万兆
//2.无符号的整形变量 是能存储 一定范围 0和正数的变量类型
// byte 0到255
// uint 0到42亿多
// ushort 0到65535
// ulong 0到18百万兆
//3.浮点数(小数)// 从非零数开始从左到右算有效数字,多的四舍五入
// float 存储7或8位有效数字 根据编译器不同 有效数字也可能不一样
// 在C#中 申明的小数 默认是double类型 加f是告诉系统 它是float类型
// double 存储15到17位有效数字
// decimal 存储27到28位的有效数字
// 在C#中 申明的小数 默认是double类型 加m是告诉系统 它是decimal类型
//4.特殊类型
// bool true false 表示真假的数据类型
// char 用来存储单个字符的变量类型(赋值的时候要用单引号括起来 例如char a = '唐';)
// string 字符串类型 用来存储多个字符 没有上限
#endregion
#region 为什么有那么多不同的变量类型
#endregion
}
}
}
2.变量本质
using System;
namespace lesson3_变量本质
{
class Program
{
static void Main(string[] args)
{
#region 变量的存储空间
// 通过 sizeof 方法 可以获取变量类型所占的内存空间(单位:字节)
// 有符号
int sbyteSize = sizeof(sbyte);
Console.WriteLine("sbyte 所占字节数位:" + sbyteSize);
int intSize = sizeof(int);
Console.WriteLine("int 所占字节数位:" + intSize);
int shortSize = sizeof(short);
Console.WriteLine("short 所占字节数位:" + shortSize);
int longSize = sizeof(long);
Console.WriteLine("sbyte 所占字节数位:" + longSize);
Console.WriteLine("***************************************");
// 无符号
int byteSize = sizeof(byte);
Console.WriteLine("byte 所占字节数位:" + byteSize);
int uintSize = sizeof(uint);
Console.WriteLine("uint 所占字节数位:" + uintSize);
int ushortSize = sizeof(ushort);
Console.WriteLine("ushort 所占字节数位:" + ushortSize);
int ulongSize = sizeof(ulong);
Console.WriteLine("ulong 所占字节数位:" + ulongSize);
Console.WriteLine("***************************************");
// 浮点数
int floatSize = sizeof(float);
Console.WriteLine("float 所占字节数位:" + floatSize);
int doubleSize = sizeof(double);
Console.WriteLine("double 所占字节数位:" + doubleSize);
int decimalSize = sizeof(decimal);
Console.WriteLine("decimal 所占字节数位:" + decimalSize);
Console.WriteLine("***************************************");
// 特殊类型
int boolSize = sizeof(bool);
Console.WriteLine("bool 所占字节数位:" + boolSize);
int charSize = sizeof(char);
Console.WriteLine("char 所占字节数位:" + charSize);
// sizeof 是不能够得到 string 类型所占的内存大小的
// 因为字符串长度是可变的 不定
Console.WriteLine("***************************************");
#endregion
}
}
}
3.变量的命名规则
using System;
namespace lesson4_变量的命名规则
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("变量的命名规则");
#region 必须遵守的规则
//1.不能重名
//2.不能以数字开头
//3.不能使用程序关键字命名
//4.不能有特殊符号(下划线除外)
//建议的命名规则:变量名要有含义->用英文(拼音)表示变量的作用
//非常不建议的命名规则:用汉字命名
#endregion
#region 常用命名规则
//驼峰命名法 首字母小写 之后每个词的首字母都大写(变量)
//帕斯卡命名法 所有单词首字母都大写(函数,类)
//潜在知识点:C#中对大小写是敏感的 是区分的
#endregion
}
}
}
4.常量
using System;
namespace lesson5_常量
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("常量");
#region 常量的声明
//关键字 const
//固定写法:
//const 变量类型 变量名=初始值;
//变量的声明
int i = 20;
//常量的声明
const int i2 = 20;
#endregion
#region 常量的特点
//1.必须初始化
//2.不能被修改
const float PI = 3.1415926f;
#endregion
}
}
}
5.转义字符
using System;
namespace lesson6_转义字符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("转义字符");
#region 转义字符的使用
//什么是转义字符?
//它是字符串的一部分 用来表示一些特殊含义的字符
//比如:在字符串当中表现 单引号 引号 空行 。。。
// string str="123"45";
#region 固定写法
//固定写法 \字符
//不同的 \ 和字符的组合 表示不同的含义
//常用转义字符
//单引号 \'
string str = "\'哈哈哈\'";
Console.WriteLine(str);
//双引号 \"
str = "\"哈哈哈\"";
Console.WriteLine(str);
//换行 \n
str = "123456789\n123456789";
Console.WriteLine(str);
//斜杠 \\
str = "哈\\哈\\哈";
Console.WriteLine(str);
//不常用转义字符(了解)
//制表符 \t(空一个tab键)
str = "哈\t哈哈";
Console.WriteLine(str);
//光标退格 \b
str = "123\b123";
Console.WriteLine(str);
//空字符 \0
str = "123\0123";
Console.WriteLine(str);
//警报音 \a
str = "\a";
Console.WriteLine(str);
#endregion
#endregion
#region 取消转义字符
string str2 = @"哈哈\哈哈";
Console.WriteLine(str2);
Console.WriteLine(@"123\123");
#endregion
}
}
}
6.类型转换_隐式转换
using System;
namespace lesson7_类型转换_隐式转换_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("类型转换-隐式转换");
//什么是类型转换
//类型转换就是不同变量类型之间的相互转换
//隐式转换的基本规则->不同类型之间自动转换
//大范围装小范围
#region 相同大类型之间的转换
//有符号 long ->int ->short ->sbyte
long l = 1;
int i = 1;
short s = 1;
sbyte sb = 1;
//隐式转换 int隐式转换成了long
//可以用大范围 装小范围 类型 (隐式转换)
l = i;
//不能够用小范围类型去装在大范围的类型
//i = l;
l = i;
l = s;
l = sb;
i = s;
//无符号 ulong ->uint ->ushort ->byte
ulong ul = 1;
uint ui = 1;
ushort us = 1;
byte b = 1;
//浮点数 decimal double ->float
decimal de = 1.1m;
double d = 1.1;
float f = 1.1f;
//decimal这个类型 没有办法用隐式转换的形式 去存储 double和float
//de=d;
//de=f;
//f