C#1115变量
·
变量
值类型
枚举
//枚举a
enumt emumt2 = enumt.OK;
//获取枚举变量的值
int val = (int)emumt2;
//
int o = 1;
//获取枚举名称
string oname = Enum.GetName(typeof(enumt),o);
//根据值获取枚举对象
enumt otype= (enumt)Enum.Parse(typeof(enumt), o.ToString());
//根据枚举名称获取枚举对象
enumt otype2 = (enumt)Enum.Parse(typeof(enumt), oname);
//获取枚举名称数组
string[] names = Enum.GetNames(typeof(enumt));
//获取枚举值数组
var values = Enum.GetValues(typeof(enumt));
结构体

public struct Student
{
public int id;
public string name;
public int age;
public void GetStudent()
{
Console.WriteLine("id=" + id+",name=?"+name+",age="+age);
}
public Student(int id, string name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
}
static void Main(string[] args)
{
//结构体对象
//无参构造
Student student = new Student();
student.id = 2;
student.name = "xiaoli";
student.age = 12;
student.GetStudent();
//带参构造
Student student1 = new Student(3,"XIAOZHAO",21);
student1.GetStudent();
}
引用类型
//引用类型不存储数据值,存储引用
//1个变量改变内存值 都变
//string Dynamic Object
int value2 = 123;
object ol = value2;//装箱
int intol = (int)ol;//拆箱
//Dynamic 可存储任何类型
//程序运行时进行类型检查,和对象类型不同
dynamic na = "ss";
dynamic num1 = 222;
dynamic ll = false;
//string
string na1 = "ss";
string path = "d:\\document\\imgs";
string path1 = @"d:\document\imgs";
//null 空引用
//null 与 ""区别 ""会分配存储空间,长度为0 而null不会。
//""等价于 string.Empty 修正 实际值一样
隐式转换
//隐式类型转换 从小变大 从派生类到基类,数据不丢失
//隐式类型转换 从小变大 从派生类到基类
short shortnum=23;
int intnum = shortnum;
long longnum = intnum;
float floatnum = 3.6f;
double doublenum = floatnum;
float floatnum2 = intnum;
double doublenum2 = intnum;
decimal decnum = longnum;








// decimal decnum=doublenum /double/float类型不能隐式转换为decimal
decimal decnum = doublenum;

显式转换
type(num)
//显式类型转换
//强转 大转小 浮点转整 字符串转值 可能数据丢失
double doublenumq = 3.56;
int intnumq = (int)doublenumq;



type.Parse(string)字符串类型转换为值类型
//type.Parse(string)
string stringnum = "222.2";
float floatnumq = float.Parse(stringnum);
string stringDec = "222.32";
decimal decimalnum = decimal.Parse(stringDec);
}
}





c#的值类型
引用类型:C# 中内置的引用类型包括 Object(对象)、Dynamic(动态)和 string(字符串)。
//
string str = "22.31";
int intnum3 = 0;
bool result = int.TryParse(str, out intnum3);
string strnum = "32.3";
result = float.TryParse(strnum, out float floatnums);





//无法转换也不报错 type.TryParse(string,out type变量)
string str = "22.31";
int intnum3 = 0;
//小数无法转换为int
bool result = int.TryParse(str, out intnum3);//无法转换
//如果成功,转换成功str 失败 false
string strnum = "32.3";
result = float.TryParse(strnum, out float floatnums);
//convert.ToType(value) 一种类型转换为指定类型
object oNum = 120;
byte byteNum = Convert.ToByte(oNum);
string strNum = "23123";
int intNum = Convert.ToInt32(strNum);
bool boolValue = false;
string stringBoval = boolValue.ToString();
double doubleV = 322.21;
float floatV = Convert.ToSingle(doubleV);
}
}
}











运算符
算数运算符
//算数运算符
int a = 30,b = 15;
int c = a + b;
int d = a - b;
int e = a * b;
int f = a / b;
int g = a % b;
int a1 = a++;
int b2 = --b;
Console.WriteLine($"a={a},b={b},c={c},d={d},e={e},f={f},g={g},a1={a1},b2={b2}");




关系运算符
== >= <= < > !=
int ax=15,bx=2;
bool sx = ax == bx;
bool sx2 = ax != bx;
bool sx3 = ax > bx;
bool sx4 = ax < bx;
bool sx5 = ax >= bx;
Console.WriteLine($"sx={sx},sx2={sx2},sx3={sx3},sx4={sx4},sx5={sx5}");

逻辑运算符
&& || !
int al = 12, bl = 2, cl = 23, dl = 32;
bool rel1 = al > bl;
bool rel2 = al < bl;
bool rel3 = rel1 && rel2;
bool rel4 = cl >= dl;
bool rel5 = rel4 || rel3;
Console.WriteLine($"rel1={rel1},rel2={rel2},rel3={rel3},rel4={rel4},rel5={rel5}");

位运算符
| & ~ ^


赋值运算符
//赋值运算符
int af = 30;
int bf = 10;
bf += af;
Console.WriteLine($"bf={bf}");
bf -= 6;
Console.WriteLine($"bf={bf}");
bf *= 2;
Console.WriteLine($"bf={bf}");
bf /= 2;
Console.WriteLine($"bf={bf}");
bf %= 10;
Console.WriteLine($"af={af},bf={bf}");
int cf = 4;
//100
cf <<= 1;
//1000=8
Console.WriteLine(cf);
cf >>= 1;
int df = 5;
df &= 2;
//5=101 2=010
//101 & 020 =000
Console.WriteLine($"cf={cf},df={df}");
int ef = 6;
ef ^= 4;
// ef=110 4=100
//110 ^ 100=010;2 按位异或 相同为0 不同为1
Console.WriteLine($"ef={ef}");
int ff = 31;
ff |= 32;
//ff 10000-1=01111
//32 10000
//01111 | 10000 = 11111=63
Console.WriteLine($"ff={ff}");
}
}


其他运算符
sizeof
int intSize = sizeof(int);
int floatSize = sizeof(float);
int decimalSize = sizeof(decimal);
int enumSize = sizeof(enumt);
int charSize = sizeof(char);
Console.WriteLine($"intsize={intSize},floatS={floatSize}," +
$"decimalS={decimalSize},enumS={enumSize},charS={charSize}");

typeof
//typeof
Type typeInt=typeof(int);
Type typeStri=typeof(string);
Type typefloat=typeof(float);
Type typeDecimal=typeof(decimal);
Type typeEnum=typeof(enumt);
Type types = typeof(Student);
Console.WriteLine("typeint:" + typeInt + ",typestr:" + typeStri+",typefloat:"+typefloat+",typeDe:"+typeDecimal
+"typeEnum:"+typeEnum+"typestudent:"+types);

条件?A:B
//?:三目运算符
int asan = 21;
string peopleAge = asan < 18 ? "child" : "adult";
Console.WriteLine("people:" + peopleAge);

is
//is
object ai = 22;
bool si = ai is int;
Console.WriteLine("ai=?int:"+si);

as
//int intobj=(int)obj; // 错误:字符串不能直接强制转换为int
//int intobj2 = obj as int; // 错误:as运算符不能用于值类型



运算符优先级
int ay = 5;
int by = 4, cy = 6;
int dy = ay + by * cy;
//dy=5+4*6=5+24=29;
int ey = by > cy ? 1 : 0;
//ey=4>6 x =0
int fy = (ay + by) * cy - dy;
//fy=9*6-29=26 25
int gy = ++ay + by * (ay - by);
//6+4*(1)=10 //6+4*2=14
bool hy = (ay > by) || (cy > by);
//true || true =true
Console.WriteLine("dy:"+dy+",ey:"+ey+",fy:"+fy+",gy:"+gy+",hy:"+hy);





更多推荐


所有评论(0)